将提示文本属性添加到JTextfield

时间:2012-06-26 03:56:28

标签: java swing netbeans jtextfield

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

2 个答案:

答案 0 :(得分:3)

您可以在文本字段中添加一个简单的焦点侦听器,并在焦点丢失时验证文本字段的数据:

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

要在NetBeans中执行此操作,请右键单击Component,选择事件 - > 焦点 - > focusLost

答案 1 :(得分:3)

我不知道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("");
                }
            }
        });

    }

}