?? userlogin.java
字號:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserLogin extends JFrame implements ActionListener {
DataBaseManager db=new DataBaseManager();
JButton jButton2;
ResultSet rs;
JRadioButton jRadioButton4;
JLabel jLabel5;
JLabel jLabel4;
JLabel jLabel2,jLabel3;
JRadioButton jRadioButton3;
JRadioButton jRadioButton2;
JRadioButton jRadioButton1;
JButton jButton3;
JButton jButton1;
JLabel jLabel1;
JTextField jTextField1;
JPasswordField jPasswordField1;
public UserLogin(){
this.setBounds(431, 223, 356, 225);
this.setLayout(null);
this.setTitle("用戶登陸");
this.setResizable(false); //固定窗口大小
this.setVisible(true); //顯示面板
{
jLabel2 = new JLabel();
getContentPane().add(jLabel2);//獲得窗口的面板,用來添加內(nèi)容
jLabel2.setText("用戶名:");
jLabel2.setBounds(56, 56, 49, 15);
}
{
jTextField1 = new JTextField();
getContentPane().add(jTextField1);
jTextField1.setText("");
jTextField1.addActionListener(this);
jTextField1.setBounds(102, 51, 150, 24);
jTextField1.grabFocus(); //顯示用戶文本框的光標
}
{
jLabel3 = new JLabel();
getContentPane().add(jLabel3);
jLabel3.setText("密碼:");
jLabel3.setBounds(62, 91, 41, 15);
}
{
jPasswordField1 = new JPasswordField();
getContentPane().add(jPasswordField1);
jPasswordField1.setText("");
jPasswordField1.addActionListener(this);
jPasswordField1.setEchoChar('*'); //設(shè)置輸入用戶密碼為
jPasswordField1.setBounds(102, 87, 150, 22);
}
{
jButton2 = new JButton();
jButton2.addActionListener(this);
getContentPane().add(jButton2);
jButton2.setForeground(Color.RED);
jButton2.setText("取消");
jButton2.setBounds(136, 148, 62, 22);
}
{
jLabel1 = new JLabel();
getContentPane().add(jLabel1);
jLabel1.setText("jLabel1");
jLabel1.setIcon(new ImageIcon(getClass().getClassLoader().getResource("logina.jpg")));
jLabel1.setBounds(-6, -14, 363, 80);
}
{
jButton1 = new JButton();
getContentPane().add(jButton1);
jButton1.addActionListener(this);
jButton1.setText("jButton1");
jButton1.setBounds(61, 148, 48, 22);
jButton1.setIcon(new ImageIcon(getClass().getClassLoader().getResource("登錄.gif")));
}
{
jButton3 = new JButton();
getContentPane().add(jButton3);
jButton3.addActionListener(this);
jButton3.setText("注冊");
jButton3.setBounds(226, 148, 48, 22);
jButton3.setIcon(new ImageIcon(getClass().getClassLoader().getResource("注冊5.gif")));
}
{
jRadioButton1 = new JRadioButton();
getContentPane().add(jRadioButton1);
jRadioButton1.setText("系統(tǒng)管理員");
jRadioButton1.addActionListener(this);
jRadioButton1.setBounds(25, 117, 95, 19);
}
{
jRadioButton2 = new JRadioButton();
getContentPane().add(jRadioButton2);
jRadioButton2.setText("統(tǒng)計員");
jRadioButton2.addActionListener(this);
jRadioButton2.setBounds(120, 117, 67, 18);
}
{
jRadioButton3 = new JRadioButton();
getContentPane().add(jRadioButton3);
jRadioButton3.setText("錄入員");
jRadioButton3.addActionListener(this);
jRadioButton3.setBounds(193, 117, 65, 19);
}
{
jRadioButton4 = new JRadioButton();
getContentPane().add(jRadioButton4);
jRadioButton4.setText("學生");
jRadioButton4.addActionListener(this);
jRadioButton4.setBounds(270, 117, 60, 19);
}
ButtonGroup group = new ButtonGroup();
group.add(jRadioButton1);
group.add(jRadioButton2);
group.add(jRadioButton3); //把單選框按鈕放在同一個組按鈕中
group.add(jRadioButton4);
{
jLabel4 = new JLabel();
getContentPane().add(jLabel4);
jLabel4.setText("jLabel4");
jLabel4.setBounds(-372, 228, 1326, 473);
jLabel4.setIcon(new ImageIcon(getClass().getClassLoader().getResource("mww.jpg")));
}
{
jLabel5 = new JLabel();
getContentPane().add(jLabel5);
jLabel5.setText("jLabel5");
jLabel5.setIcon(new ImageIcon(getClass().getClassLoader().getResource("mww.jpg")));
jLabel5.setBounds(-41, 60, 1326, 700);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jButton2)
{
System.exit(0);
}
if(e.getSource()==jButton3){
int result=JOptionPane.showOptionDialog(null, "是否要注冊新用戶?", "系統(tǒng)提示",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, new String[] {"是","否"}, "否"); //彈出相應消息對話框
if(result==JOptionPane.YES_OPTION){
new UserRegrster();
this.dispose();
}
else{
new UserLogin();
this.dispose();
}
}
else
{
if(e.getSource()==jButton1){
if(jTextField1.getText().trim().equals(""))
{
JOptionPane.showOptionDialog(this,"用戶不能為空", "錯誤信息" ,
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
null, null, null); //彈出相應消息對話框
jTextField1.requestFocus();//焦點放置在用戶名文本框中
return;
}
if(jPasswordField1.getText().trim().equals(""))
{
JOptionPane.showOptionDialog(this,"密碼不能為空", "錯誤信息" ,
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
null, null, null); //彈出相應消息對話框
jPasswordField1.requestFocus();//焦點放置在密碼文本框中
return;
}
String strSQL;
//SQL語句根據(jù)用戶名和密碼獲得結(jié)果集
strSQL="select * from [user1] where UserName='"+
jTextField1.getText().trim()+"'and PassWord='"+
jPasswordField1.getText().trim()+"'";
rs=db.getResult(strSQL);
boolean isExist=false;
try
{
if(isExist=rs.first()) {
if(jRadioButton1.getText().equals("系統(tǒng)管理員")){
new Administrator();
this.setExtendedState(Administrator.MAXIMIZED_BOTH); //顯示最大的窗口界面
this.dispose(); //自動消失
}
else if(jRadioButton2.getText().equals("統(tǒng)計員")) {
new Statistician();
this.setExtendedState(Administrator.MAXIMIZED_BOTH);
this.dispose();
}
else if(jRadioButton3.getText().equals("錄入員")){
new Inputer();
this.setExtendedState(Administrator.MAXIMIZED_BOTH);
this.dispose();
}
else if(jRadioButton1.getText().equals("學生")){
new Students();
this.setExtendedState(Administrator.MAXIMIZED_BOTH);
this.dispose();
}
}
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
}
if(!isExist)
{
JOptionPane.showOptionDialog(this,"用戶名不存在或者密碼不正確!", "錯誤信息" ,
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
null, null, null); //彈出相應消息對話框
jTextField1.setText(null);//將用戶文本框置為空
jPasswordField1.setText(null);//將密碼文本框置為空
jTextField1.requestFocus();//焦點放置在用戶名文本框中
}
}
}
}
public static void main(String[] args) {
new UserLogin();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -