KeyBindings在Swing App中没有响应

时间:2014-01-28 21:39:39

标签: java eclipse swing user-interface key-bindings

所以我试着写这个应用程序基本上是一个谁可以更快地按键的游戏(1代表玩家1,0代表玩家2)。该应用程序有一个简单的GUI和一个简单的记分板,当玩家击中其中一个键时应该更新。 这是GUI的屏幕截图:http://screencloud.net/v/a6vT 没关系开始按钮,只需重置分数,我将在以后实施。

以下是代码文件:

1.GameFrame.java

package MediatorGame;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class GameFrame extends JFrame {

    private GamePanel currentPanel; 

    public GameFrame() {

        currentPanel = new GamePanel();
        setupFrame();
    }

    private void setupFrame(){
        this.setContentPane(currentPanel);
        currentPanel.setFocusable(true);
        currentPanel.requestFocusInWindow();
        this.setSize(450, 300);
    }

}

2.GamePanel.java

package MediatorGame;

import javax.swing.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;



@SuppressWarnings("serial")
public class GamePanel extends JPanel{
    private JButton startButton;
    private SpringLayout currentLayout;
    private JLabel p1Label;
    private JLabel p2Label;
    private int p1Score = 0;
    private int p2Score = 0;
    private JLabel p1ScoreLabel;
    private JLabel p2ScoreLabel;
    private InputMap iMap;
    private ActionMap aMap;


    public GamePanel() {
        startButton = new JButton("Start");
        currentLayout = new SpringLayout();
        p1Label = new JLabel("Player 1");
        p2Label = new JLabel("Player 2");
        p1ScoreLabel = new JLabel(String.valueOf(p1Score));
        p2ScoreLabel = new JLabel(String.valueOf(p2Score));
        iMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        aMap = getActionMap();

        setupPanel();
    }

    private void setupPanel(){  
        setBackground(new Color(255, 255, 204));
        setLayout(currentLayout);           

        currentLayout.putConstraint(SpringLayout.WEST, startButton, 206, SpringLayout.WEST, this);
        currentLayout.putConstraint(SpringLayout.SOUTH, startButton, -40, SpringLayout.SOUTH, this);
        add(startButton);

        currentLayout.putConstraint(SpringLayout.NORTH, p2Label, 50, SpringLayout.NORTH, this);
        currentLayout.putConstraint(SpringLayout.EAST, p2Label, -62, SpringLayout.EAST, this);
        add(p2Label);

        currentLayout.putConstraint(SpringLayout.WEST, p1Label, 75, SpringLayout.WEST, this);
        currentLayout.putConstraint(SpringLayout.NORTH, p1Label, 0, SpringLayout.NORTH, p2Label);
        add(p1Label);

        currentLayout.putConstraint(SpringLayout.WEST, p1ScoreLabel, 0, SpringLayout.WEST, p1Label);
        currentLayout.putConstraint(SpringLayout.NORTH, p1ScoreLabel, 0, SpringLayout.NORTH, p2ScoreLabel);
        add(p1ScoreLabel);

        currentLayout.putConstraint(SpringLayout.NORTH, p2ScoreLabel, 6, SpringLayout.SOUTH, p2Label);
        currentLayout.putConstraint(SpringLayout.WEST, p2ScoreLabel, 0, SpringLayout.WEST, p2Label);
        add(p2ScoreLabel);

        iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), "incP1Score");
        iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_0, 0), "incP2Score");
        aMap.put("incP1Score", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                p1Score += 1;
            }
        });
        aMap.put("incP2Score", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                p2Score += 1;
            }
        });

     }  
}

3.GUIRunner.java

package MediatorGame;

import javax.swing.UIManager;

public class GUIRunner {

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e){ }

        java.awt.EventQueue.invokeLater(new Runnable(){
              public void run() {
                   GameFrame myApp = new GameFrame();
                   myApp.setVisible(true);
              }
        });
    }

}

我查了很多问题。他们中的大多数都将责任归咎于JPanel可关注,并要求关注,错误的keyEvent代码(例如“1”代替“1”或VK_1)或默认getInputMap()方法而不是getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);。我似乎已经处理了所有这些,但是当我按下按键时标签仍然没有更新。

我的问题:出了什么问题?我应该关注标签而不是整个面板吗?错误的键码?我已经尝试了很多假设的解决方案而且不能休息一下!

1 个答案:

答案 0 :(得分:4)

据我所知,标签不更新的原因只是因为你没有更新它们:

@Override
public void actionPerformed(ActionEvent e) {
    p1Score += 1;
    p1ScoreLabel.setText(String.valueOf(p1Score));
}

@Override
public void actionPerformed(ActionEvent e) {
    p2Score += 1;
    p1ScoreLabel.setText(String.valueOf(p2Score));
}