繁体   English   中英

如何在 JTextField 中模拟鼠标单击?

[英]How to simulate a mouse click in a JTextField?

我一直在练习创建一个简单的注册表单 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");
}
}
} 

我仍然是编程的初学者,如果您发现我的代码有点太长,我深表歉意,但非常感谢您的帮助!

有些人(包括我自己)称这种东西为Field Watermark 您需要在包含在focusGainedfocusLost事件中的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);  // ****

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM