简体   繁体   English

在jTextField上执行“触发默认按钮”操作

[英]Fire Default Button on jTextField Action Performed

In my program have a JButton and JTextField . 在我的程序中有一个JButtonJTextField
I want jButton fire while press Enter key in JTextField 我想在按JTextField Enter键时触发jButton
I'm not talking about KeyEvent . 我不是在谈论KeyEvent

private void jTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                            
    this.getRootPane().setDefaultButton(jButton1);
}

This just make JButton focus enabled after enter in JTextField JTextField输入后,这只会使JButton焦点启用
So when I will enter in JTextField it must fire JButton ActionEvent. 因此,当我输入JTextField时,它必须触发JButton ActionEvent。

And have this code on jButton1 ActionPerformed method. 并在jButton1 ActionPerformed方法上具有此代码。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        JOptionPane.showMessageDialog(rootPane, "Hi!");
    } 

So how can I make JButton fired on JTextField ActionEvent ? 那么如何使JButtonJTextField ActionEvent上触发?
I tried following answer but in IDE it throwing exception. 我尝试了以下答案,但在IDE中抛出异常。

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: javax.swing.JButton.addActionListener 

You should NOT call getRootPane().setDefaultButton(jButton1); 您不应调用getRootPane().setDefaultButton(jButton1); inside the ActionListener of text field (actually you don't need an ActionListener for text field). 在文本字段的ActionListener中(实际上,文本字段不需要ActionListener )。

You should call getRootPane().setDefaultButton(jButton1); 您应该调用getRootPane().setDefaultButton(jButton1); in UI initialization. 在UI初始化中。

setDefaultButton(jButton1) is NOT there to "fire" the Enter key event. setDefaultButton(jButton1)此处以“触发” Enter键事件。 It is there to "specify" that you want jButton1 to be the "default" button. 在此可以“指定”您希望jButton1成为“默认”按钮。

See below demonstration. 请参见下面的演示。

import javax.swing.*;
import java.awt.FlowLayout;

public class DefaultButton {

  public static void main(String[] args) {

    JButton button = new JButton("Button 1");
    button.addActionListener(DefaultButton::jButton1ActionPerformed);

    JTextField textField = new JTextField(20);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new FlowLayout());
    frame.getContentPane().add(button);
    frame.getContentPane().add(textField);

    frame.getRootPane().setDefaultButton(button);

    frame.setBounds(300, 200, 400, 300);
    frame.setVisible(true);
  }

  private static void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    JOptionPane.showMessageDialog(null, "Hi!");
  }
}

UPDATE: 更新:

If you don't want to make button the default button and you just want to show the message dialog when user hits Enter key, then you can call the same jButton1ActionPerformed() from an ActionListener of the text field like this: 如果您不想将button为默认按钮,而只想在用户按下Enter键时显示消息对话框,则可以从文本字段的ActionListener调用相同的jButton1ActionPerformed() ,如下所示:

JTextField textField = new JTextField(20);
textField.addActionListener(DefaultButton::jButton1ActionPerformed);

And remove this line: 并删除此行:

frame.getRootPane().setDefaultButton(button);

One way to achieve that (if i got this straight), is to add an ActionListener to the JTextField which will call JButton#doClick() method. 实现此目的的一种方法(如果我明白这一点),是向JTextField添加一个ActionListener ,该方法将调用JButton#doClick()方法。 (I think it is pretty clear what the doClick() method does). (我认为doClick()方法的作用很明显)。

An example: 一个例子:

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FireButtonOnEnter extends JFrame {
    private static final long serialVersionUID = -7346953935931623335L;

    public FireButtonOnEnter() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 400);
        setLocationRelativeTo(null);
        getContentPane().setLayout(new FlowLayout());
        JTextField textField = new JTextField(15);
        JButton button = new JButton("Print Hello");
        button.addActionListener(e -> System.out.println("Hello world."));
        textField.addActionListener(e -> button.doClick());
        getContentPane().add(textField);
        getContentPane().add(button);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new FireButtonOnEnter().setVisible(true));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM