0

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

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

我的 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);
     }
  }

如果有人可以提供帮助;我非常感谢它:]

4

1 回答 1

3

您已经查看了KeyBindings,然后是简单且可能的,例如(基于 3.HFOE 的代码,谢谢),其余的可能是阻止第一个和最后一个的setEnabledJButtonsCards

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.
            }
        }
    }
}
于 2012-06-09T20:05:27.503 回答