所以,我想用JPanel制作一个菜单屏幕,我让它工作起来,但是当我按下“开始”按钮时,它没有关闭菜单窗口,它只是创建了一个新窗口,如何将它保持在同一个窗口上,而不关闭/打开菜单窗口,或者当我按下“开始”按钮时,我想关闭菜单窗口并打开“游戏”窗口(JPanel)。
这是MainClass.java
package bombermangame;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame{
private static final long serialVersionUID = 1L;
public static int WIDTH = 870, HEIGHT = 800;
public static JPanel menu = new Menu();
public static Listener keys = new Listener();
public MainClass(){
setContentPane(menu);
pack();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("BomberMan V0.3");
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new MainClass();
}
}下面是Menu.java类
package bombermangame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Menu extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton startButton = new JButton("Play");
private int x = 0, y = 500;
private boolean down = false;
private boolean up = true;
private Timer timer = new Timer();
public Menu() {
setBackground(Color.blue);
startButton = new JButton("Start");
startButton.setBounds(0,0, 100, 40);
startButton.setPreferredSize(new Dimension(100, 40));
startButton.addActionListener(this);
startButton.setFocusPainted(true);
this.add(startButton);
public void actionPerformed(ActionEvent ae) {
Object a = ae.getSource();
Game game = new Game();
MainClass frm = new MainClass();
Listener keys = new Listener();
if (a == startButton) {
timer.cancel();
frm.getContentPane().remove(new Menu());
frm.addKeyListener(keys);
frm.setContentPane(game);
frm.revalidate();
frm.repaint();
game.setBackground(Color.BLACK);
game.setDoubleBuffered(true);
game.setBounds(0, 0, WIDTH, HEIGHT);
Game.running = true;
}
}
}编辑:多亏了@威士忌蜘蛛的帮助,我才知道我制作了2帧,但没有正确引用它们。但是现在我已经解决了这个问题,当我解决这个问题时,我的Jpanel就不能和我的侦听器一起工作了。我尝试过将听众直接添加到我的游戏JPanel和我的MainClass JFrame中,但这两种方法都不起作用。
这是我的一些菜单课,
public void actionPerformed(ActionEvent ae) {
Object a = ae.getSource();
JPanel game = new Game();
Listener keys = new Listener();
if (a == startButton) {
timer.cancel();
MainClass.frame.getContentPane().remove(this);
MainClass.frame.setContentPane(game);
MainClass.frame.addKeyListener(keys);
game.addKeyListener(keys);
game.setBackground(Color.BLACK);
game.setDoubleBuffered(true);
game.setBounds(0, 0, WIDTH, HEIGHT);
Game.running = true;
}
}发布于 2015-04-17 20:39:35
您在这里创建了一个MainClass:
public static void main(String[] args) {
new MainClass();
}..。再说一次这里..。
public void actionPerformed(ActionEvent ae) {
Object a = ae.getSource();
JPanel game = new Game();
JFrame frm = new MainClass();然后,当您尝试删除该菜单时,而不是将其引用到现有菜单,而是创建了一个新的菜单:
frm.getContentPane().remove(new Menu());您需要重新考虑您的设计,并确保您引用的是正确的(已经存在的)对象。也就是说,当引用现有的对象时,您正在创建新的对象。
https://stackoverflow.com/questions/29708769
复制相似问题