【问题标题】:Receive KeyEvents when JTextField is focused当 JTextField 获得焦点时接收 KeyEvents
【发布时间】:2015-10-30 01:23:37
【问题描述】:

我正在开发一款游戏,我有一个 JFrame,它接收 JTextField 中的玩家姓名输入。

我想要的是可以通过按 JButton 或按 ENTER 键来关闭窗口。

当窗口打开时,JTextField 必须有焦点(光标应该出现在组件中)。

我已经看过了:

How do you make key bindings for a java.awt.Frame?

How do you make key binding for a JFrame no matter what JComponent is in focus?

但我还没有解决问题,可能是焦点管理有问题。

我尝试了以下代码:

public class PlayerNameWindow extends JFrame implements KeyListener {

    private String playerName;
    private JLabel backgroundLabel;
    private JLabel enterNameLabel;
    private JButton confirmButton;
    private JTextField nameField;
    private Image background;

    public PlayerNameWindow() {

        initComponents();

    }

    private void initComponents() {

        backgroundLabel = new JLabel();
        enterNameLabel = new JLabel();
        confirmButton = new JButton();
        nameField = new JTextField();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(400, 200));
        setResizable(false);
        getContentPane().setLayout(null);

        addKeyListener(this);

        enterNameLabel.setFont(new Font("Tahoma", 1, 18)); 
        enterNameLabel.setForeground(new Color(255, 255, 255));
        enterNameLabel.setText("Enter your name:");
        getContentPane().add(enterNameLabel);
        enterNameLabel.setBounds(40, 80, 160, 30);

        confirmButton.setText("Confirm");
        confirmButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                confirmButtonMouseClicked(evt);
           }
        });
        confirmButton.setBounds(160, 150, 90, 25);
        getContentPane().add(confirmButton);

        getContentPane().add(nameField);
        nameField.setBounds(220, 80, 140, 30);

        getContentPane().add(backgroundLabel);
        backgroundLabel.setBounds(0, 0, 400, 200);

        pack();
        setLocationRelativeTo(null); 

    }                      

    private void confirmButtonMouseClicked(MouseEvent evt) { 

        confirmAction();

    }         

    private void confirmAction() {

        playerName = nameField.getText();
        System.exit(0);

    }

    public String getPlayerName() {

          return this.playerName;

    }

    public void keyPressed(KeyEvent e) {

         int code = e.getKeyCode();
         if (code == KeyEvent.VK_ENTER)
             System.exit(0);

    }

    public void keyReleased(KeyEvent e) {

           //do-nothing

    }

    public void keyTyped(KeyEvent e) {

          //do-nothing

    }

}

我该怎么办?

谢谢

【问题讨论】:

    标签: java swing jtextfield keylistener


    【解决方案1】:

    将 keyListener 添加到 JTextField。在您的代码中,nameField.addKeyListener(this);

    【讨论】:

    • 当然,这很有意义!我不知道为什么我没想到 xD 抱歉这个愚蠢的问题,谢谢!
    • (1-),@Ingen,您不应该使用 KeyListener。与 AWT 一起使用的 KeyListener。 Swing 有更好的 API 可以使用。您可以将 ActionListener 添加到 JTextField 以处理 Enter 键。这就是 UI 设计的使用方式。不要重新发明轮子。对于其他 KeyStroke,您应该使用 KeyBindings 而不是 KeyListener。
    • 我有一段时间没用过java了,也没见过KeyBindings,所以如果它们在任何方面都比KeyListeners好,那么肯定会继续前进。在使用 ActionListeners 时,我不会用另一个替换一个。使用 Action Listener 来确定是否按下了一个 KEY,然后采取行动是在重新发明轮子,当您可以简单地使用 keyListener 时,这将是编写您自己的 KeyListener。再说一次,KeyBinding 可能更好,我没用过,所以不能说。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2017-03-12
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    相关资源
    最近更新 更多