如何通过按“Enter”键来制作jbutton触发器?

时间:2016-04-01 02:00:19

标签: jframe jbutton keypress keylistener keyevent

您好我已经构建了一个简单的GUI游戏来猜测0-100之间的数字,我有一个按钮,用户点击该按钮来检查数字。我希望用户能够按下键进入并执行按钮的操作。我一直在寻找解决方案,但似乎找不到我的问题的答案:/ 提前谢谢你

package org.eclipse.wb.swt;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class JGuessinggame {

private JFrame frame;
private JTextField textField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                JGuessinggame window = new JGuessinggame();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public JGuessinggame() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 15));
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel lblWwe = new JLabel("Welcome to the Magic Number Guessing game");
    lblWwe.setFont(new Font("Segoe UI", Font.PLAIN, 15));
    lblWwe.setBounds(56, 11, 333, 44);
    frame.getContentPane().add(lblWwe);

    JLabel lblGuessANumber = new JLabel("Guess a number between 0 - 100");
    lblGuessANumber.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblGuessANumber.setBounds(100, 66, 215, 14);
    frame.getContentPane().add(lblGuessANumber);

    textField = new JTextField();
    textField.addKeyListener(new KeyAdapter() {





    });
    textField.setBounds(159, 128, 86, 20);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    JLabel lblAnswer = new JLabel("");
    lblAnswer.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblAnswer.setBounds(100, 174, 324, 29);
    frame.getContentPane().add(lblAnswer);


    Random ran = new Random();
    int number;
    number = (ran.nextInt(100)+1);


    JButton btnCheck = new JButton("Check!");
    btnCheck.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        try{

            int guess = Integer.parseInt(textField.getText());

            if(number>guess)
                lblAnswer.setText("try higher!");   
            else if(number<guess)
                lblAnswer.setText("try lower!");
            else if(number==guess)

                lblAnswer.setText("you guess correctly! , the magic number was "+number);

            }catch(Exception exc){
            exc.printStackTrace();  
            }   






        }
    });
    btnCheck.setBounds(159, 214, 89, 23);
    frame.getContentPane().add(btnCheck);


}

}

0 个答案:

没有答案