我一直在练习创建一个简单的注册表单 GUI,最近我发现了 Focus Listener。我已经在我的程序中实现了它,以便进一步完善它,并以某种方式让它工作。虽然它确实按照我的预期工作,但我的下一个目标是不让用户在 JTextField 中自动输入,而是希望他们能够按照自己的意愿手动点击进出文本字段。问题是我很难思考如何实现这一点。
目前这是我的代码:
package com.main;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class RegisterScreen extends JFrame implements ActionListener, FocusListener{
JLabel lblCreateUser, lblCreatePassword, lblHeader;
JTextField txtNewUsername, txtNewPassword;
JButton btnCreateAccount, btnReturnBack;
public RegisterScreen(){
super("Create new User");
setLayout(null);
lblCreateUser = new JLabel("Please enter your Email Address");
lblCreatePassword = new JLabel("Please enter your Password");
lblHeader = new JLabel("Create new account");
txtNewUsername = new JTextField("Username");
txtNewPassword = new JTextField("Password");
btnCreateAccount = new JButton("Create account");
btnReturnBack = new JButton("Back");
lblHeader.setBounds(130, 50, 300,40);
lblCreateUser.setBounds(50,115, 300,40);
lblCreatePassword.setBounds(50, 200, 300, 40);
txtNewUsername.setBounds(50, 150, 300, 40);
txtNewPassword.setBounds(50, 230, 300, 40);
btnCreateAccount.setBounds(50, 280,125, 40);
btnReturnBack.setBounds(223, 280,125, 40);
btnReturnBack.addActionListener(this);
btnCreateAccount.addActionListener(this);
txtNewUsername.addFocusListener(this);
txtNewPassword.addFocusListener(this);
add(lblHeader);
add(lblCreateUser);
add(txtNewUsername);
add(lblCreatePassword);
add(txtNewPassword);
add(btnCreateAccount);
add(btnReturnBack);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent exit){
System.exit(0);
}
});
setSize(400,600);
setVisible(true);
setResizable(false);
}
public static void main (String []args){
RegisterScreen register = new RegisterScreen();
}
public void actionPerformed(ActionEvent e) {
File user = new File("Usernames.txt");
File pass = new File("Passwords.txt");
if (e.getSource() == btnCreateAccount) {
try (BufferedWriter addUser = new BufferedWriter(new FileWriter(user, true)); BufferedWriter addPass = new BufferedWriter(new FileWriter(pass, true))) {
if(txtNewUsername.getText().isEmpty() && txtNewPassword.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "Username or Password must not be blank", "Error", 0);
}
else{
addUser.write(txtNewUsername.getText());
addUser.newLine();
addPass.write(txtNewPassword.getText());
addPass.newLine();
JOptionPane.showMessageDialog(null, "Account Successfully Created", "Success", JOptionPane.INFORMATION_MESSAGE);
}
}
catch (IOException exp) {
JOptionPane.showMessageDialog(this, "Account creation failed", "Error", JOptionPane.ERROR_MESSAGE);
}
}
else if(e.getSource() == btnReturnBack){
LoginScreen login = new LoginScreen();
dispose();
}
}
public void focusGained(FocusEvent e) {
if (e.getSource() == txtNewUsername){
txtNewUsername.setText("");
}
if (e.getSource() == txtNewPassword){
txtNewPassword.setText("");
}
}
public void focusLost(FocusEvent e) {
if (e.getSource() == txtNewUsername){
txtNewUsername.setText("Username");
}
if (e.getSource() == txtNewPassword){
txtNewPassword.setText("Password");
}
}
}
我仍然是编程的初学者,如果您发现我的代码有点太长,我很抱歉,但非常感谢任何帮助!
回答1
有些人(包括我自己)称这种东西为字段水印。您需要在 focusGained
和 focusLost
events 中包含的 if
语句中添加一些条件,例如:
@Override
public void focusGained(FocusEvent e) {
if (e.getSource() == txtNewUsername && txtNewUsername.getText().equals("Username")) {
txtNewUsername.setForeground(Color.black);
txtNewUsername.setText("");
}
else if (e.getSource() == txtNewPassword && txtNewPassword.getText().equals("Password")) {
txtNewPassword.setForeground(Color.black);
txtNewPassword.setText("");
}
}
@Override
public void focusLost(FocusEvent e) {
if (e.getSource() == txtNewUsername && txtNewUsername.getText().trim().isEmpty()) {
txtNewUsername.setForeground(Color.lightGray);
txtNewUsername.setText("Username");
}
else if (e.getSource() == txtNewPassword && txtNewPassword.getText().trim().isEmpty()) {
txtNewPassword.setForeground(Color.lightGray);
txtNewPassword.setText("Password");
}
}
在您的组件初始化期间:
txtNewUsername = new JTextField("Username");
txtNewUsername.setForeground(Color.lightGray); // ****
txtNewPassword = new JTextField("Password");
txtNewPassword.setForeground(Color.lightGray); // ****