?? login.java
字號(hào):
/**************************************************
*文件名: Login.java
*功能: 用戶登陸界面
***************************************************/
import java.sql.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
class Login extends JFrame
{
private JButton okButton = new JButton("確定");
private JButton cancelButton = new JButton("取消");
private JPanel buttonPanel = new JPanel();
private JLabel userLabel = new JLabel("用戶名");
private JLabel passLabel = new JLabel("口令");
private JPanel labelPanel = new JPanel();
private JTextField userNameTextField = new JTextField(10);
private JPasswordField passwordField = new JPasswordField(10);
private JPanel txtPanel = new JPanel();
private JPanel label_txt_panel = new JPanel();
private int login = 0 ;
private conDB conIns;
public Login(conDB conapp)
{
this.conIns = conapp;
setTitle("登錄窗口");
setSize(250,150);
setResizable(false);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setLocation(350,200);
//"確定"按鈕事件監(jiān)聽(tīng)
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
//事件處理函數(shù)
loginOperate();
if(login == 1)
{
//用戶名和口令正確,打開(kāi)主界面
JFrame mainGUI = new createGUI(conIns);
mainGUI.show();
dispose();
}
}
});
//"取消"按鈕事件監(jiān)聽(tīng)
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
//直接退出程序
System.exit(0);
}
});
//以下是界面設(shè)置
buttonPanel.setLayout(new GridLayout(1,2));
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
labelPanel.setLayout(new GridLayout(2,1));
labelPanel.add(userLabel);
labelPanel.add(passLabel);
txtPanel.setLayout(new GridLayout(2,1));
txtPanel.add(userNameTextField);
txtPanel.add(passwordField);
label_txt_panel.setLayout(new GridLayout(1,2));
label_txt_panel.add(labelPanel);
label_txt_panel.add(txtPanel);
Container contentPane=getContentPane();
contentPane.add(label_txt_panel,"Center");
contentPane.add(buttonPanel,"South");
}
//"確定"按鈕的事件處理函數(shù)
private void loginOperate() {
String loginquery;
String loginusename = userNameTextField.getText();
String loginpassword = passwordField.getText();
try
{
loginquery = "select * from login where (username='"+loginusename +
"' and password = '"+loginpassword+"')";
conIns.stm = conIns.con.createStatement();
conIns.rs = conIns.stm.executeQuery( loginquery );
boolean Records = conIns.rs.next();
if ( ! Records )
{
//彈出消息框
JOptionPane.showMessageDialog( this,"登錄失敗" );
//清空文本框,等待用戶的再次輸入
userNameTextField.setText("");
passwordField.setText("");
return;
}
else
{
login = 1 ;
}
//conIns.con.close();
}
catch(SQLException sqlex)
{
sqlex.printStackTrace();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -