JAVA JFrames : Spacebar resets my game

标签 java swing focus jframe cardlayout

所以我制作了一个游戏,我将使用卡片布局在帧之间进行转换。 但是,每次我使用任务栏调整音量然后返回到我的游戏时,如果我按空格键(这是我的启动按钮),游戏将返回到我的第一帧,即主帧。

为什么会出现这个问题?我听说它与“可聚焦”有关,但我不确定这是什么意思。

我的 JFrame 的设置与典型的 JFrame 类似:

public class testAngles {

public static void main(String args[]) throws IOException {
    JFrame frame = new JFrame("Angle Tests");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    movements m = new movements();
    frame.add(m);
    frame.addKeyListener(m);
    frame.setSize(800, 600);
    frame.setVisible(true);
     }
  }

如果有人可以帮忙;我非常感激:]

最佳答案

你看看KeyBindings ,那么很简单并且可能这个,例如(基于3.HFOE的代码,谢谢),剩下的可以是为第一个和最后一个JButtons设置 block 值setEnabled 卡片

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class OnTheFlyImageTest {

    private static final long serialVersionUID = 1L;
    private JPanel cardPanel;
    private CardLayout cardLayout;
    private JFrame frame;

    public OnTheFlyImageTest() {
        JPanel cp = new JPanel(new BorderLayout());
        cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        cardLayout = new CardLayout(5, 5);
        cardPanel = new JPanel(cardLayout);
        cp.add(cardPanel);
        for (int i = 0; i < 5; i++) {// Create random panels for testing.
            String name = "ImagePanel" + (i + 1);
            String image = (i & 1) == 0 ? "foo.gif" : "bar.gif";
            Color clr = (i & 1) == 0 ? Color.red : Color.blue;
            ImagePanel imgPanel = new ImagePanel(name, image, clr);
            cardPanel.add(imgPanel, name);
            cardLayout.addLayoutComponent(imgPanel, name);
        }
        JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 5));
        JButton prevButton = new JButton("< Previous");
        prevButton.setActionCommand("Previous");
        prevButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.previous(cardPanel);
            }
        });
        buttonPanel.add(prevButton);
        JButton nextButton = new JButton("Next >");
        nextButton.setActionCommand("Next");
        nextButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.next(cardPanel);
            }
        });
        buttonPanel.add(nextButton);
        JPanel temp = new JPanel(new BorderLayout());
        temp.add(buttonPanel, BorderLayout.LINE_END);
        cp.add(temp, BorderLayout.SOUTH);
        frame = new JFrame("CardLayout and KeyBindings");
        frame.add(cp);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Test");
        frame.pack();
        frame.setVisible(true);
        setKeyBindings();
    }

    private void setKeyBindings() {
        frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                KeyStroke.getKeyStroke("TAB"), "clickTAB");
        frame.getRootPane().getActionMap().put("clickTAB", new AbstractAction() {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(cardPanel, "ImagePanel" + 1);
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new OnTheFlyImageTest();
            }
        });
    }
}

class ImagePanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private String imgString;
    private JLabel imgLabel;

    public ImagePanel(String name, String imgString, Color backGround) {
        setName(name);
        this.imgString = imgString;
        setLayout(new BorderLayout());
        setBackground((backGround));
        // Ensure size is correct even before any image is loaded.
        setPreferredSize(new Dimension(400, 300));
    }

    @Override
    public void setVisible(boolean visible) {
        if (visible) {
            System.err.println(getName() + ": Loading and adding image");
            ImageIcon icon = new ImageIcon(imgString);
            imgLabel = new JLabel(icon);
            add(imgLabel);
        }
        super.setVisible(visible);
        if (!visible) { // Do after super.setVisible() so image doesn't "disappear".
            System.err.println(getName() + ": Removing image");
            if (imgLabel != null) { // Before display, this will be null
                remove(imgLabel);
                imgLabel = null; // Hint to GC that component/image can be collected.
            }
        }
    }
}

关于JAVA JFrames : Spacebar resets my game,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10963839/

相关文章:

java - 获取页面信息时出现 NullPointerException

java - 如何在 gdb 中调试 jvm.dll 或 java 进程,就像我们调试任何其他可执行文件一样?

java - 显示多种内容类型httpresponse

java - 如何创建具有可调整大小的组件的 jframe

Java定时器和绘图动画

html - CSS 焦点 : Is there a way to unfocus?

javascript - ExtJS 防止 LoadMask 窃取焦点

java - 如何在 JSF 中支持同一种语言的多个区域设置?

java - 如何从另一个窗口将项目添加到 JList

安卓用户界面 : No focus marker when using hardware keyboard/barcode scanner