Java改变了JFrame问题的内容

时间:2013-09-14 00:27:57

标签: java swing

我正在尝试更改Jframe的内容,因此它是从菜单屏幕到游戏本身。就像你点击开始游戏然后你就可以玩游戏了。到目前为止,我已成功更改了内容,但无法从我的代码中检测到游戏的控件。例如,我按 Space ,这样游戏中的角色就会拍摄,但每当我按下它时,我都无法注意到发生的任何事情。我已经看到了与此非常相似的主题,但这些主题并没有帮助我解决我的问题。所以,这是我的代码:

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.
    }
}

致电add(new Board());召唤游戏。董事会是游戏的一员。

1 个答案:

答案 0 :(得分:1)

解决方案非常简单。更改框架中的内容后,您需要调用revalidate以使更改生效。只需将这行代码添加到actionPerformed的末尾即可,它将起作用:

revalidate();