使用Enter键来使用JButton而不是单击鼠标?

时间:2015-03-19 12:33:41

标签: java jframe

如何绑定键盘上的“Enter”键以按下JButton?目前正试图解决这个问题,但不知道该怎么做。

这是我的代码供参考。它应该做的是创建一个猜谜游戏(它确实如此),但我想添加按Enter键的功能,点击“Enter”按钮(在这种情况下为jButtonEnter)。

package day21;
import java.awt.*;
import java.awt.event.*;
//import java.applet.*;
import javax.swing.*;
//import javax.swing.border.*;

public class Day21 extends JApplet implements ActionListener{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    //All important Variables
    JTextArea outTextArea = new JTextArea(10,20);
    JScrollPane scroller = new JScrollPane(outTextArea);
    static int GuessMe = (int) Math.ceil(Math.random()*1000);//randomizes the number
    JPanel jPanelTop = new JPanel();
    JPanel jPanelMid = new JPanel();
    JPanel jPanelLow = new JPanel();
    JLabel jLabelTop = new JLabel("Guess a number between 1 and 1000");
    static JTextField jTextFieldInput = new JTextField("Guess Here",25);
    JButton jButtonEnter = new JButton("Enter");
    JButton jButtonReset = new JButton("Reset");
    JButton jButtonClose = new JButton("Close");

    public void init(){

        this.getContentPane().setBackground(Color.white);
        this.setSize(new Dimension(400, 120));

        //Top Panel
        jPanelTop.setBackground(Color.cyan);
        jPanelTop.setBorder(BorderFactory.createRaisedBevelBorder());
        jPanelTop.setPreferredSize(new Dimension(14, 40));
        jPanelTop.setToolTipText("Top Panel");
        this.getContentPane().add(jPanelTop, BorderLayout.NORTH);
        jPanelTop.add(jLabelTop);

        //Middle Panel
        jPanelMid.setBackground(Color.orange);
        jPanelMid.setBorder(BorderFactory.createRaisedBevelBorder());
        jPanelMid.setPreferredSize(new Dimension(14, 40));
        jPanelMid.setToolTipText("Center Panel");
        this.getContentPane().add(jPanelMid, BorderLayout.CENTER);
        jPanelMid.add(jTextFieldInput);
        jPanelMid.add(jButtonEnter);
        jButtonEnter.addActionListener(this);

        //Lower Panel
        jPanelLow.setBackground(Color.black);
        jPanelLow.setBorder(BorderFactory.createRaisedBevelBorder());
        jPanelLow.setPreferredSize(new Dimension(14, 40));
        jPanelLow.setToolTipText("Lower Panel");
        this.getContentPane().add(jPanelLow, BorderLayout.SOUTH);
        jPanelLow.add(jButtonReset);
        jPanelLow.add(jButtonClose);
        jButtonClose.addActionListener(this);
        jButtonReset.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
        double input = 0;
        boolean Error = false;
        String ErrorMSG = "Error: Please Enter a Number";
        try{
            input = Double.parseDouble(jTextFieldInput.getText().trim());;
        }
        catch(NumberFormatException n){
            Error = true;
        }
        if(Error){
            JOptionPane.showMessageDialog(rootPane, ErrorMSG); //If variable entered is not a number
        }
        String correctAnswer = "Hooray! You guessed Correctly! \n Press Reset to play again";
        String tooHigh = input + " is too high";
        String tooLow = input + " is too low";
        if(!Error){
            if(e.getSource() == jButtonEnter){
                if (input == GuessMe){
                    jPanelLow.setBackground(Color.green);
                    JOptionPane.showMessageDialog(rootPane, correctAnswer);
                }
                if (input > GuessMe){
                    jPanelLow.setBackground(Color.red);
                    JOptionPane.showMessageDialog(rootPane, tooHigh);
                }
                if (input < GuessMe){
                    jPanelLow.setBackground(Color.red);
                    JOptionPane.showMessageDialog(rootPane, tooLow);
                }
            }
        }
        if(e.getSource() == jButtonReset){ 
            Day21.reset(); //runs the reset() method which resets the window back to it's normal state
        }
        if(e.getSource() == jButtonClose){
            System.exit(1); //exits the program
        }
    }

    public static void reset(){

        GuessMe = (int) Math.ceil(Math.random()*1000);//randomizes the number
        jTextFieldInput.setText("Guess Here");
    }
}

2 个答案:

答案 0 :(得分:1)

根据您的具体要求,查看Enter Key and Button有关此主题的讨论和一些解决方案:

  1. 使用根窗格设置默认按钮
  2. 使用UIManager按下按钮
  3. 使用键绑定来调用默认操作

答案 1 :(得分:0)

定义一个方法 - processEnterButton()并将所有必要的逻辑放在那里。如果单击该按钮,则从actionPerformed()调用该方法。还要为ENTER键定义key binding,并从绑定中调用processEnterButton()。