1

Alright so here's the problem:

I created this small guessing game and wanted to make it gui based... but it appears in two different windows -

  1. first window is the menu(button and label)
  2. second window is activated by the button and has the game in it.

Is there a way for me to have both windows in one- AS IN the first window gets overwritten by the second window?

public class Skeleton extends JFrame implements ActionListener
    {   

JPanel glass = new JPanel();
JButton btn = new JButton("Start");
TextField tf = new TextField();
JLabel label = new JLabel("Enter Guess Here: ");
JLabel answerLabel = new JLabel("Answer:...");
Board bob =new Board();

public Skeleton()
{
    setIconImage(new ImageIcon("icon.png").getImage());
    getContentPane().setBackground(Color.darkGray);
    getContentPane().setForeground(Color.black);
    setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
    add(label);
    label.setForeground(Color.black);
    add(tf);
    tf.setText("");
    add(btn);
    btn.setBackground(Color.green);
    btn.addActionListener(this);
    add(answerLabel);
    answerLabel.setForeground(Color.black);


    setTitle("Guessing Game");
    setDefaultCloseOperation(EXIT_ON_CLOSE);    
    setSize(300,200);
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(true);


}
public void actionPerformed(ActionEvent e)
{//GAME FRAME
    JFrame frame = new JFrame();
    int userGuess= Integer.parseInt(tf.getText());
    frame.add(bob);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setSize(300,285);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setIconImage(new ImageIcon("icon.png").getImage());
...........

This is obviously not the full code, just the part that uses the GUI.

NOTE: I do have some other within this actionevent which occur after the button is clicked.

Also this is my first game ever :D (if i can call it one)

vamosrafa
  • 685
  • 5
  • 11
  • 35
Shrey
  • 671
  • 7
  • 14
  • 2
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) for ideas. 2) Don't extend frame or other top level containers. Instead create & use an instance of one. 3) Don't set the size of top level containers. Instead layout the content & call `pack()`. – Andrew Thompson Jun 17 '13 at 11:40
  • 1
    You should be using [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) for this scenario. Hopefully this [example](http://stackoverflow.com/a/9349137/1057230) be of some help on the topic :-) – nIcE cOw Jun 17 '13 at 14:09

2 Answers2

2

You create new JFrame into "actionPerformed" method, so it's normal to have two frames (the Skeleton instance and the new one).

Just use "this" instead of a new JFrame instance to keep only one JFrame instance.

LoganMzz
  • 1,597
  • 3
  • 18
  • 31
2

Each JFrame will create a new window. If you want to display everything into the same window, create only one JFrame and change its content.

You can change the whole content of a JFrame by setting its contentPane (frame.setContentPane(...)) or by adding/removing Panel to it.

I suggest to take a look at JPanel since you will use it a lot.

Raphaël
  • 3,646
  • 27
  • 28
  • 1
    For many components in one space, a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as see in this [short example](http://stackoverflow.com/a/5786005/418556) is a better option. – Andrew Thompson Jun 17 '13 at 11:42