Sign in to follow this  
The C modest god

[java] JFrame questions

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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);
}

}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this