swing桌面程序登陆窗口

/*
 * StuManager.java
 *
 * Created on __DATE__, __TIME__
 */
 
package swing;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
/**
 *
 * @author  __USER__
 */
public class StuLanding extends javax.swing.JFrame implements ActionListener {
 /** Creates new form StuManager */
 public StuLanding() {
  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.
  */
 //GEN-BEGIN:initComponents
 // <editor-fold defaultstate="collapsed" desc="Generated Code">
 private void initComponents() {
  this.setTitle("Student Landing");
  login = new javax.swing.JButton("登陆");
  cancel = new javax.swing.JButton("取消");
  jLabel1 = new javax.swing.JLabel();
  jLabel2 = new javax.swing.JLabel();
  jPanel1 = new javax.swing.JPanel();
  jLabel3 = new javax.swing.JLabel();
  jLabel4 = new javax.swing.JLabel();
  jLabel5 = new javax.swing.JLabel();
  jPasswordField1 = new javax.swing.JPasswordField();
  jTextField1 = new javax.swing.JTextField();
  this.setBounds(getX(),getY(),300,350);
  this.setBackground(getBackground().BLACK);
  Toolkit kit=Toolkit.getDefaultToolkit();
  Dimension screenSize=kit.getScreenSize();
  this.setSize(WIDTH,HEIGHT);
  int width=screenSize.width;
  int height=screenSize.height;
  int x=(width-WIDTH)/2;
  int y=(height-HEIGHT)/2;
  this.setLocation(x, y);
  
  setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
  login.setText("登陆");
  login.setActionCommand("login");
  login.addActionListener(this);
  cancel.setText("取消");
  cancel.setActionCommand("cancel");
  cancel.addActionListener(this);
  jLabel1.setText("user");
 
  jLabel2.setText("password");
 
  jLabel3.setText("information manager system");
 
  jLabel4.setText("*******************");
 
  jLabel5.setText("*******************");
 
  org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(
    jPanel1);
  jPanel1.setLayout(jPanel1Layout);
  jPanel1Layout
    .setHorizontalGroup(jPanel1Layout
      .createParallelGroup(
        org.jdesktop.layout.GroupLayout.LEADING)
      .add(
        jPanel1Layout
          .createSequentialGroup()
          .add(
            jPanel1Layout
              .createParallelGroup(
                org.jdesktop.layout.GroupLayout.LEADING)
              .add(
                jPanel1Layout
                  .createSequentialGroup()
                  .add(
                    76,
                    76,
                    76)
                  .add(
                    jLabel4,
                    org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                    114,
                    org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
              .add(
                jPanel1Layout
                  .createSequentialGroup()
                  .add(
                    74,
                    74,
                    74)
                  .add(
                    jLabel5))
              .add(
                jPanel1Layout
                  .createSequentialGroup()
                  .add(
                    47,
                    47,
                    47)
                  .add(
                    jLabel3)))
          .addContainerGap(64, Short.MAX_VALUE)));
  jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
    org.jdesktop.layout.GroupLayout.LEADING).add(
    jPanel1Layout.createSequentialGroup().add(jLabel5)
      .addPreferredGap(
        org.jdesktop.layout.LayoutStyle.UNRELATED).add(
        jLabel3,
        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
        24, Short.MAX_VALUE).add(14, 14, 14).add(
        jLabel4)));
 
  org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
    getContentPane());
  getContentPane().setLayout(layout);
  layout
    .setHorizontalGroup(layout
      .createParallelGroup(
        org.jdesktop.layout.GroupLayout.LEADING,isResizable())
      .add(jPanel1,
        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
        Short.MAX_VALUE)
      .add(
        layout
          .createSequentialGroup()
          .add(
            layout
              .createParallelGroup(
                org.jdesktop.layout.GroupLayout.TRAILING,
                false)
              .add(
                org.jdesktop.layout.GroupLayout.LEADING,
                layout
                  .createSequentialGroup()
                  .add(
                    24,
                    24,
                    24)
                  .add(
                    login)
                  .addPreferredGap(
                    org.jdesktop.layout.LayoutStyle.RELATED,
                    org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                    Short.MAX_VALUE)
                  .add(
                    cancel))
              .add(
                org.jdesktop.layout.GroupLayout.LEADING,
                layout
                  .createSequentialGroup()
                  .add(
                    32,
                    32,
                    32)
                  .add(
                    layout
                      .createParallelGroup(
                        org.jdesktop.layout.GroupLayout.TRAILING)
                      .add(
                        jLabel1)
                      .add(
                        jLabel2))
                  .addPreferredGap(
                    org.jdesktop.layout.LayoutStyle.RELATED)
                  .add(
                    layout
                      .createParallelGroup(
                        org.jdesktop.layout.GroupLayout.LEADING)
                      .add(
                        jPasswordField1,
                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                        124,
                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                      .add(
                        jTextField1))))
          .add(59, 59, 59)));
  layout
    .setVerticalGroup(layout
      .createParallelGroup(
        org.jdesktop.layout.GroupLayout.LEADING)
      .add(
        org.jdesktop.layout.GroupLayout.TRAILING,
        layout
          .createSequentialGroup()
          .add(
            jPanel1,
            org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
            org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
            Short.MAX_VALUE)
          .add(38, 38, 38)
          .add(
            layout
              .createParallelGroup(
                org.jdesktop.layout.GroupLayout.BASELINE)
              .add(jLabel1)
              .add(
                jTextField1,
                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
          .add(18, 18, 18)
          .add(
            layout
              .createParallelGroup(
                org.jdesktop.layout.GroupLayout.BASELINE)
              .add(jLabel2)
              .add(
                jPasswordField1,
                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                20,
                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
          .add(18, 18, 18)
          .add(
            layout
              .createParallelGroup(
                org.jdesktop.layout.GroupLayout.BASELINE)
              .add(login).add(
                cancel)).add(
            45, 45, 45)));
 
  pack();
 }// </editor-fold>
 //GEN-END:initComponents
 
 public void actionPerformed(ActionEvent e) {
  if ("login".equals(e.getActionCommand())) {
   //通过数据库验证
//   sql s=new sql();
//   boolean flag = true;
//   if (flag) {
    boolean x,y;
    nameText=jTextField1.getText();
    jPassword=jPasswordField1.getPassword();
    sjPassword=String.valueOf(jPassword);
    System.out.println(nameText);
    System.out.println(sjPassword);
    Connection con=null;
    try {
      try {
      Class.forName(DriverClass);
     } catch (ClassNotFoundException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
     }
        con = DriverManager.getConnection(url,userName,Password);
        System.out.println("The database connection succeeds");
      java.sql.Statement st =  con.createStatement();
//      String sqlsyn="select * from [use] where users=jTextField1.getText()";//错误语法
      String sqlsyn="select * from [use] where users='"+ jTextField1.getText() +"'";
//      ResultSet rs = null;
      ResultSet rs = st.executeQuery(sqlsyn);
     while(rs.next()){
       try {
       sName=rs.getString(1);
       sPassword=rs.getString(2);
      } catch (SQLException e2) {
       // TODO Auto-generated catch block
       e2.printStackTrace();
      }
      }
     rs.close();
    } catch (SQLException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
    System.out.println(sName);
    System.out.println(sPassword);
    x=(nameText.equals(sName));
    if(x==true){System.out.println(nameText+"true"+sName);}
    else {System.out.println(x+"false");}
    y=(sjPassword.equals(sPassword));
    if(y==true){System.out.println(sjPassword+"true"+sPassword);}
    else {System.out.println(sjPassword+"false"+sPassword);}
    boolean z=(x&&y);
    if(z==true){
//     this.setVisible(false);
     this.dispose();
     StuWindows sw=new StuWindows();
     sw.initComponents();
     System.out.println(e.getActionCommand());
    }
    else if(z=false){
     //当输入不正确的用户名和密码时,系统会将文本框内的内容清空
     jTextField1.setText("");
     jPasswordField1.setText("");
    }
//   }
  }
  if("cancel".equals(e.getActionCommand())){
   System.out.println(e.getActionCommand());
   jTextField1.setText("");
   jPasswordField1.setText(""); 
  }
 
 }
// public void sqlcon(){
//    String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  //加载JDBC驱动
//    String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=his";  //连接服务器和数据库sample
//    String userName = "sa";  //默认用户名
//    String userPwd = "mssql";  //密码
//    java.sql.Connection con;
//    try {
//     Class.forName(driverName);
//     con = DriverManager.getConnection(dbURL,userName,userPwd);
//     System.out.println("Connection Successful!");  //如果连接成功 控制台输出Connection Successful!
//         java.sql.Statement s = con.createStatement();
//         ResultSet rs = s.executeQuery("select * from use where users=nameText");
//         while(rs.next()){
//          
//          sName=rs.getString(1);
//          sPassword=rs.getString(2);
//          System.out.println(sName);
//          System.out.println(sPassword);
//         }
//    } catch (Exception e) {
//     e.printStackTrace();
//    }
//   
// }
 /**
  * @param args the command line arguments
  */
 public static void main(String args[]) {
  java.awt.EventQueue.invokeLater(new Runnable() {
   public void run() {
    new StuLanding().setVisible(true);
   }
  });
  
 }
 
 //GEN-BEGIN:variables
 // Variables declaration - do not modify
 private javax.swing.JButton login;
 private javax.swing.JButton cancel;
 private javax.swing.JLabel jLabel1;
 private javax.swing.JLabel jLabel2;
 private javax.swing.JLabel jLabel3;
 private javax.swing.JLabel jLabel4;
 private javax.swing.JLabel jLabel5;
 private javax.swing.JPanel jPanel1;
 public javax.swing.JPasswordField jPasswordField1;
 private javax.swing.JTextField jTextField1;
 private int WIDTH=300;
 private int HEIGHT=150;
 private String url= "jdbc:sqlserver://localhost:1433; DatabaseName=his";
 private String userName="sa";
 private String Password="mssql";
 private static final String DriverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver";
 private String sjPassword;
 public String nameText;
 public char[] jPassword;
 public String nt;
 public String jpw;
 public String sName;
 public String sPassword;
 // End of variables declaration//GEN-END:variables
 
}
 
 
 
 
public class StuWindows{
                   public void static main(String[] args){
                              JFrame jf=new JFrame();
                              jf.setSize(220,300);
                               jf.setvisible(true);
 
}
}

你可能感兴趣的:(swing,职场,休闲)