是否可以创建一个密码字段,您可以在其中获取java中的文本?现在我尝试了像
这样的东西 Password = new JPasswordField();
Password.setBounds(231, 134, 131, 23);
frame.getContentPane().add(Password);
Password.getText();
但它不会起作用,因为密码是"私有"。
这是我目前的代码
package Bank;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
public class BankLogin {
String usernameuser;
String passworduser;
ArrayList<String> Usernames = new ArrayList<String>();
ArrayList<String> Passwords = new ArrayList<String>();
private JFrame frame;
private JTextField Username;
private JPasswordField passwordField;
private JPasswordField Password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BankLogin window = new BankLogin();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public BankLogin() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 508, 381);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPasswordField Password = new JPasswordField();
Password.setBounds(231, 134, 131, 23);
frame.getContentPane().add(Password);
Username = new JTextField();
Username.setBounds(231, 73, 131, 23);
frame.getContentPane().add(Username);
Username.setColumns(10);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(109, 66, 112, 36);
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 20));
frame.getContentPane().add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(109, 125, 112, 36);
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 20));
frame.getContentPane().add(lblPassword);
JButton btnLogin = new JButton("Login");
btnLogin.setBounds(109, 240, 89, 23);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
frame.getContentPane().add(btnLogin);
JButton btnRegister = new JButton("Register");
btnRegister.setBounds(273, 240, 89, 23);
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if((Username.getText()).length() != 0 && (Password.getPassword()).length !=0){
JOptionPane.showMessageDialog(frame, "A basic JOptionPane messagessdfasdf dialog");
}
else{
Usernames.add(Username.getText());
Passwords.add(Password.getPassword());
JOptionPane.showMessageDialog(frame, "A basic JOptionPane message dialog");
}
}
});
frame.getContentPane().add(btnRegister);
}
} 它基本上是一个登录系统。现在我正在努力将用户输入的密码添加到密码数组中。
答案 0 :(得分:-1)
要给它一个对象名称并使用get Password
JPasswordField pass = new JPasswordField();
pass.setBounds(231, 134, 131, 23);
frame.getContentPane().add(pass);
pass.getPassword();