📄 loginjdialog.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * LoginJDialog.java * * Created on 2008-12-23, 11:20:29 */package frame;import java.awt.Dimension;import java.awt.Toolkit;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.JOptionPane;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import utils.UIUtils;/** * * @author Jason */public class LoginJDialog extends javax.swing.JDialog { /** Creates new form LoginJDialog */ public LoginJDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); } /** 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">//GEN-BEGIN:initComponents private void initComponents() { usernameJLbl = new javax.swing.JLabel(); usernameJTxt = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jPasswordField1 = new javax.swing.JPasswordField(); loginJBtn = new javax.swing.JButton(); exitJBtn = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("欢迎使用电子通讯录"); usernameJLbl.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); usernameJLbl.setText("用户名:"); usernameJTxt.setText("admin"); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel2.setText("密码:"); loginJBtn.setText("登录"); loginJBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { loginJBtnActionPerformed(evt); } }); exitJBtn.setText("退出"); exitJBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitJBtnActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(loginJBtn) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(exitJBtn)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(usernameJLbl) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(usernameJTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap()) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel2, usernameJLbl}); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jPasswordField1, usernameJTxt}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(usernameJLbl) .addComponent(usernameJTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(loginJBtn) .addComponent(exitJBtn)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void exitJBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitJBtnActionPerformed // TODO add your handling code here: System.exit(0);}//GEN-LAST:event_exitJBtnActionPerformed private void loginJBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginJBtnActionPerformed try { // TODO add your handling code here: String username = this.usernameJTxt.getText(); if (username.trim().length() == 0) { JOptionPane.showMessageDialog(this, "用户名不能为空!", "警告", JOptionPane.WARNING_MESSAGE); return; } String pwd = String.valueOf(this.jPasswordField1.getPassword()); if (pwd.trim().length() == 0) { JOptionPane.showMessageDialog(this, "密码不能为空!"); return; } Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/eabook", "root","root" ); Statement stmt = con.createStatement(); String sql = "select * from t_userinfo where username = '" + username + "' and pwd = '" + pwd + "'"; System.out.print(sql); ResultSet rs = stmt.executeQuery(sql); if(rs.next()){ JOptionPane.showMessageDialog(this,"登录成功!"); this.dispose(); RecordJFrame recordJFrame = new RecordJFrame(); recordJFrame.setVisible(true); }else{ JOptionPane.showMessageDialog(this, "用户名或密码有误,请检查!"); } if(stmt != null){ stmt.close(); } if(con != null){ con.close(); } } catch (SQLException ex) { Logger.getLogger(LoginJDialog.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(LoginJDialog.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_loginJBtnActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try { UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); LoginJDialog dialog = new LoginJDialog(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } });// Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();// int x = (screenSize.width - dialog.getWidth())/2;// int y = (screenSize.height - dialog.getHeight())/2;// dialog.setLocation(x,y); UIUtils.setComponentAtScreenCenter(dialog); dialog.setVisible(true); } catch (ClassNotFoundException ex) { Logger.getLogger(LoginJDialog.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(LoginJDialog.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(LoginJDialog.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedLookAndFeelException ex) { Logger.getLogger(LoginJDialog.class.getName()).log(Level.SEVERE, null, ex); } } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton exitJBtn; private javax.swing.JLabel jLabel2; private javax.swing.JPasswordField jPasswordField1; private javax.swing.JButton loginJBtn; private javax.swing.JLabel usernameJLbl; private javax.swing.JTextField usernameJTxt; // End of variables declaration//GEN-END:variables}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -