【问题标题】:with using enter key from the key Board instead of the mouse click using java使用键盘上的 enter 键而不是使用 java 的鼠标单击
【发布时间】:2014-02-25 08:52:20
【问题描述】:

我正在尝试让输入键而不是鼠标单击做出反应。

我不知道如何使用 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();
    }
}

这是输出。

我希望计算支付按钮对输入键做出反应,而不是使用鼠标单击它。

提前谢谢你。

【问题讨论】:

    标签: java swing jframe awt


    【解决方案1】:

    选项 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 的按下。

    【讨论】:

    • 如果我想要另一个键(例如空格键)作为回车键做出反应怎么办?这可能吗?
    • @user3080461 为此,您需要在按钮的组件输入映射中注册一个 keyBinding
    • 满载鳗鱼的气垫船:我还有一个问题。我正在尝试添加一个清除按钮来清除表单。我创建了一个按钮并添加到面板中,但是,我不知道如何使其清晰,或者更确切地说从所有内容中重置表单。有没有办法做到这一点?
    • @user3080461:您具体要清除什么?
    • 满是鳗鱼的气垫船:我想清除所有的文本字段。
    【解决方案2】:

    添加此代码

            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 
            }
    

    【讨论】:

      猜你喜欢
      • 2011-05-25
      • 2015-05-22
      • 1970-01-01
      • 2021-05-12
      • 2015-02-21
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 2017-08-04
      相关资源
      最近更新 更多