从 JFrame [Java] 关闭 JPanel window
Closing off a JPanel window from a JFrame [Java]
所以,我想用 JPanel 制作一个菜单屏幕,我让它工作了,但是当我按下开始按钮时,它并没有关闭菜单 Window,它只是制作了一个新 window,我该怎么做,保持不变 window,没有 closing/opening 菜单 window,或者我想关闭菜单 window然后打开游戏 window(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.javaclass
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;
}
}
}
编辑:感谢@whiskeyspider 的帮助,我了解到我制作了 2 帧但没有正确引用它们。但是现在我已经解决了这个问题,我的监听器有问题,当我修复这个问题时,我的 Jpanel 将无法与我的监听器一起工作。我已经尝试将 Listener 直接添加到我的 Game JPanel 和我的 MainClass JFrame,但两者都不起作用。
这是我的一些菜单 class,
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;
}
}
您在此处创建了 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());
您需要稍微重新考虑您的设计并确保您引用了正确的(已经存在的)对象。也就是说,当您引用 现有 个对象时,您正在创建 new 个对象。
所以,我想用 JPanel 制作一个菜单屏幕,我让它工作了,但是当我按下开始按钮时,它并没有关闭菜单 Window,它只是制作了一个新 window,我该怎么做,保持不变 window,没有 closing/opening 菜单 window,或者我想关闭菜单 window然后打开游戏 window(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.javaclass
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;
}
}
}
编辑:感谢@whiskeyspider 的帮助,我了解到我制作了 2 帧但没有正确引用它们。但是现在我已经解决了这个问题,我的监听器有问题,当我修复这个问题时,我的 Jpanel 将无法与我的监听器一起工作。我已经尝试将 Listener 直接添加到我的 Game JPanel 和我的 MainClass JFrame,但两者都不起作用。
这是我的一些菜单 class,
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;
}
}
您在此处创建了 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());
您需要稍微重新考虑您的设计并确保您引用了正确的(已经存在的)对象。也就是说,当您引用 现有 个对象时,您正在创建 new 个对象。