我有一个问题,我有两个类,main和login。login是一个菜单,有一个登录按钮,当按钮被按下时,它应该设置一个布尔值为true。但是当我在main函数中检查这个时,它在if语句中没有做任何事情。
请忽略任何不好的做法等,我只是学习和探索
登录类
package menu;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class login {
private static JTextField usr_field;
private static JLabel title;
private static JButton login_btn;
private static JPasswordField pwd_field;
private static String usr = "admin";
private static String pass = "pass";
private static JLabel incorrect;
public static boolean authenticated;
/**
* @wbp.parser.entryPoint
*/
public static void loginWindow()
{
JFrame frame = new JFrame();
frame.getContentPane().setBackground(new Color(74, 74, 74));
frame.getContentPane().setLayout(new MigLayout("", "[grow]", "[grow]"));
frame.setTitle("Login");
JPanel panel = new JPanel();
panel.setBackground(new Color(74, 74, 74));
frame.getContentPane().add(panel, "cell 0 0,grow");
panel.setLayout(null);
usr_field = new JTextField();
usr_field.setForeground(Color.WHITE);
usr_field.setFont(new Font("Courier New", Font.BOLD, 15));
usr_field.setBackground(new Color(56, 56, 56));
usr_field.setBounds(31, 68, 218, 19);
panel.add(usr_field);
usr_field.setColumns(10);
title = new JLabel("JMultitool");
title.setForeground(new Color(255, 255, 255));
title.setFont(new Font("Sylfaen", Font.BOLD, 32));
title.setBounds(68, 15, 170, 43);
panel.add(title);
login_btn = new JButton("Login");
login_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
char[] inputPass = pwd_field.getPassword();
char[] appPass = login.pass.toCharArray();
if (Arrays.equals(inputPass, appPass) && usr.equals(usr_field.getText()))
{
authenticated = true;
}
else
{
authenticated = false;
incorrect.setVisible(true);
}
}
});
login_btn.setFont(new Font("Tahoma", Font.BOLD, 10));
login_btn.setForeground(Color.WHITE);
login_btn.setBackground(new Color(65, 65, 65));
login_btn.setBounds(99, 126, 85, 21);
panel.add(login_btn);
pwd_field = new JPasswordField();
pwd_field.setForeground(new Color(255, 255, 255));
pwd_field.setBackground(new Color(56, 56, 56));
pwd_field.setBounds(31, 97, 218, 19);
panel.add(pwd_field);
incorrect = new JLabel("Incorrect username or password!");
incorrect.setForeground(Color.RED);
incorrect.setFont(new Font("Tahoma", Font.PLAIN, 12));
incorrect.setBounds(53, 156, 190, 13);
panel.add(incorrect);
incorrect.setVisible(false);
frame.setVisible(true);
frame.setSize(310, 230);
frame.setResizable(false);
}
}
主类
package multitool;
import menu.loginFront;
public class main {
public static void startMenus()
{
loginFront.loginWindow();
if (loginFront.authenticated)
{
System.exit(0); // should exit if authenticated is true.
}
}
public static void main(String[] args){
startMenus();
}
}
我试过使用一个getter和setter仍然有相同的效果。
1条答案
按热度按时间aiazj4mn1#
要解决您的问题,您需要使用观察者模式,在对象之间进行特定的通信,这意味着任何更改发生在已验证的字段中,它将通知主类。
你可以阅读更多关于这个模式The Observer Pattern in Java
主登录类:
PCLauth分类:
登录类: