gpt4 book ai didi

java - 通过 Button Action Listener 添加游戏的 JPanel(使用 KeyListener)会禁用 keylistener

转载 作者:行者123 更新时间:2023-11-29 09:08:43 24 4
gpt4 key购买 nike

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class Game extends JPanel{
private static JFrame primary= new JFrame("Game");
private JButton x1;

public Game(){
x1= new JButton("YES");
x1.addActionListener(new LevelChoice(1));
add(x1);
}

public static void setScreen(JPanel jp){


//primary.removeAll();
//System.out.println("hi");
//primary.revalidate();
//primary.repaint();

}

public static void main(String[] args){

primary.setPreferredSize(new Dimension(1000, 700));
/*primary.add(new LevelHUD("xxxxxxxxxxxxxxxxxxxx" +
"xoooooooooooooooooox" +
"xoooooooooooooooooox" +
"xooomoooooooooooooox" +
"xoooooooooxoooooooox" +
"xoooooooooxooooomoox" +
"xoooommoooxxxxooooox" +
"xoooomooooooooooooox" +
"xoooomooomooooooooox" +
"xooomooooolooomoooox" +
"xoooomcoooooooooooox" +
"xooomococoooooooooox" +
"xooomocoooloooooooox" +
"xgoomocoooooooooooox" +
"xcooooogooooooooooox" +
"xocooooocoooooooooox" +
"xoococooolooogooooox" +
"xoooooooooooooooooox" +
"xxxxxxxxxxxxxxxxxxxx"));*/

primary.add(new Game());



primary.setResizable(false);
primary.setVisible(true);
primary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
primary.pack();
}

private class LevelChoice implements ActionListener{

private int level;

public LevelChoice(int i){
level=i;
}

public void actionPerformed(ActionEvent e) {
//Game.setScreen(new LevelHUD(gamelevel1));
primary.add(new LevelHUD("xxxxxxxxxxxxxxxxxxxx" +
"xoooooooooooooooooox" +
"xoooooooooooooooooox" +
"xooomoooooooooooooox" +
"xoooooooooxoooooooox" +
"xoooooooooxooooomoox" +
"xoooommoooxxxxooooox" +
"xoooomooooooooooooox" +
"xoooomooomooooooooox" +
"xooomooooolooomoooox" +
"xoooomcoooooooooooox" +
"xooomococoooooooooox" +
"xooomocoooloooooooox" +
"xgoomocoooooooooooox" +
"xcooooogooooooooooox" +
"xocooooocoooooooooox" +
"xoococooolooogooooox" +
"xoooooooooooooooooox" +
"xxxxxxxxxxxxxxxxxxxx"));
revalidate();

}

}


}

好吧,我有一个大约有 12 个类 atm 的游戏,但我不会详细介绍 - 我的游戏涉及玩家通过按键监听器使用箭头键的移动。这是我的主要类(class) - 现在发生的是这个。我正在努力做到这一点,以便一个人可以选择他们想要玩的级别 - A 级或 B 级。然后我添加一个包含该游戏级别的 JPanel 供用户玩 - 如果我添加游戏级别主要方法,一切正常!

但是,当我单击按钮然后添加 JPanel 时,玩家无法移动,仅此而已 - 然而,关卡中的所有怪物都运行良好。

有什么想法吗? ButtonListener 是否覆盖 KeyListener 之类的?顺便说一句,setFocusable 已经在游戏级别面板类中设置为 true,所以我怀疑这是个问题

最佳答案

KeyListeners 要求他们正在收听的组件具有焦点,当您单击按钮时,焦点会改变并且键不再“发送”到您的组件。

你有两个选择。更改您的代码以使用 Key Bindings (首选)或者在你想要接收按键事件的面板上调用requestFocusInWindow

更新了例子

演示核心问题的简单示例。焦点必须返回到具有 KeyListener 的组件,它不足以聚焦它的子组件或父组件。

请确保焦点请求是在EDT内进行的,否则有可能被盗

public class TestKeyListener {

public static void main(String[] args) {
new TestKeyListener();
}

public TestKeyListener() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
KeyPane keyPane = new KeyPane();
frame.add(keyPane);
frame.add(new ButtonPane(keyPane), BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class KeyPane extends JPanel {

private JLabel keyed;
public KeyPane() {

setLayout(new GridBagLayout());
keyed = new JLabel("...");
add(keyed);
setFocusable(true);
requestFocusInWindow();

addKeyListener(new KeyAdapter() {

@Override
public void keyPressed(KeyEvent e) {
keyed.setText(KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers()).toString());
}

});

addFocusListener(new FocusListener() {

@Override
public void focusGained(FocusEvent e) {
keyed.setText("Focus gained");
}

@Override
public void focusLost(FocusEvent e) {
keyed.setText("Focus lost");
}

});

}

}

public class ButtonPane extends JPanel {

public ButtonPane(final Component focus) {
setLayout(new GridBagLayout());
JButton bad = new JButton("Steal Focus");
JButton good = new JButton("Return Focus");
good.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
focus.requestFocusInWindow();
}
});

add(good);
add(bad);
}

}

}

关于java - 通过 Button Action Listener 添加游戏的 JPanel(使用 KeyListener)会禁用 keylistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13486379/

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