java JTextField:更改前景色并保存内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16811477/
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-11-01 00:03:30  来源:igfitidea点击:

JTextField: Change the foreground color and save the content

javaswingjtextfieldforeground

提问by user2144555

I have a frame with some JTextFieldsfor the user insert some values. When the window is opened, the text fields have written, in gray, what the user should write in that container, like "value in seconds"...

我有一个框架,其中有一些JTextFields供用户插入一些值。当窗口打开时,文本字段以灰色写入用户应该在该容器中写入的内容,例如“以秒为单位的值”...

I want to change the color of those letters (I think it is the foreground) to dark when the user starts to write in the JTextFields, and save to a Stringwhat is written by the user.

我想这些信(我觉得是前景)的颜色改为深色,当用户开始写的JTextFields,并保存到String什么是由用户编写。

回答by Daniel Lerps

For the colour change you have to implement a FocusListenerwhich sets the foreground with setForeground(). If you want to have a String of the current content of the JTextFieldyou can achieve this with a DocumentListenerto the underlying Document.

对于颜色更改,您必须实现一个FocusListener设置前景的setForeground(). 如果你想拥有一个当前内容的字符串,JTextField你可以使用DocumentListener底层的Document.

See this code as an example (I use blue and red for the colour and store the Text value of tfin the Stringcontent):

请参阅此代码作为示例(我使用蓝色和红色作为颜色并将文本值存储tfString内容中):

JTextField tf = new JTextFiedl();
tf.addFocusListener(new FocusListener()
{
    @Override
    public void focusGained(FocusEvent fe)
    {
        tf.setForeground(INACTIVE_COLOUR);
    }

    @Override
    public void focusLost(FocusEvent fe)
    {
        tf.setForeground(ACTIVE_COLOUR);
    }
});

A full working example is here:

一个完整的工作示例在这里:

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

public class TF
{
    private final Color ACTIVE_COLOUR = Color.BLUE;
    private final Color INACTIVE_COLOUR = Color.RED;

    private String content; //text of the text field is stored here

    private JTextField tf;
    private JTextField lbl;

    public TF()
    {
        JFrame mainFrame = new JFrame("Window");

        tf = new JTextField("Hint");
        lbl = new JTextField("click here to change focus");

        tf.setForeground(ACTIVE_COLOUR);

        setListeners();

        mainFrame.add(tf, BorderLayout.NORTH);
        mainFrame.add(lbl, BorderLayout.SOUTH);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    private void setListeners()
    {
        tf.addFocusListener(new FocusListener()
        {
            @Override
            public void focusGained(FocusEvent fe)
            {
                tf.setForeground(INACTIVE_COLOUR);
            }

            @Override
            public void focusLost(FocusEvent fe)
            {
                tf.setForeground(ACTIVE_COLOUR);
            }
        });

        tf.getDocument().addDocumentListener(new DocumentListener()
        {
            @Override
            public void removeUpdate(DocumentEvent de)
            {
                content = tf.getText();
            }

            @Override
            public void insertUpdate(DocumentEvent de)
            {
                content = tf.getText();
            }

            @Override
            public void changedUpdate(DocumentEvent de)
            {
                content = tf.getText();
            }
        });
    }

    public static void main(String[] args)
    {
        TF tf = new TF();
    }
}

回答by camickr

See Text Promptfor another approach.

另一种方法请参见文本提示