和我给你的例子基本差不多 自己改改
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class LoginFrame extends JFrame{
private JTextField txtName;
private JPasswordField txtPwd;
public LoginFrame(){
setTitle("登入窗口例子");
setLayout(null);
setResizable(false);
setSize(340,250);
JPanel pane=new JPanel();
pane.setBounds(0,0,334,218);
pane.setBorder(null);
pane.setLayout(null);
add(pane);
JLabel lbl=new JLabel("欢迎登入");
lbl.setBounds(140,20,240,40);
pane.add(lbl);
JLabel lbl1=new JLabel("用户名:");
lbl1.setBounds(65,80,50,18);
pane.add(lbl1);
txtPwd=new JPasswordField();
txtPwd.setBounds(120,120,120,18);
pane.add(txtPwd);
JLabel lbl2=new JLabel("密码:");
lbl2.setBounds(80,120,30,18);
pane.add(lbl2);
txtName=new JTextField();
txtName.setBounds(120,80,120,18);
pane.add(txtName);
JButton btnOk=new JButton("确定");
btnOk.setBounds(80,180,70,30);
btnOk.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String uid=txtName.getText();
String pwd=txtPwd.getText();
if(uid.length()!=0 && pwd.length()!=0){
boolean isShow=false;
Connection con=null;
try{
con=DriverManager.getConnection("xxxx");
PreparedStatement com=con.prepareStatement("select * from login where USERNAME=? and PASSWORD=?");
com.setString(1,uid);
com.setString(2,pwd);
ResultSet res=com.executeQuery();
if(res.next()){
isShow=true;
}
res.close();
com.close();
con.close();
}catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"无法连接数据库!");
System.exit(1);
}
if(isShow){
t.setVisible(false);
new MainFrame(uid,pwd);// 跳到主窗口
}else{
JOptionPane.showMessageDialog(null,"输入有误!");
}
}else
JOptionPane.showMessageDialog(null,"输入不得为空!");
}
});
pane.add(btnOk);
JButton btnCancel=new JButton("取消");
btnCancel.setBounds(180,180,70,30);
btnCancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
pane.add(btnCancel);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation(screenSize.width / 2 - (frameSize.width / 2), screenSize.height / 2 - (frameSize.height / 2));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
private void init(){
try{
Class.forName("xxx");
}catch(Exception e){
JOptionPane.showMessageDialog(null,"无法连接数据库!");
}
}
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
public void run(){
t=new LoginFrame();
t.setVisible(true);
}
});
}
private static LoginFrame t;
}