Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用键盘上的enter键代替鼠标,使用java单击_Java_Swing_Jframe_Awt - Fatal编程技术网

使用键盘上的enter键代替鼠标,使用java单击

使用键盘上的enter键代替鼠标,使用java单击,java,swing,jframe,awt,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 { priv

我试图让回车键做出反应,而不是鼠标点击

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

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();
    }
}

我希望CalculatePay按钮对enter键做出反应,而不是用鼠标单击它


提前感谢您。

选项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);

编辑
你在评论中问:

如果我希望另一个键(如空格键)作为enter键进行响应,该怎么办?可能吗

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

助记符的一个例子:

calcButton.setMnemonic(KeyEvent.VK_C);
如果您将其添加到程序中,您将看到按钮文本中的第一个“C”带下划线。您的按钮还将对按下alt-c按钮作出响应。

添加此代码

        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 
        }

如果我希望另一个键(如空格键)作为enter键进行响应,该怎么办?这可能吗?@user3080461为此,您需要在按钮的组件inputmapHovercraft中注册一个键绑定,该组件中充满了鳗鱼:我还有一个问题。我正在尝试添加一个清除按钮来清除表单。我创建了一个按钮并将其添加到面板中,但是,我不知道如何使其清晰,或者更确切地说,从所有内容中重置表单。有办法吗?@user3080461:你具体想清除什么?装满鳗鱼的气垫船:我想清除我所有的文本字段。