netbeans 在Java中登录后如何显示用户名?

ergxz8rk  于 2022-11-10  发布在  Java
关注(0)|答案(3)|浏览(247)

当用户成功登录时,我想在欢迎jFrame上显示用户名。我用Netbeans做这个项目,使用的DBMS是MySQL。基本上我创建了两个jFrames
一个为login.java,另一个为welcome.javajLabel被放置在welcome.java中,变量名被初始化为jLabel_UnameDisplay
这里是图片说明我的要求和项目的总代码:

登录名.java

public class login extends javax.swing.JFrame {

public login() {
    initComponents();
}

@SuppressWarnings("unchecked")                    
private void initComponents() {

    jPanel = new javax.swing.JPanel();
    jLabel_uname = new javax.swing.JLabel();
    jLabel_pass = new javax.swing.JLabel();
    txt_uname = new javax.swing.JTextField();
    txt_pass = new javax.swing.JPasswordField();
    jButton_login = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jLabel_uname.setText("User Name");
    jPanel.add(jLabel_uname, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 110, 100, 40));

    jLabel_pass.setText("Password");
    jPanel.add(jLabel_pass, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 170, 100, 40));
    jPanel.add(txt_uname, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 110, 160, 40));
    jPanel.add(txt_pass, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 170, 160, 40));

    jButton_login.setText("Login");
    jButton_login.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton_loginActionPerformed(evt);
        }
    });

    jPanel.add(jButton_login, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 230, 90, 40));

    getContentPane().add(jPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 400, 300));

    pack();
}                      

private void jButton_loginActionPerformed(java.awt.event.ActionEvent evt) {                                              
     if (txt_uname.getText().equals("admin")&&txt_pass.getText().equals("1234")){
        new welcome().setVisible(true);
        this.dispose();

    }else{
        try {
        Connection c;
        Class.forName("com.mysql.jdbc.Driver");
        c=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","123");

        Statement s=c.createStatement();
            ResultSet rs= s.executeQuery("SELECT * FROM user WHERE status='1'");

            while (rs.next()) {                    
                String unmae=rs.getString("username");
                String pass=rs.getString("password");

                if(unmae.equals(txtuname.getText()) && pass.equals(txtpass.getText())){
                    new welcome().setVisible(true);
                    this.dispose();
                }

            }

        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(rootPane, "Check Your Username or Password","Error",JOptionPane.ERROR_MESSAGE);
        }

    }
}                                             

public static void main(String args[]) {

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new login().setVisible(true);
        }
    });
}

private javax.swing.JButton jButton_login;
private javax.swing.JLabel jLabel_pass;
private javax.swing.JLabel jLabel_uname;
private javax.swing.JPanel jPanel;
private javax.swing.JPasswordField txt_pass;
private javax.swing.JTextField txt_uname;

}

欢迎使用.java

public class welcome extends javax.swing.JFrame {

public welcome() {
    initComponents();
}

@SuppressWarnings("unchecked")

private void initComponents() {

    jPanel = new javax.swing.JPanel();
    jLabel_UnameDisplay = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jLabel_UnameDisplay.setBackground(new java.awt.Color(102, 255, 102));
    jLabel_UnameDisplay.setOpaque(true);
    jPanel.add(jLabel_UnameDisplay, new org.netbeans.lib.awtextra.AbsoluteConstraints(93, 69, 199, 126));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
}                   

public static void main(String args[]) {

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(welcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new welcome().setVisible(true);
        }
    });
}

private javax.swing.JLabel jLabel_UnameDisplay;
private javax.swing.JPanel jPanel;

}
5us2dqdw

5us2dqdw1#

有很多方法可以做到这一点,但您希望的目标是将登录框架与欢迎框架分离,以便显示一个框架的决定不是由另一个框架做出的。
你可以
使用某种类型的观察者/委托人/监听者,当用户通过正确的身份验证时,它会得到通知,并将用户信息传递给它。然后,它将负责决定下一步要做什么,在这里,显示欢迎视图,并将用户信息传递给它(作为示例)
这是Swing中常见概念
你可以
使用某种对话框(而不是JFrame),允许代码在对话框可见时阻塞,并在对话框关闭时继续。
在这种情况下,您需要提供某种getter来获取用户详细信息(如果用户出于某种原因取消了对话框,则为null),然后您将能够根据结果决定要做什么
有关详细信息,请参阅How to use dialogs
在任何情况下,你都需要能够将登录视图中的信息传递到欢迎视图中,这种手段一般都很常见,利用setter和getter以及基本的方法调用。

cunj1qz1

cunj1qz12#

最简单的方法是在创建时将用户名传递给下一个框架。这样下一个框架(如welcome-page)就可以使用该用户名。您也可以传递一个完整的对象,以存储更多用户特定的数据。
下面是代码示例。在欢迎页面中进行细微更改

/**
 * Create the frame and pass the parameter during creation
 */
public welcome (Component parent, String logOnUserName) {
    this.parent = parent; // This is to send the reference of parent login
                          // page. This can be useful if you want to
                          // comeback to loginpage
    this.logOnUserName=logOnUserName;

...
并在从登录调用欢迎页面时进行此更改
新登录(此,用户名)。setVisible(true);
现在欢迎页面中出现用户名。在UI中显示它。

mzmfm0qo

mzmfm0qo3#

我有这个问题的最佳答案。当你点击登录按钮,然后你必须指定有新的框架,然后进入数据库,并选择你想显示在登录后的框架列。你可以显示与使用JLable。也可以提供此视频的参考:
https://youtu.be/vlYLzCzZigg

相关问题