我想知道我是否可以将变量 user2
和 pass2
(它们都位于类 showWindow()
的 Register
方法中)用于 {{1} showWindow()
类中的 }} 方法,以便在 LoginDemo
类中输入的用户名和密码将与 Register
类中的输入相匹配。
LoginDemo
public class LoginDemo
public static void showWindow() {
JPanel panel = new JPanel();
JFrame frame = new JFrame("Log in/Sign up ");
frame.setSize(300, 180);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null);
userLabel = new JLabel("USERNAME");
userLabel.setBounds(10, 10, 80, 25);
panel.add(userLabel);
userText = new JTextField(20);
userText.setBounds(100, 10, 160, 25);
panel.add(userText);
passwordLabel = new JLabel("PASSWORD");
passwordLabel.setBounds(10, 40, 80, 25);
panel.add(passwordLabel);
passwordText = new JPasswordField(20);
passwordText.setBounds(100, 40, 160, 25);
panel.add(passwordText);
button = new JButton("LOG-IN");
button.setBounds(10, 80, 80, 25);
button.addActionListener(new LoginDemo());
panel.add(button);
success = new JLabel("");
success.setBounds(10, 110, 300, 25);
panel.add(success);
JButton registerButton = new JButton("CREATE ACCOUNT");
registerButton.setBounds(130, 80, 150, 25);
panel.add(registerButton);
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
Register w2 = new Register();
w2.showWindow();
}
});
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
Register w2 = new Register();
String user = userText.getText();
String password = passwordText.getText();
if(user.equals(w2.user2) && password.equals(w2.pass2))
{
JOptionPane.showMessageDialog(null,"Log in Successful!");
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Login Details" + " \nLogin Error ");
}
}
}
答案 0 :(得分:0)
你可以通过 Login 类的构造函数传递它。 向登录类添加构造函数。
public class LoginDemo {
private String user2, pass2;
public LoginDemo(String user2, String pass2) {
this.user2 = user2;
this.pass2 = pass2;
}
现在在您的注册类中传递值
LoginDemo w1 = new LoginDemo(user2, pass2);
在注册类中,您可以通过 this.user2
和 this.pass2