java 检查布尔值是否设置为true

gt0wga4j  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(168)

我有一个问题,我有两个类,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仍然有相同的效果。

aiazj4mn

aiazj4mn1#

要解决您的问题,您需要使用观察者模式,在对象之间进行特定的通信,这意味着任何更改发生在已验证的字段中,它将通知主类。
你可以阅读更多关于这个模式The Observer Pattern in Java
主登录类:

public class MainLogin implements PropertyChangeListener {
    private static boolean authenticated;
      public static void main(String[] args) {
            Login login = new Login();
            MainLogin observer = new MainLogin();
            login.authenticated.addPropertyChangeListener(observer);
        }
    
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            this.authenticated = (boolean) evt.getNewValue();
            if (this.authenticated)
                System.exit(0);
        }
    }

PCLauth分类:

public class PCLauth  {

    private boolean authenticated;
    private PropertyChangeSupport support;

     public PCLauth () {
         support = new PropertyChangeSupport(this);
     }

    public void addPropertyChangeListener(PropertyChangeListener pcl) {
        support.addPropertyChangeListener(pcl);
    }

    public void removePropertyChangeListener(PropertyChangeListener pcl) {
        support.removePropertyChangeListener(pcl);
    }

   public void authenticate(boolean state) {
        support.firePropertyChange("authenticated", this.authenticated, state);
        this.authenticated = state;
   }

登录类:

public class Login {
    
        private  JTextField usr_field;
        private  JLabel title;
        private  JButton login_btn;
        private  JPasswordField pwd_field;
        private  String usr = "admin";
        private static String pass = "pass";
    
        public  PCLauth authenticated;
    
        public Login() {
            this.authenticated = new PCLauth();
            buildGui();
        }
    
        /**
         * @wbp.parser.entryPoint
         *
         *
         */
    
    
        private  void buildGui()
        {
            JFrame frame = new JFrame();
            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0.5;
            c.weighty = 0.5;
            c.gridx = 1;
            c.gridy = 0;
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.getContentPane().setBackground(new Color(74, 74, 74));
            frame.getContentPane().setLayout(new GridBagLayout());
            frame.setTitle("Login");
    
            JPanel panel = new JPanel(new GridBagLayout( ));
            panel.setBackground(new Color(74, 74, 74));
            frame.getContentPane().add(panel);
            JLabel username = new JLabel("User name");
            panel.add(username, c);
            c.gridx++;
            usr_field = new JTextField();
            panel.add(usr_field, c);
            usr_field.setColumns(10);
    
            c.gridx = 1;
            c.gridy = 1;
    
            title = new JLabel("password");
            panel.add(title, c);
    
    
            c.gridx++;
    
            pwd_field = new JPasswordField();
    
    
            panel.add(pwd_field, c);
    
            c.gridx = 1;
            c.gridy = 4;
    
            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())) {
                        System.out.println( SwingUtilities.isEventDispatchThread()  + ", " +Thread.currentThread().getName());
    
                        authenticated.authenticate(true);
    
                    }
                    else {
                        authenticated.authenticate(false);
                        JOptionPane.showMessageDialog(panel, "Obs,your username or password is incorrect!");
                    }
                }
            });
            panel.add(login_btn, c);
            frame.setSize(new Dimension(500, 500));
            frame.setVisible(true);
            frame.setResizable(false);
    
        }
    }

相关问题