点击“登录”,“登录”界面不能跳转出来(小弟菜鸟,求高手帮忙)
LoginInte易做图ce.java;import java.sql.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class LoginInte易做图ce {
public static void main(String args[])
{
Login login=new Login();
login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Login extends JFrame
{
JPanel contentPane;
JLabel msg,label1,label2;
JTextField tf;
JPasswordField pf;
JButton btn1,btn2;
Connection con;
Statement stmt;
ResultSet rs;
String ssql;
//PreparedStatement ps;
Login()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// con=DriverManager.getConnection("jdbc:odbc:library");
String dbur1 = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:\\library.mdb";
con=DriverManager.getConnection(dbur1);
stmt=con.createStatement();
ssql="SELECT * FROM logintable";
rs=stmt.executeQuery(ssql);
} catch(SQLException e1)
{
msg.setText("数据库连接有误!");
}
catch (ClassNotFoundException e2) {
msg.setText(e2.getMessage());
};
contentPane=(JPanel)getContentPane();
msg=new JLabel(" ");
label1=new JLabel("用户名:");
label2=new JLabel("密 码:");
tf=new JTextField(15);
pf=new JPasswordField(15);
btn1=new JButton("登录");
btn2=new JButton("注册");
label1.setFont(new Font("宋体",Font.BOLD,20));
label2.setFont(new Font("宋体",Font.BOLD,20));
btn1.setFont(new Font("宋体",Font.BOLD,20));
btn2.setFont(new Font("宋体",Font.BOLD,20));
msg.setFont(new Font("宋体",Font.BOLD,20));
tf.setFont(new Font("宋体",Font.BOLD,20));
pf.setFont(new Font("宋体",Font.BOLD,20));
contentPane.setLayout(new FlowLayout());
contentPane.add(label1);
contentPane.add(tf);
contentPane.add(label2);
contentPane.add(pf);
contentPane.add(btn1);
contentPane.add(btn2);
contentPane.add(msg);
btn1.addActionListener(new ButtonAct());
btn2.addActionListener(new ButtonAct());
addWindowListener(new WindowClose());
setVisible(true);
setSize(300,200);
setTitle("登录界面");
}
class WindowClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
(e.getWindow()).dispose();
System.exit(0);
}
}
class ButtonAct implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
String command=e.getActionCommand();
char pwd[]=new char[40];
pwd=pf.getPassword();
String username=tf.getText();
String password=String.valueOf(pwd);
if(command.equals("登录"))
{
//判断用户名是否为空
if (username.length() == 0||password.length()==0)
{
if(username.length() == 0)
msg.setText("用户名不能为空!");
else
msg.setText("密码不能为空!");
}
//判断密码是否为空
//else if (pwd[0] == '\0') //字符数组都是以“\0”结束(方法一)
/* else if(password.length()==0)//方法二
{
msg.setText("密码不能为空!");
}*/
else
{
while(rs.next())
{
if(username==rs.getString(1)&&password==rs.getString(2))
{
msg.setText("成功登录");
Permissions_Frame frame=new Permissions_Frame();
frame.setVisible(true);
frame.setSize(300,200);
}
}
}
}//if
else
if(command.equals("注册"))
{
msg.setText("进入注册界面!");
MenuFrame1 frame1=new MenuFrame1();
frame1.setVisible(true);
frame1.setSize(300,200);
}
}
catch(SQLException e3)
{
msg.setText("数据库连接有误!");
};
}
}
}
MenuFrame.java;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.FlowLayout;
class MenuFrame extends JFrame
{
JPanel contentPane;
JLabel label1,label2,label3,msg;
JButton btn1,btn2,btn3;
MenuFrame()
{
contentPane=(JPanel) getContentPane();
contentPane.setLayout(new FlowLayout());
label1.setText("老师身份");
label2.setText("学生身份");
label3.setText("管理员身份");
msg.setText("");
btn1.setText("Teacher");
btn2.setText("Student");
btn3.setText("Manager");
contentPane.add(label1);
contentPane.add(btn1);
contentPane.add(label2);
contentPane.add(btn2);
contentPane.add(label3);
contentPane.add(btn3);
btn1.addActionListener(new Btn_Act());
btn2.addActionListener(new Btn_Act());
btn3.addActionListener(new Btn_Act());
addWindowListener(new WindowClose());
setTitle("权限选择界面");
setSize(300,200);
setVisible(true);
}
class WindowClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
(e.getWindow()).dispose();
System.exit(0);
}
}
class Btn_Act implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String command=e.getActionCommand();
if(command.equals("教师身份"))
{
msg.setText("您选择的是教师界面!");
}
else
if(command.equals("学生身份"))
{
msg.setText("您选择的是学生界面!");
}
else
if(command.equals("管理员身份"))
{
msg.setText("您选择的是管理员界面!");
}
}
}
}
MenuFrame1.java;
import javax.swing.JFrame;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
class MenuFrame1 extends JFrame{
JPanel contentPane,p1,p2,p3,p4;
JLabel label1,label2,msg;
JTextField tf;
JPasswordField pf;
JButton btn;
Connection cn;
PreparedStatement ps;
String ssql;
MenuFrame1()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dbur1 = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:\\library.mdb";
cn=DriverManager.getConnection(dbur1);
ssql="INSERT INTO logintable VALUES(?,?)";
ps=cn.prepareStatement(ssql);
} catch(SQLException e1)
{msg.setText("数据库连接有误!");}
catch (ClassNotFoundException e2) {
msg.setText(e2.getMessage());
};
contentPane=(JPanel) getContentPane();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
label1=new JLabel("用户名");
label2=new JLabel("密 码");
msg=new JLabel(" ");
tf=new JTextField(15);
pf=new JPasswordField(15);
btn=new JButton("注册");
p1.add(label1);
p1.add(tf);
p2.add(label2);
p2.add(pf);
p3.add(btn);
p4.add(msg);
contentPane.setLayout(new FlowLayout());
contentPane.add(p1);
contentPane.add(p2);
contentPane.add(p3);
contentPane.add(p4);
btn.addActionListener(new Btn_Act());
addWindowListener(new WindowClose());
}
class WindowClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
try{
cn.commit();
cn.close();
}catch(Exception e3)
{};
(e.getWindow()).dispose();
System.exit(0);
}
}
class Btn_Act implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//char pwd[]=new char[40];
//pwd=pf.getPassword();
//String username=tf.getText();
//String password=String.valueOf(pwd);
try
{
ps.setString(1,tf.getText());
ps.setString(2,String.valueOf(pf.getPassword()));
ps.executeUpdate();
msg.setText("记录插入成功!");
tf.setText("");
pf.setText("");
tf.requestFocus();
}catch(Exception e4){
msg.setText("输入数据有误!");
tf.requestFocus();
};
}
}
}
补充:Java , Java相关