Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

[java] JFrame questions

This topic is 4494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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);
}

}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!