为什么我的按钮没有反应啊?求救、、、

yeyuxp 2011-12-13 03:33:57
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;

public class Denglu extends JFrame implements
ActionListener,ItemListener //这是登录界面的开始设计
{
JLabel a1,a2,a3,a4;
JButton b1,b2,b3;
JRadioButton c1,c2;
JTextField d1;
JPasswordField e1;
ButtonGroup g1;
JPanel f2;
String s3="";
public Denglu()
{
setTitle("登录窗口");
setSize(500,500);
JPanel f1=(JPanel)getContentPane();
f1.setLayout(new FlowLayout());

a2=new JLabel("用户名: ");
a3=new JLabel("密码: ");
a4=new JLabel();//a4是为验证权限作的定义
a4.setSize(80,30);
a4.setVisible(false);//a4是不可见的
d1=new JTextField(10);
e1=new JPasswordField(10);
e1.setEchoChar('*');
c1=new JRadioButton("管理员");
c2=new JRadioButton("读者");
g1=new ButtonGroup();
g1.add(c1);g1.add(c2);
b1=new JButton("登录");
b2=new JButton("注册");
b3=new JButton("退出");

f1.add(a4); //下面加了许多的 new JLabel(" ")也是为了布局
f1.add(new JLabel(" "));
f1.add(a2);f1.add(d1);f1.add(new JLabel(" "));


f1.add(new JLabel(" "));f1.add(a3);f1.add(e1);f1.add(new JLabel(" "));
f1.add(new JLabel(" "));f1.add(c1);f1.add(c2);f1.add(new JLabel(" "));
f1.add(b1);f1.add(new JLabel(" "));f1.add(b2);f1.add(new JLabel(" "));f1.add(b3);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c1.addItemListener(this);
c2.addItemListener(this);
}



public void itemStateChanged(ItemEvent i1)
{
Connection con; //为建立连接数据库用的
Statement stmt; //为作查询用的
ResultSet rs1; //为反应结果用的
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException i3)
{ }


if(c1.isSelected()==true) //此时c1是管理员
{
s3=c1.getText(); //这里是s3获取单选按钮里面的内容
try{
con = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=shuji.mdb");
stmt=con.createStatement();
String condition=("select * from student where 权限种别='"+s3+"'"); //查询用户表里的权限种别是否有管理员
rs1=stmt.executeQuery(condition);
if(rs1.next())
{
a4.setText(s3);
}
con.close();

}catch(SQLException i4) {}
}

if(c2.isSelected()==true) //此时c2是读者
{
s3=c2.getText();//这里是s3获取单选按钮里面的内容
try
{
con = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=shuji.mdb");
stmt=con.createStatement();
String condition=("select * from student where 权限种别 ='"+s3+"'");//验证数据库里是否有读者
rs1=stmt.executeQuery(condition);
if(rs1.next())
{
a4.setText(s3);
}
con.close();
}catch(SQLException i4) {}
}
}

public void actionPerformed(ActionEvent i2)
{ Connection con;
Statement stmt;
ResultSet rs1;
String s1="",s2="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException i5)
{ }


if(i2.getSource()==b1)//登录按钮
{
s1=d1.getText();//获取用户输入的用户名内容
s2=new String(e1.getPassword());//获取用户输入的密码
s3=a4.getText();//获取用户选择的权限
try{
con = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=shuji.mdb");
stmt=con.createStatement();
String condition=("select * from student where 用户名='"+s1+"' and密码='"+s2+"' and权限种别='"+s3+"'");//获取的信息与数据库中的用户表相核对是否正确
rs1=stmt.executeQuery(condition);
if(rs1.next())
{
JOptionPane.showMessageDialog(null,"登录成功 ");

if(s1.equals("admin")==true) //如果用户名是admin(数据库里的管理员只有admin ),就进入管理员的界面 ,因为管理员和用户的权限不同
{
Admin x6=new Admin(); //调用Admin类 ***********************************
x6.setVisible(true);
//setVisible(false); //将当前登录窗体设置为不可见
}

else
{ //如果验证用户名不是admin ,就进入读者界面
Reader x2=new Reader(); //调用读者的类 **********************************
x2.setVisible(true); //设置读者的窗体为可见
}
//setVisible(false); //设置当前的窗体不可见
}
else{
JOptionPane.showMessageDialog(null, "用户名或密码或权限种别错误!!!");} //如果上面的验证信息有一项与数据库里的不符就提示登录失败
con.close();
}catch(SQLException i6) {}
}

if(i2.getSource()==b2){ //还是调用类(注册类)
Zhuce x2=new Zhuce();
x2.setVisible(true);
//setVisible(false);
}

if(i2.getSource()==b3)//退出
{
JOptionPane.showMessageDialog(null,"谢谢使用!!");
System.exit(0);
}
}
...全文
89 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeyuxp 2011-12-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chenhaijing 的回复:]
里面要判断是哪一个按钮的触发的事件:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {do what you want}
}
[/Quote]
*****************************************

if(i2.getSource()==b1)//登录按钮
{
s1=d1.getText();//获取用户输入的用户名内容
s2=new String(e1.getPassword());//获取用户输入的密码
s3=a4.getText();//获取用户选择的权限
try{
con = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=shuji.mdb");
stmt=con.createStatement();
String condition=("select * from student where 用户名='"+s1+"' and密码='"+s2+"' and权限种别='"+s3+"'");//获取的信息与数据库中的用户表相核对是否正确
rs1=stmt.executeQuery(condition);
if(rs1.next())
{
JOptionPane.showMessageDialog(null,"登录成功 ");


我设置过了。。。。
Derek-Chen 2011-12-13
  • 打赏
  • 举报
回复
设置断点,debug一下,看监听程序起到作用了没?
Derek-Chen 2011-12-13
  • 打赏
  • 举报
回复
里面要判断是哪一个按钮的触发的事件:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {do what you want}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧