我使用Netbeans 8.2和mysql查询浏览器1.1.20.build成功,我需要在成功登录后关闭登录表单

时间:2017-02-27 06:37:46

标签: java mysql netbeans login jframe

这是我登录表单的编码。

package Viewer;

import Model.DBG1;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JOptionPane;

public class login extends javax.swing.JFrame 
{
    public login() 
    {
        initComponents();
    }
    @SuppressWarnings("unchecked")                              
    private void initComponents() 
    {
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        eid = new javax.swing.JTextField();
        login = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        pwd = new javax.swing.JPasswordField();
        cancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

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

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Password");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 170, 70, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("User Name");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 127, -1, 30));
        getContentPane().add(eid, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 130, 210, -1));

        login.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Viewer/res/loginn.jpg"))); // NOI18N
        login.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                loginActionPerformed(evt);
            }
        });
        getContentPane().add(login, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 230, 100, 40));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Login");
        jLabel5.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 80, 140, -1));
        getContentPane().add(pwd, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 170, 210, -1));

        cancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Viewer/res/cancel.jpg"))); // NOI18N
        cancel.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                cancelActionPerformed(evt);
            }
        });
        getContentPane().add(cancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 230, 100, 40));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Viewer/res/forloginbackground_.jpg"))); // NOI18N
        jLabel1.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 460, 370));

        pack();
    }                     
    private void loginActionPerformed(java.awt.event.ActionEvent evt) 
    {                                      

         if (eid.getText().trim().length() == 0 | pwd.getPassword() == null) 
         {
            JOptionPane.showMessageDialog(null, "Username or Password cannot be blank", "Error", 0);
         } 
         else 
         {
            try {
                ResultSet rs = new DBG1().getData("select * from user where eid = '" +eid.getText() + "'");
                Vector v = new Vector();
                if (rs.next()) 
                {
                    v.add(rs.getString(1));
                }
                if (v.isEmpty()) 
                {
                    JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error", 0);
                    eid.setText("");
                    pwd.setText("");
                    eid.grabFocus();
                } 
                else 
                {
                    ResultSet rs1 = new DBG1().getData("select * from login where eid ='" + eid.getText() + "'");
                    while (rs1.next()) 
                    {
                        String type = (rs1.getString(3));

                        if (eid.getText().trim().equalsIgnoreCase(rs1.getString("eid")) && pwd.getText().equals(rs1.getString("pwd")) && type.equals("Admin")) 
                        {
                            this.dispose();
                          //  new Home_Page(eid.getText()).setVisible(true);
                          //  new Home_Page(eid.getText()).setVisible(true);
                        } 
                        else if (eid.getText().trim().equalsIgnoreCase(rs1.getString("username")) && pwd.getText().equals(rs1.getString("password")) && type.equals("Receptionist")) 
                        {
                            this.dispose();
                           // new Home_Page(eid.getText()).setVisible(true);
                        } 
                        else 
                        {
                            JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error", 0);
                            eid.setText("");
                            pwd.setText("");
                            eid.grabFocus();
                        }
                    }
                    System.out.println("Succeeded");
                }
            } 
            catch (Exception e) 
            {
                System.out.println(e);
            }
        }
 //System.exit(0); This one closes the Home_Page as well
    }                                     
     private void cancelActionPerformed(java.awt.event.ActionEvent evt) 
    {                                    
        new login().dispose();
        System.out.println(pwd.getPassword());
        System.out.println("Cleared");
    }                                         
    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 cancel;
    private javax.swing.JTextField eid;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JButton login;
    private javax.swing.JPasswordField pwd;                 
}
===================================================================

这是我的Home_Page的编码。     包查看器;

import Model.JDBC;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class Home_Page extends javax.swing.JFrame 
{

    String User;
    Model.JDBC j=new JDBC();

    public Home_Page() 
    {
        initComponents();

      // User=user;
        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension d = t.getScreenSize();
        int w = (int) d.getWidth();
        int h = (int) d.getHeight()-40;
        setBounds(0, 0, w, h);
       jLabel2.setText(new Controler.getDate().date());
        time();
       // jLabel3.setText(user);
        ResultSet rs;

         try 
         {
      //      rs = j.getData("select type from login where eid='"+eid+"'");
          /* if(rs.next()){
                if(rs.getString(1).equals("Admin"))
                {
                    home.setEnabled(true);
                    job.setEnabled(true);
                    employee.setEnabled(true);
                    jButton1.setEnabled(true);
                    machine.setEnabled(true);
                    branch.setEnabled(true);

                }
                else if(rs.getString(1).equals("user"))
                {
                    home.setEnabled(false);
                    machine.setEnabled(false);
                    branch.setEnabled(false);
                    employee.setEnabled(false);

                }
                else
                {

                }
            }*/
        } 
        catch (Exception ex) 
        {
            Logger.getLogger(Home_Page.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

      public static void time() 
      {
        new Thread(new Runnable() 
        {
            public  void run() 
            {
                while (true) 
                {
                    String times = new Date().toString();
                    String h = times.split(" ")[3];
                 //   jLabel1.setText(h);
                    try 
                    {
                        Thread.sleep(60);
                    } 
                    catch (Exception e) 
                    {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    @SuppressWarnings("unchecked")

    private void initComponents() 
    {
        jToolBar1 = new javax.swing.JToolBar();
        home = new javax.swing.JButton();
        job = new javax.swing.JButton();
        employee = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        machine = new javax.swing.JButton();
        branch = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jDesktopPane1 = new javax.swing.JDesktopPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setRollover(true);

        home.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Viewer/res/home.jpg"))); // NOI18N
        home.setText("Home");
        home.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        home.setFocusable(false);
        home.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        home.setMaximumSize(new java.awt.Dimension(100, 100));
        home.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        home.addAncestorListener(new javax.swing.event.AncestorListener() 
        {
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) 
            {
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) 
            {
                homeAncestorRemoved(evt);
            }
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) 
            {
            }
        });
        home.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                homeActionPerformed(evt);
            }
        });
        home.addKeyListener(new java.awt.event.KeyAdapter() 
        {
            public void keyPressed(java.awt.event.KeyEvent evt) 
            {
                homeKeyPressed(evt);
            }
        });
        jToolBar1.add(home);

        job.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Viewer/res/jobregistry.jpg"))); // NOI18N
        job.setText("Job Registry");
        job.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        job.setFocusable(false);
        job.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        job.setMaximumSize(new java.awt.Dimension(100, 100));
        job.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        job.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                jobActionPerformed(evt);
            }
        });
        job.addKeyListener(new java.awt.event.KeyAdapter() 
        {
            public void keyPressed(java.awt.event.KeyEvent evt) 
            {
                jobKeyPressed(evt);
            }
        });
        jToolBar1.add(job);

        employee.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Viewer/res/user.jpg"))); // NOI18N
        employee.setText("Employee");
        employee.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        employee.setFocusable(false);
        employee.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        employee.setMaximumSize(new java.awt.Dimension(100, 100));
        employee.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        employee.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                employeeActionPerformed(evt);
            }
        });
        employee.addKeyListener(new java.awt.event.KeyAdapter() 
        {
            public void keyPressed(java.awt.event.KeyEvent evt) 
            {
                employeeKeyPressed(evt);
            }
        });
        jToolBar1.add(employee);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Viewer/res/user4.jpg"))); // NOI18N
        jButton1.setText("User");
        jButton1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setMaximumSize(new java.awt.Dimension(100, 100));
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.addKeyListener(new java.awt.event.KeyAdapter() 
        {
            public void keyPressed(java.awt.event.KeyEvent evt) 
            {
                jButton1KeyPressed(evt);
            }
        });
        jToolBar1.add(jButton1);

        machine.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Viewer/res/machine2.jpg"))); // NOI18N
        machine.setText("Machine Registry");
        machine.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        machine.setFocusable(false);
        machine.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        machine.setMaximumSize(new java.awt.Dimension(100, 100));
        machine.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        machine.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                machineActionPerformed(evt);
            }
        });
        machine.addKeyListener(new java.awt.event.KeyAdapter() 
        {
            public void keyPressed(java.awt.event.KeyEvent evt) 
            {
                machineKeyPressed(evt);
            }
        });
        jToolBar1.add(machine);

        branch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Viewer/res/branch4.jpg"))); // NOI18N
        branch.setText("Branch Registry");
        branch.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        branch.setFocusable(false);
        branch.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        branch.setMaximumSize(new java.awt.Dimension(100, 100));
        branch.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        branch.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                branchActionPerformed(evt);
            }
        });
        branch.addKeyListener(new java.awt.event.KeyAdapter() 
        {
            public void keyPressed(java.awt.event.KeyEvent evt) 
            {
                branchKeyPressed(evt);
            }
        });
        jToolBar1.add(branch);

        jLabel1.setText("jLabel1");

        jLabel2.setText("jLabel2");

        jLabel3.setText("jLabel3");

        jDesktopPane1.setBackground(new java.awt.Color(51, 51, 51));

        javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
        jDesktopPane1.setLayout(jDesktopPane1Layout);
        jDesktopPane1Layout.setHorizontalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jDesktopPane1Layout.setVerticalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 956, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 686, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(0, 874, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jDesktopPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDesktopPane1)
                .addContainerGap())
        );

        pack();
    }                       

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

    }                                    

    private void homeAncestorRemoved(javax.swing.event.AncestorEvent evt) 
    {   }                                    

    private void homeKeyPressed(java.awt.event.KeyEvent evt) 
    {  }                               

    private void jobActionPerformed(java.awt.event.ActionEvent evt) 
    {                                    
        job jb=new job();
        jDesktopPane1.add(jb);
        jb.setVisible(true);
    }                                   

    private void jobKeyPressed(java.awt.event.KeyEvent evt) 
    { }                              

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

        employee emp=new employee();
        jDesktopPane1.add(emp);
        emp.setVisible(true);
    }                                        

    private void employeeKeyPressed(java.awt.event.KeyEvent evt) 
    {     }                                   

    private void machineActionPerformed(java.awt.event.ActionEvent evt) 
    {                                        
        machine m=new machine();
        jDesktopPane1.add(m);
    }                                       

    private void machineKeyPressed(java.awt.event.KeyEvent evt) 
    {   }                                  

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

        branch b=new branch();
        jDesktopPane1.add(b);
        b.setVisible(true);

// TODO add your handling code here:
    }                                      

    private void branchKeyPressed(java.awt.event.KeyEvent evt) 
    {                                  
        // TODO add your handling code here:
    }                                 

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

        User u=new User();
        jDesktopPane1.add(u);
        u.setVisible(true);

// TODO add your handling code here:
    }                                        

    private void jButton1KeyPressed(java.awt.event.KeyEvent evt) 
    {                                    
        // TODO add your handling code here:
    }                                   

    /**
     * @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(Home_Page.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (InstantiationException ex) 
        {
            java.util.logging.Logger.getLogger(Home_Page.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (IllegalAccessException ex) 
        {
            java.util.logging.Logger.getLogger(Home_Page.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (javax.swing.UnsupportedLookAndFeelException ex) 
        {
            java.util.logging.Logger.getLogger(Home_Page.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        Calendar expiry = Calendar.getInstance();
        expiry.set(2013, 9 , 27,0,0); // Expire at 31 Jan 2010
        Calendar now = Calendar.getInstance();

        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
             //  new Home_Page("Admin").setVisible(true);
            }
        });
    }                   
    private javax.swing.JButton branch;
    private javax.swing.JButton employee;
    private javax.swing.JButton home;
    private javax.swing.JButton jButton1;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton job;
    private javax.swing.JButton machine;                 
}

现在发生的事情是当我运行我的java类时,主要的一个,登录表单和Home_Page同时出现。我要做的是,将代码设置为在用户成功登录时自动关闭登录表单。 我尝试的最后一种方法关闭了Home_page和登录表单。  //System.exit(0);

请尽可能帮助我!非常感谢你提前。

0 个答案:

没有答案