我基本上一直试图通过使用JFrame并切换出2个JPanel(一个用于菜单,另一个用于实际游戏)来制作游戏菜单。我试图实现我能想到的最基本的格式,但似乎无法使其工作。如果有人可以解释代码有什么问题,我将不胜感激。在单个JFrame中使用多个JPanel
这里是我的JFrame,菜单面板,以及ActionListener的
package buttonMenu;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Skeleton extends JFrame implements ActionListener{
JPanel menu;
JButton button;
public Skeleton(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
menu = new JPanel();
button = new JButton("button");
menu.setSize(400, 400);
menu.setBackground(Color.BLACK);
menu.setVisible(true);
menu.add(button);
button.setLocation(200, 200);
button.addActionListener(this);
add(menu, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent a){
JPanel panel = Game.Game();
this.remove(menu);
this.add(panel);
}
public static void main(String args[]){
new Skeleton();
}
}
的actionPerformed调用这个类中创建
package buttonMenu;
import java.awt.Color;
import javax.swing.JPanel;
public class Game{
public static JPanel Game(){
JPanel panel = new JPanel();
panel.setSize(400, 400);
panel.setBackground(Color.WHITE);
return panel;
}
}
同样的面板,如果任何人都可以向我解释什么是错,此代码我将不胜感激。由于
感谢您的建议,我从来没有使用过重新验证,但它现在工作。此外,感谢您的链接,它看起来像卡布局是要走的路。 –