import java.awt.event.*;
在和田等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需設(shè)計(jì)網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營(yíng)銷,成都外貿(mào)網(wǎng)站建設(shè),和田網(wǎng)站建設(shè)費(fèi)用合理。
import java.awt.*;
import javax.swing.*;
import java.sql.*;
public class Login extends JFrame implements ActionListener{
JButton button1,button2;
static JTextField text1,text2;
JPanel panel1,panel2,panel3,panel4,panel5;
Container con1;
int count=0;//密碼輸入次數(shù)計(jì)數(shù)器
Login()
{
super("用戶登陸");
text1=new JTextField(10);
text2=new JPasswordField(10);
button1=new JButton("提交");
button2=new JButton("取消");
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
setLocation(350,250);
con1=getContentPane();
con1.setLayout(new GridLayout(5,1));
con1.setPreferredSize(new Dimension(300,250));
panel1.add(new JLabel(""));
panel2.add(new JLabel("用戶名:"));
panel2.add(text1);
panel3.add(new JLabel("密 碼:"));
panel3.add(text2);
panel4.add(button1);
panel4.add(button2);
panel5.add(new JLabel(""));
con1.add(panel1);
con1.add(panel2);
con1.add(panel3);
con1.add(panel4);
con1.add(panel5);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ setVisible(false);
System.exit(0);
}
});
button1.addActionListener(this);
button2.addActionListener(this);
this.setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
try{登陸();}
catch(SQLException ee){}
}
else if(e.getSource()==button2)
{
退出銷售系統(tǒng)();
}
}
public void 登陸() throws SQLException
{
Database db=new Database();
if(count3)
{
if(db.check())
{
JOptionPane.showMessageDialog(this,"登陸成功!","提示對(duì)話框",JOptionPane.WARNING_MESSAGE);
setVisible(false);
MainFrame f=new MainFrame();f.pack();
}
else
{
count++;
if(count3)
{
JOptionPane.showMessageDialog(this,"登陸失敗!","提示對(duì)話框",JOptionPane.WARNING_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(this,"密碼錯(cuò)誤超過(guò)3次!","警告對(duì)話框",JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
}
else
{
JOptionPane.showMessageDialog(this,"密碼錯(cuò)誤超過(guò)3次!","警告對(duì)話框",JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
public void 退出銷售系統(tǒng)()
{
System.exit(0);
}
}
分三個(gè)包,自己建個(gè)包,導(dǎo)進(jìn)去就ok了,數(shù)據(jù)庫(kù)是access的。
package 登錄;
import java.awt.EventQueue;
public class Cilent {
private JFrame frame;
private JTextField textField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cilent window = new Cilent();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Cilent() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("登陸界面");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
JLabel lblNewLabel = new JLabel("用戶名");
lblNewLabel.setBounds(38, 43, 80, 34);
frame.getContentPane().add(lblNewLabel);
textField = new JTextField();
textField.setBounds(155, 42, 227, 37);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel label = new JLabel("密 碼");
label.setBounds(38, 115, 80, 34);
frame.getContentPane().add(label);
passwordField = new JPasswordField();
passwordField.setBounds(155, 115, 227, 37);
frame.getContentPane().add(passwordField);
JButton btnNewButton = new JButton("登 錄");
btnNewButton.setBounds(60, 187, 115, 34);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
UserCheck UC=new UserCheck(textField.getText(),String.valueOf(passwordField.getPassword()));
if(UC.getI()!=0) //有此用戶
{
frame.setVisible(false);
}
else
{
textField.setText("");
passwordField.setText("");
}
}
});
JButton button = new JButton("取 消");
button.setBounds(242, 187, 115, 34);
frame.getContentPane().add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
textField.setText("");
passwordField.setText("");
}
});
}
}
/*****************************************************************/
package 登錄;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import 操作處理.UsersCL;
/**@author 20111024
* 檢測(cè)登錄的用戶在數(shù)據(jù)庫(kù)中有無(wú),若沒(méi)有,則提示沒(méi)有此用戶,
* 若有,則判斷級(jí)別:普通用戶還是管理員。
*/
public class UserCheck {
private int i=0; //用戶級(jí)別:0不是用戶、1是管理員、2是普通用戶
UserCheck(String name ,String password)
{
String jdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String connectDB="jdbc:odbc:Students";
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
try {
Class.forName(jdriver);
con=DriverManager.getConnection(connectDB);
stmt=con.createStatement();
String query="select * from users where name='"+name+"' and passwd='"+password+"'";
rs=stmt.executeQuery(query);
if(rs.next())
{
//數(shù)據(jù)庫(kù)中有此用戶,訪問(wèn)成功
i=Integer.parseInt(rs.getString(3));
UsersCL UL=new UsersCL(i);
}
else
{
i=0; //沒(méi)有用戶是默認(rèn)是0級(jí)
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getI() {
return i;
}
}
/********************************************************************************************/
package 操作處理;
import java.awt.EventQueue;
public class UsersCL implements ActionListener{
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private int i=0;
private JLabel label_3;
private JTextField textField_4;
public UsersCL(int i) {
this.i=i;
frame = new JFrame();
frame.setTitle("用戶處理界面");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
frame.setVisible(true);
JLabel lblNewLabel = new JLabel("學(xué) 號(hào)");
lblNewLabel.setBounds(24, 32, 74, 29);
frame.getContentPane().add(lblNewLabel);
JLabel label = new JLabel("姓 名");
label.setBounds(24, 71, 74, 29);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("年 齡");
label_1.setBounds(24, 110, 74, 29);
frame.getContentPane().add(label_1);
label_3 = new JLabel("性 別");
label_3.setBounds(24, 149, 74, 29);
frame.getContentPane().add(label_3);
JLabel label_2 = new JLabel("狀 態(tài)");
label_2.setBounds(24, 195, 74, 29);
frame.getContentPane().add(label_2);
textField = new JTextField();
textField.setBounds(101, 34, 113, 25);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(101, 73, 113, 25);
frame.getContentPane().add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(101, 112, 113, 25);
frame.getContentPane().add(textField_2);
textField_3 = new JTextField();
textField_3.setEditable(false);
textField_3.setColumns(10);
textField_3.setBounds(101, 199, 288, 25);
frame.getContentPane().add(textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(101, 149, 113, 25);
frame.getContentPane().add(textField_4);
if(1==i)
{
JButton btnNewButton = new JButton("追 加");
btnNewButton.setBounds(276, 41, 113, 29);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(this);
btnNewButton.setActionCommand("追加");
JButton button_1 = new JButton("刪 除");
button_1.setBounds(276, 145, 113, 29);
frame.getContentPane().add(button_1);
button_1.addActionListener(this);
button_1.setActionCommand("刪除");
}
JButton button = new JButton("查 詢");
button.setBounds(276, 91, 113, 29);
frame.getContentPane().add(button);
button.addActionListener(this);
button.setActionCommand("查詢");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String name,age,sex,query=null;
int num,age1,count=0;
num=Integer.parseInt(textField.getText());
name=textField_1.getText();
age1=Integer.parseInt(textField_2.getText());
sex=textField_4.getText();
if(e.getActionCommand().equals("追加"))
{
query="insert into students values("+num+","+"'"+name+"',"+age1+",'"+sex+"');";
count=1;
}
else if(e.getActionCommand().equals("查詢"))
{
query="select * from students where XSB="+num+";";
count=2;
}
else if(e.getActionCommand().equals("刪除"))
{
query="delete from students where XSB="+num+" and name="+"'"+name+"'";
count=3;
}
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
String jdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String connectDB="jdbc:odbc:Students";
String query1=null;
try {
Class.forName(jdriver);
con=DriverManager.getConnection(connectDB);
stmt=con.createStatement();
if(count==1)
{
query1="select * from students where XSB="+num+";";
rs=stmt.executeQuery(query1);
if(rs.next())
textField_3.setText("已經(jīng)由此記錄,不能追加!");
else
{
stmt.executeUpdate(query);
textField_3.setText("已經(jīng)追加完成!");
}
}
else if(2==count)
{
stmt.executeQuery(query);
rs=stmt.executeQuery(query);
if(rs.next())
{
textField_3.setText("已查找到此記錄!");
}
else
{
textField_3.setText("沒(méi)有此記錄,可以追加!");
}
}
else if(3==count)
{
query1="select * from students where XSB="+num+" and name="+"'"+name+"'";
rs=stmt.executeQuery(query1);
if(rs.next())
{
stmt.executeUpdate(query);
textField_3.setText("已刪除此記錄!");
}
else
textField_3.setText("無(wú)此記錄!");
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
//關(guān)閉資源
if(stmt!=null){
try {
stmt.close();
} catch (Exception e2) {
// TODO: handle exception
}
stmt=null;
}
if(con!=null){
try {
con.close();
} catch (Exception e2) {
// TODO: handle exception
}
con=null;
}
}
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener
{
JLabel lbl1=new JLabel("用戶名");
JLabel lbl2=new JLabel("密碼");
JTextField txt=new JTextField(15);
JPasswordField pf=new JPasswordField();
JButton btn1=new JButton("確定");
JButton btn2=new JButton("取消");
public LoginFrm()
{
this.setTitle("登陸");
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(new GridLayout(3,2,10,10));
jp.add(lbl1);jp.add(txt);
jp.add(lbl2);jp.add(pf);
jp.add(btn1);jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:MyDB","","");
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select * from loginAndpassword where login='"+txt.getText()+"' and password='"+pf.getText()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"登陸成功!");
}
else
JOptionPane.showMessageDialog(null,"用戶名或密碼錯(cuò)誤!");
} catch(Exception ex){}
if(ae.getSource()==btn2)
{
txt.setText("");
pf.setText("");
}
}
}
public static void main(String arg[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
LoginFrm frm=new LoginFrm();
frm.setSize(400,200);
frm.setVisible(true);
}
}
你這個(gè)不用數(shù)據(jù)庫(kù)真的是有點(diǎn)難搞
我寫了個(gè)用集合存儲(chǔ)的,你看看,能否幫上你
java.util.ListString?list?=?new?ArrayListString();
list.add("qq=123");//存儲(chǔ)的時(shí)候用(用戶名=密碼)的形式
list.add("ww=456");
String?username?=?"qq";
String?password?=?"123";
for?(int?i?=?0;?i??list.size();?i++)?{
String?num?=?username?+"="+password;
if(num.equals(list.get(i))){
System.out.println("登錄成功");
break;
}
}