[java] JFrame questions

Started by
0 comments, last by 5MinuteGaming 18 years ago
How would I implement this, I want to create a window that will have two buttons: Start and Exit. If I press the exit button it will close the window. If I press Start it will create a new different window and close the older one. I want to implement it with the Model View Controler design. How would I do this? I know how to create buttons, with extending a JPanel. However, if I have a class A which extends the JPanel and have these buttons. Then I do "getContentPane().add(ui, BorderLayout.CENTER)" withe the JFrame class. How would I make the buttons in A do what I wanted them to do? Thanks in advance.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
For model-view-controller design I usually have the view contain a reference to the controller object and pass the controller object a model object.

For a simple frame such as one with two buttons Start and Exit you would probably not want to use a model-view-controller since you really have no model.

But here is the code anyways. Just to get you started! Hope that helps!

ViewPrototype
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class ViewPrototype extends JFrame {	private JButton btnStart = new JButton("Start");	private JButton btnExit = new JButton("Exit");	private Controller ctrl = new Controller( new Model() );		public ViewPrototype() {        	super("View Prototype");          		JPanel main = new JPanel( new BorderLayout() );        	main.setBorder( new EmptyBorder(5,5,5,5) );		main.add( btnStart, "West" );		main.add( btnExit, "East" );		getContentPane().add(main);		addWindowListener( 			new WindowAdapter() {				public void windowClosing( WindowEvent e ) {					setVisible(false);					dispose();				}			}		);		//just to keep things encapsulated		addListeners();		pack();		setVisible(true);	}	private void addListeners() {		btnStart.addActionListener(			new ActionListener() {				public void actionPerformed( ActionEvent e ) {					setVisible(false);					dispose();					ctrl.startNewGame();				}			}		);		btnExit.addActionListener( 			new ActionListener() {				public void actionPerformed( ActionEvent e ) {					ctrl.exit();				}			}		);			}	public static void main( String args[] ) {		new ViewPrototype();	}}


Controller
public class Controller {	private Model mData = null;	public Controller( Model m ) {		mData = m;			}	public void startNewGame() {		new ViewNewGame(this);	}	public void exit() {		//cleanup the data if need be or save the data.			System.exit(0);	}}


Model
/* * This class is to be used for storing information for the game * such as configuration settings, player data, highscores, etc... */public class Model {	public Model() {		//empty model	}}


ViewNewGame
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class ViewNewGame extends JFrame {	private Controller ctrl = null;	public ViewNewGame( Controller c ) {		super("The Game");		ctrl = c;		addWindowListener(			new WindowAdapter() {				public void windowClosing( WindowEvent e ) {					setVisible(false);					dispose();					ctrl.exit();				}			}		);		setSize(640,480);		setVisible(true);	}}

This topic is closed to new replies.

Advertisement