gpt4 book ai didi

java - 将 JPanel 添加到另一个 JPanel 时出现问题

转载 作者:行者123 更新时间:2023-12-01 12:52:07 25 4
gpt4 key购买 nike

我正在制作一个游戏,并且我拥有它,因此当我按下我制作的菜单上的按钮时,它会加载游戏。游戏和菜单都是JPanel。问题是,当我单击按钮时,它会加载 JPanel,但键盘输入不起作用。为什么会发生这种情况?这是我的代码:

public class Menu extends JPanel  {
static boolean load = false;
JButton play = new JButton("play");
GamePanel panel = new GamePanel();

public Menu(){
setLayout(new FlowLayout());
add(play);


setVisible(true);
play.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
Runner.panel.setPreferredSize(new Dimension(570,700));
add(Runner.panel,0,0);
//repaint();
validate();
}
});
}

public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
draw(g2d);

}

public void draw(Graphics2D g2d){

g2d.drawImage(getBulletImage(),0,0,null);

}



// gets the image file for the player class
public Image getBulletImage(){
ImageIcon ic = new ImageIcon("Menu.png");
return ic.getImage();
}

}

单击该按钮时,它会在当前 JPanel 顶部添加一个 JPanel

最佳答案

阅读这篇文章JPanel not responding to keylistener这是在同一上下文中提出的问题。

<小时/>

一些要点:

关于java - 将 JPanel 添加到另一个 JPanel 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24144880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com