- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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/
我正在尝试使用 NetBeans 在 Java 中制作类似幻灯片的应用程序。 我有一个 JFrame(主窗口),里面有两个用于导航的按钮(后退和下一步),还有一个 JPanel(mainPanel),
新代码 package test; import javax.swing.*; import java.awt.*; public class TestWindow extends JFrame{ /
我是 Java swing 编码新手。我正在尝试将 JPanel 内容复制到一个新的 JPanel,它使用原始 JPanel 的内容来显示。此外,原始 JPanel 内容随着记录的变化而变化。我尝试了
我正在尝试创建一个带有另外两个 JPanel 的 JPanel,但是,当时,当我要显示主要内容时JPanel,它只显示主Jpanel上的第一个Jpanel。 通过以下示例,您将更好地理解这个问题: 我
我正在尝试学习如何使用编码风格在 Java 中完成 GUI 的工作这就是我写的: import java.awt.Container; import java.awt.Panel; import ja
我从 Oracle 教程中得到了一个 JPanel 的例子我看到它使用默认方法关闭窗口 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 我想
我在使用 Java JPanel 时遇到问题。我想将 2 个具有不同布局的 JPanel 放入一个也有布局的 JPanel 中。有可能让它发挥作用吗? BibP() setLayout(new
我试图让一个 JPanel 出现在另一个 JPanel 中。目前,JPanel 位于外部 JFrame 中,并与我的其他 JFrame 一起加载。我希望 JPanel 位于另一个 JPanel 内部,
是否可以在 Java 的一个方法中在一个面板中包含一个面板? 就像,如果我在名为 CreatePanel 的方法中创建了一个 JPanel,我可以在其下面添加另一个吗?我正在尝试在一种方法中添加两个或
我对嵌套的 BoxLayouts 有疑问。 我想构建一个由 2 个子面板组成的 DropDownPanel:顶部的标题和底部的主体。 body 最初是隐藏的。通过单击标题,您可以切换正文的可见性并显示
我需要创建一个在 JFrame 或 JPanel 上显示多个矩形的程序。这是我到目前为止提出的代码: import javax.swing.*; import java.util.Random; im
将站点中的一些图像保存到 ArrayList 后,我试图创建一个 jpanel,它将在带有滚动 Pane 的单独 jpanel 中显示所有这些图像,以便我可以向每个图像添加 Action 事件。然
我正在开发一个简单的注册窗口,它会在 Java 应用程序打开时出现。 这是一个 JFrame,里面有一个 JPanel,它有文本字段、标签,另一个面板也包含文本字段和标签。我的问题是外部面板有背景图像
我有这个界面要创建。我有 JScrollPane 的问题: 我声明了一个带有 Gridlayout(8,1,0,2) 的 JPanel,我希望在这个面板中出现 8 行。 一行是一个JPanel,我设置
我已经创建了 JFreeChart 并将其放入图表面板(按照建议)。我还将它添加到 jPanel 中。我正在使用 jFrame。但是运行程序后我的图表不可见。有谁能帮帮我吗? final JFreeC
我在下面附上了我的代码,我的程序的屏幕截图,以及我希望标签看起来像的图形。我需要我的 JPanel textPanel 出现在 LLP 选项卡上的 JButton 下方。我试图将 textPanel
我想从窗口 (JFrame) 中删除旧的 JPanel 并添加一个新的。我该怎么做呢? 我尝试了以下方法: public static void showGUI() { JFrame fram
我刚刚接触 Java,正在为我的大学类(class)开发一个项目。我正在开发一款《百万富翁》游戏,但我陷入困境。 我有一个 JFrame 类,其中有 2 个面板。第一个是由按钮组成的,第二个是我想通过
所以,我正在制作一个绘画程序,我有一个主要的Paint类,它检测鼠标输入和绘画,还有一个Tools类,它是左侧的工具栏,拥有许多工具,例如画笔大小更改和形状更改。因此,我想向 Tools 类添加一个清
我正在尝试制作一个在 JToggleButton 的帮助下激活的弹出面板。我希望在选择 ToggleButton 时将 JPanel 添加到另一个 Jpanel 上,并在取消选择 ToggleButt
我是一名优秀的程序员,十分优秀!