使用jFrame1中的输入文本作为参考,以便在JFrame2中显示信息

时间:2013-12-27 20:00:08

标签: java swing netbeans login jframe

**您好。我有关于登录系统的这个问题。这是场景:我有2个JFrame。 JFrame1用于登录,JFrame2用于提供信息。用户名,密码和个人信息包含在数据库中。如果Employee1登录,则第二个JFrame应该显示有关Employee 1的信息。我尝试创建一个sql语句,但我不知道如何使用jFrame1中的输入文本作为参考,以便在JFrame2中显示信息。 这是登录时的代码(JFrame1):

import java.sql.*;
import javax.swing.*;

public class LoginForm extends javax.swing.JFrame {

Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
/**
 * Creates new form EmployeeHome1
 */
public LoginForm() {
    initComponents();
    conn = dbconnect.ConnectDB();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    username = new javax.swing.JTextField();
    password = new javax.swing.JPasswordField();
    b_login = new javax.swing.JButton();
    jLabel4 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Employee Payroll System - Log In");
    setMinimumSize(new java.awt.Dimension(400, 310));
    setResizable(false);
    getContentPane().setLayout(null);

    jLabel1.setFont(new java.awt.Font("Always In My Heart", 0, 48)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(255, 255, 255));
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("Employee Payroll System");
    getContentPane().add(jLabel1);
    jLabel1.setBounds(-10, 20, 400, 61);

    jLabel2.setFont(new java.awt.Font("Arial Narrow", 0, 18)); // NOI18N
    jLabel2.setForeground(new java.awt.Color(255, 255, 255));
    jLabel2.setText("username :");
    getContentPane().add(jLabel2);
    jLabel2.setBounds(59, 122, 73, 21);

    jLabel3.setFont(new java.awt.Font("Arial Narrow", 0, 18)); // NOI18N
    jLabel3.setForeground(new java.awt.Color(255, 255, 255));
    jLabel3.setText("password :");
    getContentPane().add(jLabel3);
    jLabel3.setBounds(59, 167, 70, 21);

    username.setFont(new java.awt.Font("Arial Narrow", 0, 18)); // NOI18N
    username.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    getContentPane().add(username);
    username.setBounds(171, 119, 170, 27);

    password.setFont(new java.awt.Font("Arial Narrow", 0, 18)); // NOI18N
    password.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    getContentPane().add(password);
    password.setBounds(171, 164, 170, 27);

    b_login.setFont(new java.awt.Font("Arial Narrow", 0, 18)); // NOI18N
    b_login.setText("Log In");
    b_login.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            b_loginMouseClicked(evt);
        }
    });
    getContentPane().add(b_login);
    b_login.setBounds(159, 229, 71, 29);

    jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Blue_Background.jpg"))); // NOI18N
    getContentPane().add(jLabel4);
    jLabel4.setBounds(0, 0, 400, 310);

    getAccessibleContext().setAccessibleName("");

    pack();
}// </editor-fold>                        

private void b_loginMouseClicked(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:
    String sql = "select * from tbl_login where username = '"+username.getText()+"' and password = '"+password.getText()+"'";
        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
                if (rs.next()){
                    newform();
                    username.getText
                            username
                }
                else {
                    JOptionPane.showMessageDialog(null, "Incorrect username or password.");
                }
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
}                                    

public void newform(){
    String sql1 = "select type from tbl_login where username='"+username.getText()+"' and password='"+password.getText()+"'";
        try {
            pst = conn.prepareStatement(sql1);
            rs = pst.executeQuery();
            rs.next();
            String name = rs.getString("type");
                if (name.equals("admin")) {
                    AdminHome ah = new AdminHome();
                    ah.setVisible(true);
                }
                else {
                    EmployeeHome eh = new EmployeeHome();
                    eh.setVisible(true);
                }
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
}
/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    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(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new LoginForm().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton b_login;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPasswordField password;
private javax.swing.JTextField username;
// End of variables declaration         
}

我唯一关注的是EmployeeHome(JFrame2)。它应显示登录系统的员工的信息。请尽快帮助我。提前谢谢: - )

1 个答案:

答案 0 :(得分:0)

基本上,当您创建新框架时,您需要将其引用传递给您想要提供的信息。

就个人而言,我会创建一个SessionUser对象,该对象封装了数据库中有关当前登录用户的信息。这意味着您不需要持续访问数据库。

例如......

if (rs.next) {

    // Create a new user from the result set....
    User user = createUser(rs);

    if ("admin".equals(user.getType()) {
        AdminHome ah = new AdminHome(user);
        ah.setVisible(true);
    } else {
        EmployeeHome eh = new EmployeeHome(user);
        eh.setVisible(true);
    }

}