在每个JFrame上显示UserID:Java

时间:2017-02-16 08:06:03

标签: java swing

我尝试将函数设置为,如果有任何用户登录系统,则应在应用程序中可用的所有jLabel上的JFrames中显示UserID。某些内容显示为Your are logged as +UserID

最后我为这个函数编写了一个代码,它编译时没有错误。 但是,由于此功能代码,系统需要一些时间才能加载,而其他jFrameForms也不会显示UserID。 UserID仅显示在管理主页JFrame上。我认为我的编码方式是错误的。

那怎么能解决这个问题呢?希望我能从这里得到帮助。提前谢谢!

  

登录JFrame代码:

package view;

import Conn.DBConnection;
import java.awt.Font;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class Login extends javax.swing.JFrame {

public Login() {

    initComponents();

}

@SuppressWarnings("unchecked")

private void initComponents() {

    jFramePanel = new javax.swing.JPanel();
    jMainPanel = new javax.swing.JPanel();
    jPanel_SignIn = new javax.swing.JPanel();
    jLbl_SchoolName1 = new javax.swing.JLabel();
    jLbl_SchoolAddress1 = new javax.swing.JLabel();
    jLbl_UserID1 = new javax.swing.JLabel();
    txt_UserID = new javax.swing.JTextField();
    jLbl_Password1 = new javax.swing.JLabel();
    txt_Password = new javax.swing.JPasswordField();
    jBtn_Login = new javax.swing.JButton();
    jBtn_Recover = new javax.swing.JButton();
    jLbl_SchoolLogo = new javax.swing.JLabel();
    jLbl_Credit3 = new javax.swing.JLabel();
    jLbl_Credit1 = new javax.swing.JLabel();
    jLbl_Border1 = new javax.swing.JLabel();
    jFrameLabel = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setMinimumSize(new java.awt.Dimension(1670, 926));
    setResizable(false);
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jFramePanel.setPreferredSize(new java.awt.Dimension(1670, 926));
    jFramePanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jMainPanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    jMainPanel.setOpaque(false);
    jMainPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jPanel_SignIn.setOpaque(false);
    jPanel_SignIn.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jLbl_SchoolName1.setFont(new java.awt.Font("Stencil", 0, 54)); // NOI18N
    jLbl_SchoolName1.setForeground(new java.awt.Color(0, 0, 153));
    jLbl_SchoolName1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLbl_SchoolName1.setText("NG/AL - FALAH M.V.");
    jLbl_SchoolName1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    jPanel_SignIn.add(jLbl_SchoolName1, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 30, 500, 70));

    jLbl_SchoolAddress1.setFont(new java.awt.Font("Agency FB", 1, 33)); // NOI18N
    jLbl_SchoolAddress1.setForeground(new java.awt.Color(51, 51, 51));
    jLbl_SchoolAddress1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLbl_SchoolAddress1.setText("Poruthota, Kochchikade.");
    jLbl_SchoolAddress1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    jPanel_SignIn.add(jLbl_SchoolAddress1, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 90, 500, 40));

    jLbl_UserID1.setFont(new java.awt.Font("Tahoma", 1, 22)); // NOI18N
    jLbl_UserID1.setForeground(new java.awt.Color(51, 51, 51));
    jLbl_UserID1.setText("User ID");
    jPanel_SignIn.add(jLbl_UserID1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 280, 140, 50));

    txt_UserID.setFont(new java.awt.Font("Tahoma", 0, 22)); // NOI18N
    txt_UserID.setForeground(new java.awt.Color(51, 51, 51));
    txt_UserID.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            txt_UserIDMouseClicked(evt);
        }
    });
    jPanel_SignIn.add(txt_UserID, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 280, 330, 50));

    jLbl_Password1.setFont(new java.awt.Font("Tahoma", 1, 22)); // NOI18N
    jLbl_Password1.setForeground(new java.awt.Color(51, 51, 51));
    jLbl_Password1.setText("Password");
    jPanel_SignIn.add(jLbl_Password1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 350, 140, 50));

    txt_Password.setFont(new java.awt.Font("Tahoma", 0, 22)); // NOI18N
    txt_Password.setForeground(new java.awt.Color(51, 51, 51));
    txt_Password.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            txt_PasswordMouseClicked(evt);
        }
    });
    jPanel_SignIn.add(txt_Password, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 350, 330, 50));

    jBtn_Login.setBackground(new java.awt.Color(0, 180, 80));
    jBtn_Login.setFont(new java.awt.Font("Eras Bold ITC", 1, 21)); // NOI18N
    jBtn_Login.setForeground(new java.awt.Color(255, 255, 255));
    jBtn_Login.setText("LOGIN");
    jBtn_Login.setBorderPainted(false);
    jBtn_Login.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    jBtn_Login.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jBtn_LoginActionPerformed(evt);
        }
    });
    jPanel_SignIn.add(jBtn_Login, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 420, 330, 50));

    jBtn_Recover.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
    jBtn_Recover.setForeground(new java.awt.Color(0, 0, 153));
    jBtn_Recover.setText("Forgot User ID or Password?");
    jBtn_Recover.setContentAreaFilled(false);
    jBtn_Recover.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    jPanel_SignIn.add(jBtn_Recover, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 470, 330, 50));

    jLbl_SchoolLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/SchoolLogo.png"))); // NOI18N
    jPanel_SignIn.add(jLbl_SchoolLogo, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 10, 140, 130));

    jLbl_Credit3.setFont(new java.awt.Font("Consolas", 0, 20)); // NOI18N
    jLbl_Credit3.setForeground(new java.awt.Color(255, 255, 255));
    jLbl_Credit3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLbl_Credit3.setText("Version 1.0 - Copyright © 2017 Abdul Solutions.");
    jLbl_Credit3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    jPanel_SignIn.add(jLbl_Credit3, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 650, 600, 50));

    jLbl_Credit1.setFont(new java.awt.Font("Consolas", 0, 18)); // NOI18N
    jLbl_Credit1.setForeground(new java.awt.Color(255, 255, 255));
    jLbl_Credit1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLbl_Credit1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ProLogo.png"))); // NOI18N
    jLbl_Credit1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    jPanel_SignIn.add(jLbl_Credit1, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 580, 580, 70));

    jLbl_Border1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 2, true), " LOGIN ", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Eras Bold ITC", 1, 24), new java.awt.Color(255, 255, 255))); // NOI18N
    jPanel_SignIn.add(jLbl_Border1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 200, 600, 370));

    jMainPanel.add(jPanel_SignIn, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 760, 850));

    jFramePanel.add(jMainPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(860, 50, 760, 850));

    jFrameLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/SignPageBg.jpg"))); // NOI18N
    jFramePanel.add(jFrameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1670, 926));

    getContentPane().add(jFramePanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1670, 926));

    pack();
    setLocationRelativeTo(null);
}                    

private void jBtn_LoginActionPerformed(java.awt.event.ActionEvent evt) {                                           

        if (txt_UserID.getText().equals("admin")&&txt_Password.getText().equals("123250")){       
                new AdminHome().setVisible(true);
                AdminHome.jLbl_UserID.setText(Login.txt_UserID.getText());    //coded for display user id
                this.dispose();

        }else{

        String uid = txt_UserID.getText();
        String pass = txt_Password.getText();
        String user_role = "";
        String sql = "SELECT user_role FROM tch_data WHERE user_id='"+uid+"' and password='"+pass+"' UNION SELECT user_role FROM stu_data WHERE user_id='"+uid+"' and password='"+pass+"'";

        try {
        java.sql.Connection c = DBConnection.dbconmethod();
        Statement s=c.createStatement();
        ResultSet rs= s.executeQuery(sql);

            while (rs.next()){  
                user_role = rs.getString(1);

                }if(user_role.equalsIgnoreCase("ADMIN")){
                        new AdminHome().setVisible(true);
                        AdminHome.jLbl_UserID.setText(Login.txt_UserID.getText());    //coded for display user id
                        this.dispose();

                }else {
                        UIManager.put("OptionPane.messageFont", new Font("Monospaced", Font.BOLD, 22));
                        JOptionPane.showMessageDialog(rootPane, "User not Found, Try again!","Error",JOptionPane.ERROR_MESSAGE);
                        txt_UserID.setText(null);
                        txt_Password.setText(null);
                }

        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }    

}                                          

private void txt_UserIDMouseClicked(java.awt.event.MouseEvent evt) {                                        
    if (evt.getClickCount() == 2) {
        txt_UserID.setText(null);
    }
}                                       

private void txt_PasswordMouseClicked(java.awt.event.MouseEvent evt) {                                          
    if (evt.getClickCount() == 2) {
        txt_Password.setText(null);
    }
}                                         

/**
 * @param args the command line arguments
 */
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);
    }


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

// Variables declaration                
private javax.swing.JButton jBtn_Login;
private javax.swing.JButton jBtn_Recover;
private javax.swing.JLabel jFrameLabel;
private javax.swing.JPanel jFramePanel;
private javax.swing.JLabel jLbl_Border1;
private javax.swing.JLabel jLbl_Credit1;
private javax.swing.JLabel jLbl_Credit3;
private javax.swing.JLabel jLbl_Password1;
private javax.swing.JLabel jLbl_SchoolAddress1;
private javax.swing.JLabel jLbl_SchoolLogo;
private javax.swing.JLabel jLbl_SchoolName1;
private javax.swing.JLabel jLbl_UserID1;
private javax.swing.JPanel jMainPanel;
private javax.swing.JPanel jPanel_SignIn;
private javax.swing.JPasswordField txt_Password;
public static javax.swing.JTextField txt_UserID;
// End of variables declaration                   
}
  

管理主页JFrame代码: http://pastebin.com/LcTdAqLf

0 个答案:

没有答案