是否可以創建一個password字段,您可以在java中獲取文本?現在我嘗試類似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);
}
} 這基本上是一個登錄系統。現在我正在將用戶輸入的密碼添加到密碼數組中。
可以添加完整的例外 – Austin
什麼都不行,而且不會是如何工作的,因爲什麼是私有?爲什麼你有兩個JPasswordFields?你想描述一個編譯錯誤嗎? – EJP
你可以先編譯它嗎?好像你沒有在這裏給它命名'Username = new JTextField();' – abcOfJavaAndCPP