如何在JFrame中使用keyPressed

时间:2017-03-25 10:14:42

标签: java swing jframe keylistener

我做了一个小游戏,希望能够使用按钮"输入"在键盘上运行它,但当我点击进入时没有任何事情发生。 它应该按下按钮点击的功能,但正如我所说,没有任何事情发生。

这是我的代码:

 package com.Rohanzpc.Games;

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.DecimalFormat;

public class RNGF extends JFrame implements KeyListener{

private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField txtDice;
private JTextField textCount;
private JTextField textRN;
private JTextField textIn;
private JTextField textMess;

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

/**
 * Create the frame.
 */
public RNGF() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    txtDice = new JTextField();
    txtDice.setHorizontalAlignment(SwingConstants.CENTER);
    txtDice.setText("Dice Game");
    txtDice.setBorder(null);
    txtDice.setBackground(new Color(240,240,240));
    txtDice.setFont(new Font("Trebuchet MS", Font.PLAIN, 18));
    txtDice.setEditable(false);
    txtDice.setBounds(159, 11, 106, 27);
    contentPane.add(txtDice);
    txtDice.setColumns(10);

    textCount = new JTextField();
    textCount.setEditable(false);
    textCount.setBounds(20, 80, 100, 30);
    contentPane.add(textCount);
    textCount.setColumns(10);
    textCount.setText("3");

    textRN = new JTextField();
    textRN.setEditable(false);
    textRN.setBounds(314, 80, 100, 30);
    contentPane.add(textRN);
    textRN.setColumns(10);

    textIn = new JTextField();
    textIn.setBounds(159, 143, 106, 27);
    contentPane.add(textIn);
    textIn.setColumns(10);

    JButton btnEnter = new JButton("BET");
    btnEnter.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
    btnEnter.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent enter) {

            double rn = Math.random() * 10;
            DecimalFormat rnFormat = new DecimalFormat("#.0000");
            textRN.setText(rnFormat.format(rn));
            //Random Number

            String InS = textIn.getText();
            double in;
            in = Double.parseDouble(InS);
            if(in > 6) {
                in  = 6;
            }
            if(in < 0) {
                in = 0;
            }
            //Input

            double credit = 6 - in;

            String CoS = textCount.getText();
            double count;
            count = Double.parseDouble(CoS);

            if(in > rn) {

                textMess.setText("You won!");
                 count += credit;

                 String countT = Double.toString(count);                     
                 textCount.setText(countT);

            }
            else {

                count -= 1;

                textMess.setText("You lost!");

                String countT = Double.toString(count);
                textCount.setText(countT);

            }

            if(count <= 0) {

                textCount.setText("0");
                textMess.setText("Game over!");
                textIn.setEditable(false);


            }

            System.out.println(rn);



        }
    });
    btnEnter.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            double rn = Math.random() * 10;
            DecimalFormat rnFormat = new DecimalFormat("#.0000");
            textRN.setText(rnFormat.format(rn));
            //Random Number

            String InS = textIn.getText();
            double in;
            in = Double.parseDouble(InS);
            if(in > 6) {
                in  = 6;
            }
            if(in < 0) {
                in = 0;
            }
            //Input

            double credit = 6 - in;

            String CoS = textCount.getText();
            double count;
            count = Double.parseDouble(CoS);

            if(in > rn) {

                textMess.setText("You won!");
                 count += credit;

                 String countT = Double.toString(count);                     
                 textCount.setText(countT);

            }
            else {

                count -= 1;

                textMess.setText("You lost!");

                String countT = Double.toString(count);
                textCount.setText(countT);

            }

            if(count <= 0) {

                textCount.setText("0");
                textMess.setText("Game over!");
                textIn.setEditable(false);


            }

            System.out.println(rn);

        }
    });
    btnEnter.setBounds(166, 181, 90, 23);
    contentPane.add(btnEnter);

    textMess = new JTextField();
    textMess.setEditable(false);
    textMess.setFont(new Font("Tahoma", Font.PLAIN, 13));
    textMess.setBounds(146, 71, 136, 39);
    contentPane.add(textMess);
    textMess.setColumns(10);
}

@Override
public void keyPressed(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}
}

2 个答案:

答案 0 :(得分:0)

为框架设置JButton默认按钮的最简单方法是使用:

getRootPane().setDefaultButton( btnEnter );

现在按下Enter键将调用按钮,无论它是否有焦点。

答案 1 :(得分:-1)

你可以试试这个

getContentPane().registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {//instructions}

 }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);