使用来自键盘的输入键而不是使用java的鼠标单击

时间:2014-02-01 16:49:57

标签: java swing jframe awt

我试图让输入键反应而不是鼠标点击。

我不知道如何使用java。

这是代码和输出。

import java.util.Random;
import javax.swing.*;
import java.util.Arrays;
import java.text.DecimalFormat;
import java.awt.event.*;        
import java.awt.*;

public class PayRate extends JFrame
{
    private JPanel panel;       
    private JLabel rateLabel;
    private JLabel hoursLabel;
    private JLabel payLabel;
    private JTextField rateTextField; 
    private JTextField hoursTextField;
    private JTextField payTextField;  
    private JButton calcButton;
    private JButton clearButton;
    private final int WINDOW_WIDTH = 350;
    private final int WINDOW_HEIGHT = 160;

    public PayRate()
    {
        setTitle("PAY RATE");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setVisible(true);
    }

    private void buildPanel()
    {
        rateLabel = new JLabel("RATE");
        hoursLabel = new JLabel("HOUR");
        payLabel = new JLabel("");
        rateTextField = new JTextField(8);
        hoursTextField = new JTextField(8);
        payTextField = new JTextField(27);
        calcButton = new JButton("CALCULATE PAY");
       clearButton = new JButton("   CLEAR   ");
        calcButton.addActionListener(new CalcButtonListener());
      clearButton.addActionListener(new clearButtonListener());
      getRootPane().setDefaultButton(calcButton);    // make the enter key react instead of mouse click
      //calcButton.setMnemonic(KeyEvent.VK_E);       // make  (ALT + E) response as an enter key

        panel = new JPanel();

        payTextField.setBackground(Color.ORANGE);
        rateTextField.setBackground(Color.LIGHT_GRAY); // Set the Background of rateTextField to LIGHT_GRAY
        hoursTextField.setBackground(Color.LIGHT_GRAY);// Set the Background of hoursTextField to LIGHT_GRAY
        calcButton.setBackground(Color.GREEN); // Set the background of CalcButton to GREEN
       rateLabel.setForeground(Color.BLUE);   // set the Foreground of rate label to blue
       hoursLabel.setForeground(Color.BLUE); // set the Foreground of hours label to blue
       payLabel.setForeground(Color.BLUE); // set the Foreground of pay label to blue
        panel.setBackground(Color.PINK);// Set the background of the panel to yellow
        panel.add(rateLabel);           // Add rate label to the panel
        panel.add(rateTextField);   // add rate text field to the panel
        panel.add(hoursLabel);     // add hour label to the panel
        panel.add(hoursTextField); // add hours text field to the panel
        panel.add(calcButton);      // add calculate button to the panel 
        panel.add(payLabel);      // add the pay label to the panel
        panel.add(payTextField); // add pay text field to the panel
            panel.add(clearButton);



    }
    private class CalcButtonListener implements ActionListener
   {

      public void actionPerformed(ActionEvent e)
      {
         double rt ;
         String input;  
            String display ="";
            String output = "    Your total pay for this week is: ";
         double hrs;  
            double sum = 0;
            DecimalFormat formatter = new DecimalFormat("#0.00");

         input = rateTextField.getText();
            rt = Double.parseDouble(input);


         input = hoursTextField.getText();
            hrs = Double.parseDouble(input);

            sum = hrs * rt;


         display = display + output.toUpperCase() + formatter.format(sum);
            payTextField.setText(display);


      }


   }

   private class clearButtonListener implements ActionListener
   {

      public void actionPerformed(ActionEvent e)
      {
         payTextField.setText("");
         hoursTextField.setText("");
         rateTextField.setText("");

      }
   }

    public static void main(String[] args)
    {
        new PayRate();
    }
}

这是输出。

enter image description here

我希望计算付款按钮对输入密钥做出反应,而不是使用鼠标点击它。

提前谢谢你。

2 个答案:

答案 0 :(得分:4)

选项1:使感兴趣的JButton成为JFrame的JRootPane的默认按钮:

  calcButton = new JButton("CALCULATE PAY");
  calcButton.addActionListener(new CalcButtonListener());

  getRootPane().setDefaultButton(calcButton);  // **** add this line ****

选项2:将相同的ActionListener添加到JTextFields:

  CalcButtonListener calcListener = new CalcButtonListener();
  calcButton.addActionListener(calcListener);
  rateTextField.addActionListener(calcListener);
  payTextField.addActionListener(calcListener);

修改
你在评论中提问:

  

如果我想要另一个键(例如空格键)作为回车键做出反应怎么办?那可能吗?

答案:
如果按钮具有焦点,则JButton已经连线以响应空格键按下。否则,1)设置按钮的助记符以响应alt键组合,或2)使用键绑定将按钮绑定到任何键或组合键。

助记符的一个例子:

calcButton.setMnemonic(KeyEvent.VK_C);

如果将其添加到程序中,您会看到按钮文本中的第一个“C”带有下划线。您的按钮也会响应alt-c按下。

答案 1 :(得分:1)

添加此代码

        public PayRate(){
            setTitle("PAY RATE");
            setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            buildPanel();
            add(panel);
            setVisible(true);
            getRootPane().setDefaultButton(calcButton);// here it is 
        }