简体   繁体   English

如何使用 Java Swing 在按钮操作侦听器上正确设置 session

[英]How to properly set the session on the button action listener using Java Swing

Today I am practicing new tech skills which is java swing with mysql for the database.今天我正在练习新的技术技能,即 java swing 和 mysql 用于数据库。 My task today is to create a login form and when the user click the sign in button, user will go to the next form together with the details of users.我今天的任务是创建一个登录表单,当用户单击登录按钮时,用户将 go 连同用户的详细信息一起转到下一个表单。

Goal: How to get the users details to the second form?目标:如何将用户详细信息获取到第二个表单?

  1. Is java swing have a session like what programming language have? java swing 有一个 session 像什么编程语言有?
  2. If yes, what term name use session on java swing programming?如果是,在 java swing 编程上使用什么术语名称 session?

Login Form:登录表单:

登录表单

Sign In Function: Function:

JButton btnNewButton = new JButton("Sign In");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String email = EmailTxtInput.getText();
            String password = PassTxtInput.getText();
            
            if(email.length() == 0) {
                JOptionPane.showMessageDialog(null,"Invalid Email");
            }
            else if(password.length() == 0) {
                JOptionPane.showMessageDialog(null,"Invalid Password");
            }else {
                
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/health_check?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","");
                    String query = "SELECT COUNT(*) as checking_if_exist, role, first_name, users_id FROM users WHERE email = '"+email+"' AND password = '" +password +"' ";
                    
                    Statement sta = con.createStatement();
                    ResultSet rs = sta.executeQuery(query);
                    rs.next();
                    
                    
                    if(rs.getInt(1) == 1) {
                        
                        JOptionPane.showMessageDialog(null,"Successfully Login");
                        
                        if(rs.getInt(2) == 1) {
                            
                            AdminDashboardScreen admindashboardscreen = new AdminDashboardScreen();
                            
                            admindashboardscreen.setUndecorated(true);
                            admindashboardscreen.setLocationRelativeTo(null);
                            admindashboardscreen.setVisible(true);
                            setVisible(false);
                        }
                        else if(rs.getInt(2) == 2) {
                            
                            UserDashboard userdashboard = new UserDashboard();
                            
                            userdashboard.setUndecorated(true);
                            userdashboard.setLocationRelativeTo(null);
                            userdashboard.setVisible(true);
                            setVisible(false);
                        }
                        
                        
                    }else {
                        JOptionPane.showMessageDialog(null,"Invalid Account");
                    }
                
                    
                    sta.close();
                    
                }catch(Exception ex) {
                    System.out.println(ex.getMessage());
                }
                
            }
            
        }
    });

After I solved the first problem here is what happen after I add constructor to the UserDashboard Form the gui that I created is not showing.在我解决了第一个问题之后,在我将构造函数添加到 UserDashboard 表单之后,我创建的 gui 没有显示出来。

桂

Here is the real output without constructor这是没有构造函数的真正的output

实际输出

Thank you.谢谢你。

You are using Eclipse WindowBuilder.您正在使用 Eclipse WindowBuilder。 In order for custom classes to appear in the design view, each one must have a default, no-arguments constructor as stipulated in the JavaBeans specification.为了使自定义类出现在设计视图中,每个类都必须有一个默认的、无参数的构造函数,如JavaBeans规范中规定的那样。 So give class AdminDashboardScreen and class UserDashboard two constructors: one default constructor – so that you can use the class in WindowBuilder – and another constructor that takes arguments which you need when running your application. So give class AdminDashboardScreen and class UserDashboard two constructors: one default constructor – so that you can use the class in WindowBuilder – and another constructor that takes arguments which you need when running your application.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM