简体   繁体   English

java - 如何在java swing中的JOptionPane中显示输入文本

[英]How to display input text in JOptionPane in java swing

I'm trying to get some input from the user through JTextFields and when an event happens on any of the textfields, a JOptionPane message dialog containing the components name and message pops up.我试图通过 JTextFields 从用户那里获取一些输入,当任何文本字段上发生事件时,会弹出一个包含组件名称和消息的 JOptionPane 消息对话框。 For example, if user enters 'Hello' in textField1, the JOptionPane should pop up like below例如,如果用户在 textField1 中输入“Hello”,则 JOptionPane 应如下所示弹出

带有文本“testField1:Hello”、“确定”按钮的 JOption 弹出窗口

This is what I have as code ..这就是我的代码..

package testing;
import javax.swing.*;
import java.awt.*;
public class TextFieldFrame extends JFrame{
    
    private JTextField textField1; private JTextField textField2;
    private JTextField textField3; private JPasswordField passwordField;
    
    public TextFieldFrame(){
        setTitle("Testing JTextField and JPasswordField");
        setLayout(new FlowLayout());  
        textField1 = new JTextField(10); add(textField1);
        textField2 = new JTextField("Enter text here"); add(textField2);
        textField3 = new JTextField("Uneditable text field", 21);
        textField3.setEditable(false); add(textField3);
        passwordField = new JPasswordField("Hidden Text"); add(passwordField);
        
        TextFieldHandler handler = new TextFieldHandler();
        
        textField1.addActionListener(handler);
        textField2.addActionListener(handler);
        textField3.addActionListener(handler);
        passwordField.addActionListener(handler);
    }
    
    private class TextFieldHandler extends JOptionPane{
        public void TextFieldHandler(TextFieldHandler handler) {
        String string = ""; // declare string to display
        if(handler.getInputValue() == textField1)
            string = String.format("textField1: %s", textField1.getText());
        else if(handler.getInputValue() == textField2)
            string = String.format("textField2: %s", textField2.getText());
        else if(textField3 == textField3)
            string = String.format("textField3: %s", textField3.getText());
        else if(handler.getInputValue() == passwordField)
            string = String.format("paswordField: %s", passwordField.getPassword());
        JOptionPane.showMessageDialog(null, string);
    }
    }
    
    public static void main(String[] args){
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TextFieldFrame().setVisible(true);
                new TextFieldFrame().setSize(400,100);
            }
        });
    }
}

And I get these errors我得到了这些错误

error: incompatible types: TextFieldFrame.TextFieldHandler cannot be converted to ActionListener
                textField1.addActionListener(handler);
error: incompatible types: TextFieldFrame.TextFieldHandler cannot be converted to ActionListener
            textField2.addActionListener(handler);
error: incompatible types: TextFieldFrame.TextFieldHandler cannot be converted to ActionListener
            textField3.addActionListener(handler);
error: incompatible types: TextFieldFrame.TextFieldHandler cannot be converted to ActionListener
            passwordField.addActionListener(handler);

You can add a listener to your text fields separately.您可以单独向文本字段添加侦听器。 And for each listener, perform a specific action.并为每个侦听器执行特定操作。

DocumentListener is a good choice. DocumentListener是一个不错的选择。 But the problem is that it will call by any change that the user made to the text field.但问题是它会调用用户对文本字段所做的任何更改。 You can not expect it to be called just after typing hello world!你不能指望它在输入 hello world 之后就被调用! It will call the listener for each letter: h, he, hel, hell, ...它会为每个字母调用监听器:h, he, hel, hell, ...

JTextField textField1 = new JTextField();
textField.getDocument().addDocumentListener(new DocumentListener() {
    @Override
    public void removeUpdate(DocumentEvent e) {}

    @Override
    public void insertUpdate(DocumentEvent e) {
        JOptionPane.showMessageDialog( null, String.format("textField1: %s", textField1.getText()));
    }

    @Override
    public void changedUpdate(DocumentEvent e) {}
});

But If I were you, I would use the MouseListener and the mouse exit event.但如果我是你,我会使用MouseListener和鼠标退出事件。 So when the user finishes typing and exits its mouse outside of textField, then JOptionPane will be called.因此,当用户完成输入并在 textField 之外退出鼠标时,将调用JOptionPane

For each of your text fields or password fields, you can add a listener this way:对于每个文本字段或密码字段,您可以通过以下方式添加侦听器:

JTextField textField1 = new JTextField();
textField.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseExited(MouseEvent e) {
        JOptionPane.showMessageDialog( null, String.format("textField1: %s", textField1.getText()));
    }
});

UPDATE更新

As @Abra mentioned in the comment you can also use FocusListener and focusLost event:正如评论中提到的@Abra,您还可以使用FocusListenerfocusLost事件:

textField.addFocusListener(new FocusAdapter() {
    @Override
    public void focusLost(FocusEvent e) {
        JOptionPane.showMessageDialog( null, String.format("textField1: %s", textField1.getText()));
    }
});

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

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