我正在使用JPasswordField爲登錄功能提供用戶名和密碼。 .getPassword適用於密碼,但不適用於用戶名。任何想法,如果我輸入相同的確切的東西,爲什麼它不工作的用戶名?Java JPasswordField .getPassword()問題
這裏是我的代碼:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
class pros
{
JFrame frame;
private static void show(){
JFrame frame = new JFrame("LOGIN");
frame.setSize(450,450);
frame.setLayout(new GridLayout(0,1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
JLabel username = new JLabel("Enter the Username: ",JLabel.LEFT);
username.setForeground(Color.WHITE);
username.setFont(username.getFont().deriveFont(24f));
panel.add(username);
JPasswordField user = new JPasswordField(10);
user.setEchoChar('?');
panel.add(user);
JLabel password = new JLabel("Enter your Password: ",JLabel.RIGHT);
password.setForeground(Color.WHITE);
password.setFont(password.getFont().deriveFont(24f));
panel.add(user);
panel.add(password);
JPasswordField field = new JPasswordField(10);
field.setEchoChar('•');
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String input = String.valueOf(field.getPassword());
String user = String.valueOf(user.getPassword());
if(input.equals("double" && user.equals("relocating")))
{
field.setText("");
pane();
}
}});
panel.add(field);
panel.add(ok);
frame.add(panel);
frame.setVisible(true);
}
或者可能是因爲您使用的字符串和字段的名稱相同 – Cherif