📄 logindialog.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class LoginDialog extends JDialog
{
public String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
public String source = "jdbc:odbc:srs";
private static String stuid;
private String stupwd;
JPanel panel = new JPanel();
JLabel jlid = new JLabel();
JLabel jlpwd = new JLabel();
JTextField jtfid = new JTextField();
JPasswordField jpfpwd = new JPasswordField();
JButton jbok = new JButton();
JButton jbreg = new JButton();
BorderLayout borderLayout1 = new BorderLayout();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JComboBox jchoose = new JComboBox();
private int choose = 0;
public LoginDialog()
{
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception
{
panel.setLayout(null);
this.getContentPane().setLayout(borderLayout1);
this.setTitle("学生注册系统登录");
//this.setVisible(false);
this.setSize(320,250);
this.setResizable(false);
jlid.setFont(new java.awt.Font("Dialog", Font.PLAIN, 18));
jlid.setForeground(Color.black);
jlid.setText("学号");
jlid.setBounds(new Rectangle(51, 33, 56, 16));
jlpwd.setFont(new java.awt.Font("Dialog", Font.PLAIN, 18));
jlpwd.setForeground(Color.black);
jlpwd.setText("密码");
jlpwd.setBounds(new Rectangle(54, 72, 63, 16));
jtfid.setText("33060423");
jtfid.setBounds(new Rectangle(137, 29, 120, 22));
jpfpwd.setText("33060423");
jpfpwd.setBounds(new Rectangle(138, 69, 118, 22));
jbok.setBounds(new Rectangle(59, 151, 73, 26));
jbok.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
jbok.setText("登录");
jbok.addActionListener(new LoginDialog_jbok_actionAdapter(this));
jbreg.setBounds(new Rectangle(189, 150, 73, 26));
jbreg.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
jbreg.setText("注册");
jbreg.addActionListener(new LoginDialog_jbreg_actionAdapter(this));
jLabel1.setFont(new java.awt.Font("Dialog",Font.PLAIN,16));
jLabel1.setForeground(Color.blue);
jLabel1.setText("如果您是第一次登录,请先注册");
jLabel1.setBounds(new Rectangle(52, 194, 247, 16));
jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 16));
jLabel2.setForeground(Color.red);
jLabel2.setText("登录方式 ");
jLabel2.setBounds(new Rectangle(44, 112, 73, 16));
jchoose.setBounds(new Rectangle(138, 110, 118, 17));
jchoose.addItem("学生登录");
jchoose.addItem("管理员登录");
jchoose.addItemListener(new LoginDialog_jchoose_itemAdapter(this));
panel.add(jpfpwd, null);
panel.add(jLabel2);
panel.add(jbreg, null);
panel.add(jbok, null);
panel.add(jLabel1);
panel.add(jchoose);
panel.add(jtfid, null);
panel.add(jlid, null);
panel.add(jlpwd, null);
panel.add(jlid, null);
this.getContentPane().add(panel, java.awt.BorderLayout.CENTER);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.getRootPane().setDefaultButton(jbok);
}
public void jbok_actionPerformed(ActionEvent e)
{
char []p;
stuid = jtfid.getText().trim();
p = jpfpwd.getPassword();
stupwd = new String(p);
//System.out.println(stupwd);
try
{
Class.forName(driver);
}
catch(ClassNotFoundException ex)
{
JOptionPane.showMessageDialog(this,"没有发现驱动程序"+driver,"Error",1);
ex.printStackTrace();
System.exit(1);
}
try
{
Connection connection = DriverManager.getConnection(source);
SQLWarning warn = connection.getWarnings();
while (warn != null)
{
JOptionPane.showMessageDialog(this,warn.getMessage(),"ERROR",1);
warn = warn.getNextWarning();
}
String stusql = "select * from Student where id = '"+stuid+"'";
String adminsql = "select * from admin where id = '"+stuid+"'";
//PreparedStatement pstm = connection.prepareStatement(sql);
Statement stm = connection.createStatement();
ResultSet rs;
System.out.println(choose);
if(choose ==1 )
{
System.out.println(adminsql);
rs = stm.executeQuery(adminsql);
}
else
{
System.out.println(stusql);
rs = stm.executeQuery(stusql);
}
//System.out.println(rs);
while (rs.next())
{
String stupwdtemp = rs.getString("password");
//System.out.println(stupwdtemp+"test");
if ( !stupwdtemp.equals( stupwd) )
{
JOptionPane.showConfirmDialog(this,"密码错误,请重新输入","ERROR",JOptionPane.ERROR_MESSAGE,JOptionPane.OK_OPTION);
}
else
{
if(choose == 1)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
EditCourse editframe = new EditCourse();
editframe.setLocation( (screenSize.width -
editframe.getSize().width) / 2,
(screenSize.height -
editframe.getSize().height) / 2);
editframe.setVisible(true);
this.dispose();
}
else
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
MainFrame mainframe = new MainFrame();
mainframe.setLocation( (screenSize.width -
mainframe.getSize().width) / 2,
(screenSize.height -
mainframe.getSize().height) / 2);
mainframe.setVisible(true);
this.dispose();
}
}
}
rs.close();
stm.close();
connection.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,"在执行数据库访问时出现错误!","Error",1);
//ex.printStackTrace();
ex.printStackTrace();
System.exit(1);
}
}
public void jbreg_actionPerformed(ActionEvent e)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
RegFrame reg = new RegFrame();
//reg.setLocation(300,200);
reg.setLocation( (screenSize.width - reg.getSize().width)/2,
(screenSize.height - reg.getSize().height)/2);
reg.setVisible(true);
//this.dispose();
}
public static String getID()
{
return stuid;
}
public void jchoose_itemStateChanged(ItemEvent e) {
int b;
b = jchoose.getSelectedIndex();
choose = b;
if ( b==1)
{
jlid.setText("管理员");
jtfid.setText("admin");
jpfpwd.setText("");
}
else
{
jlid.setText("学号");
jtfid.setText("33060423");
}
}
}
class LoginDialog_jchoose_itemAdapter
implements ItemListener {
private LoginDialog adaptee;
LoginDialog_jchoose_itemAdapter(LoginDialog adaptee) {
this.adaptee = adaptee;
}
public void itemStateChanged(ItemEvent e) {
adaptee.jchoose_itemStateChanged(e);
}
}
class LoginDialog_jbreg_actionAdapter
implements ActionListener {
private LoginDialog adaptee;
LoginDialog_jbreg_actionAdapter(LoginDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jbreg_actionPerformed(e);
}
}
class LoginDialog_jbok_actionAdapter
implements ActionListener {
private LoginDialog adaptee;
LoginDialog_jbok_actionAdapter(LoginDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jbok_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -