JTextField添加灰色的文本,在焦点

时间:2018-04-06 08:52:28

标签: java swing

我希望我创建的窗口有2个TextFields,用于输入用户名和密码。如果我创建我的登录窗口,我希望这两个textFields在那些textFields中有一些灰色的文本,以便在哪里放置用户名和密码。这可能吗?只是没有在网络上找到任何好的答案,我不想用focusGained和focusLost事件做“蹩脚”的方式。

它应该看起来像这样参考: Reference picture

代码在这里:

package com.sociuspugnae.login;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Font;
import javax.swing.border.TitledBorder;
import javax.swing.JTextField;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LoginWindow extends JFrame {

    private JPanel contentPane;
    private JTextField textFieldUsername;
    private JTextField textFieldPassword;

    /**
     * Create the frame.
     */
    public LoginWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 250);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel labelName = new JLabel(Main.name);
        labelName.setToolTipText("the program's name");
        labelName.setFont(new Font("Tahoma", Font.PLAIN, 20));
        labelName.setHorizontalAlignment(SwingConstants.CENTER);
        labelName.setBorder(new TitledBorder(null, "v " + Main.version, TitledBorder.LEADING, TitledBorder.TOP, null, null));

        textFieldUsername = new JTextField();
        textFieldUsername.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if(textFieldUsername.getText().isEmpty()) {
                    textFieldUsername.setText("Username");
                }
            }
            @Override
            public void focusGained(FocusEvent e) {
                if(textFieldUsername.getText().equals("Username")) {
                    textFieldUsername.setText("");
                }
            }
        });
        textFieldPassword = new JTextField();
        textFieldPassword.setColumns(10);

        JButton buttonLogin = new JButton("Login");
        buttonLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(textFieldUsername.getText().equals("Admin") && textFieldPassword.getText().equals("123")) {
                    JOptionPane.showMessageDialog(contentPane, "Login successful!");
                }


            }
        });
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.TRAILING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(7)
                    .addComponent(labelName, GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                    .addGap(7))
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(textFieldUsername, GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                    .addGap(50)
                    .addComponent(textFieldPassword, GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                    .addContainerGap())
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(150)
                    .addComponent(buttonLogin, GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                    .addGap(150))
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(7)
                    .addComponent(labelName, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                    .addGap(18)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addComponent(textFieldPassword, GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                        .addComponent(textFieldUsername, GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
                    .addGap(29)
                    .addComponent(buttonLogin, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE))
        );
        contentPane.setLayout(gl_contentPane);
    }
}

1 个答案:

答案 0 :(得分:2)

您可以覆盖JTextField的paintComponent方法,如下所示:

public class MyTextField extends JTextField {
.
.
@Override
public void paintComponent(Graphics g) {
  super.paintComponent(g);
  if (this.getText().equals("")){
     g drawString( ...);
  }
}

}