0

I'm trying to change the content of the Jframe so it is from the menu screen to the game itself. It is like you click the start game then you will be able to play the game. So far I have successfully changed the contents but the controls of the game aren't able to be detected from my code. For example, I press Space so the character from the game will shoot but whenever I press it I cannot notice anything happening. I've seen the topics that are quite similar to this but those haven't helped me solve my issue. So, here's my code:

package rtype;

import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Rtype extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
public Rtype() {
    setSize(1020, 440);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setTitle("PROJECT JAEGER");
    setResizable(false);
    setVisible(true);
    JButton startButton = new JButton("START GAME");//The JButton name.
    add(startButton);//Add the button to the JFrame.
    startButton.addActionListener(this);//Reads the action.
}

public static void main(String[] args) {
    new Rtype();
}
public void actionPerformed(ActionEvent i) {
    getContentPane().removeAll();
    add(new Board());
    setVisible(true);  
        System.out.println("The Button Works!");//what the button says when clicked.
    }
}

Calling add(new Board()); summons the game. The Board is the class of the game.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
  • For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Sep 14 '13 at 08:02

1 Answers1

1

The solution is very simple. After changing what is in the frame, you need to invoke revalidate to make the changes take effect. Just add this line of code to the end of actionPerformed and it will work:

revalidate();
tbodt
  • 16,609
  • 6
  • 58
  • 83
  • One other change: Move `setVisible(true)` to the end, after you add the button. – tbodt Sep 14 '13 at 00:33
  • still the same :( here's the current content of the actionperformed: public void actionPerformed(ActionEvent i) { getContentPane().removeAll(); add(new Board()); System.out.println("The Button Works!");//what the button says when clicked. revalidate(); setVisible(true); } – user2680383 Sep 14 '13 at 00:36
  • I tested it by adding `revalidate` and putting `setVisible(true)` at the end, and it worked. – tbodt Sep 14 '13 at 00:36
  • I don't have your `Board` class, so I can't test for sure, but you could also try giving the board input focus, by calling `requestFocus on the board after adding it. – tbodt Sep 14 '13 at 00:38
  • I mean it does changes the content but when I press certain controls like space so the chracter in the game will shoot, but the chracter does nothing. – user2680383 Sep 14 '13 at 00:38
  • setVisible(true); in actionPerformed is showing the same frame, is Board another Frame or what kind of component is Board? – porfiriopartida Sep 14 '13 at 00:48
  • Board is just another class that needs a Frame to run. So Board appears on the same frame. – user2680383 Sep 14 '13 at 00:51
  • Can you update the code as you are using it right now? I don't see the revalidate() call – porfiriopartida Sep 14 '13 at 00:51
  • Thank you guys for the replies! It now works! But the only problem is I need to switch windows first then the character will shoot once I get back to the Jframe's window. Is there a way to prevent that? – user2680383 Sep 14 '13 at 00:54
  • That's a separate question. – tbodt Sep 14 '13 at 00:58
  • Here's the link :D http://stackoverflow.com/questions/18797337/java-jframe-needs-to-switch-windows-first-before-controls-can-be-used sorry the title is messed up – user2680383 Sep 14 '13 at 01:15