java 向 JTextfield 添加提示文本属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11200585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:12:35  来源:igfitidea点击:

Adding a prompt text property to JTextfield

javaswingnetbeansjtextfield

提问by Jayashri

I am using Netbeans IDE. I want to give the prompt text to JTextfieldin such a way that when user enters the text into JTextFieldit gets cleared and accepts the users input.

我正在使用 Netbeans IDE。我想以JTextfield这样一种方式提供提示文本,当用户输入文本时,JTextField它会被清除并接受用户输入。

采纳答案by David Kroukamp

You can add a simple focus listener to your textfield, and validate the data of the textfield when focus is Lost something like this:

您可以向文本字段添加一个简单的焦点侦听器,并在失去焦点时验证文本字段的数据,如下所示:

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author David
 */
public class Test extends JFrame {

    private JTextField textField, textField2;

    public Test() {
        createAndShowUI();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                Test test = new Test();
            }
        });
    }

    private void createAndShowUI() {
        setTitle("Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        createComponents();
        addComponentsToContentPane();
        addListeners();

        pack();
        setVisible(true);
    }

    private void addComponentsToContentPane() {
        getContentPane().setLayout(new GridLayout(2, 1));

        getContentPane().add(textField);
        getContentPane().add(textField2);
    }

    private void createComponents() {
        textField = new JTextField(10);
        textField2 = new JTextField("Click here to lose focus of above textField");
    }

    private void addListeners() {
        textField.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent fe) {
            }

            @Override
            public void focusLost(FocusEvent fe) {
                if (textField.getText().length() >=1) {
                    JOptionPane.showMessageDialog(null, "You entered valid data");
                    textField.setText("");
                }else {
                    JOptionPane.showMessageDialog(null, "You entered invalid data");
                    textField.grabFocus();//make the textField in foucs again
                }
            }
        });
    }
}

To do this in NetBeans right click on the Component, select Events->Focus->focusLost.

为此,在 NetBeans 中右键单击Component,选择Events-> Focus-> focusLost

回答by user3033626

I don't know what propt-text-fields David Kroukamp already saw, but with the following code I created those textFields who I know ;)

我不知道 David Kroukamp 已经看到了什么 propt-text-fields,但是使用以下代码我创建了我认识的那些 textFields ;)

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JTextField;

public class PTextField extends JTextField {

    public PTextField(final String proptText) {
        super(proptText);
        addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
                if(getText().isEmpty()) {
                    setText(proptText);
                }
            }

            @Override
            public void focusGained(FocusEvent e) {
                if(getText().equals(proptText)) {
                    setText("");
                }
            }
        });

    }

}