📄 loginframe.java
字号:
//以下是布局GUI的类
//package myprojects.main;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
class LoginFrame extends JFrame
{
private JButton loginButton ;
private Connection loginconnection;
private Statement loginstatement;
private ResultSet loginresultSet;
public static JTextField userNameTextField;//声明登陆名为全局变量
private JPasswordField passwordField;
private JComboBox 登陆列表;
private String[] 登陆模式;
private int login = 0 ;
private static String 登陆类型="学生登陆";
public LoginFrame()
{
setTitle("登录窗口");
setSize(300,200);
setResizable(false);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setLocation(350,200);
GridBagLayout layout = new GridBagLayout();
Container contents = getContentPane();
contents.setLayout(layout);
GridBagConstraints constraints = new GridBagConstraints();
userNameTextField = new JTextField(); //needed below
userNameTextField.setPreferredSize(new Dimension(120, 25));
// user name label
JLabel userNameLabel = new JLabel();
//userNameLabel.setDisplayedMnemonic("用户名");
userNameLabel.setLabelFor(userNameTextField);
userNameLabel.setText("用户帐号");
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
contents.add(userNameLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
constraints.gridwidth = 2;
constraints.gridheight = 1;
contents.add(userNameTextField, constraints);
passwordField = new JPasswordField(); // needed below
passwordField.setPreferredSize(new Dimension(120, 25));
// password label
JLabel passwordLabel = new JLabel();
passwordLabel.setText("登陆口令");
passwordLabel.setLabelFor(passwordField);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
contents.add(passwordLabel, constraints);
// password field
constraints.gridx = 1;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.gridheight = 1;
contents.add(passwordField, constraints);
String[] 登陆模式 ={"学生登陆","教师登陆","系统维护"};
登陆列表 = new JComboBox(登陆模式);
登陆列表.setPreferredSize(new Dimension(120, 25));
JLabel loginLabel = new JLabel();
loginLabel.setText("登陆类型");
loginLabel.setLabelFor(登陆列表);
//判断登陆类型,以便进入不同的用户界面
登陆列表.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
登陆类型 = (String)登陆列表.getSelectedItem();
// System.out.println(登陆类型);
}
});
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
contents.add(loginLabel, constraints);
// 登陆模式
constraints.gridx = 1;
constraints.gridy = 2;
constraints.gridwidth = 2;
constraints.gridheight = 1;
contents.add(登陆列表, constraints);
JPanel buttonPanel = createButtonPanel(); // sets global loginButton
constraints.gridx = 0;
constraints.gridy = 3;
constraints.gridwidth = 3;
constraints.gridheight = 1;
contents.add(buttonPanel, constraints);
}
private JPanel createButtonPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, 0));
// login button (global variable)
loginButton = new JButton();
loginButton.setText("确 定");
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
logindispose();
if(login == 1)
{
if(登陆类型.equals("教师登陆"))
{
JFrame f=new TeacherFrame();
f.show();
dispose();
}
else if(登陆类型.equals("系统维护"))
{
JFrame f=new ManagerFrame();
f.show();
dispose();
}
else //(登陆类型.equals("学生登陆"))
{
JFrame f=new StudentFrame();
f.show();
dispose();
}
}
}
});
panel.add(loginButton);
// space
panel.add(Box.createRigidArea(new Dimension(5,0)));
// cancel button
JButton cancelButton = new JButton();
cancelButton.setText("取 消");
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
dispose();
System.exit(0);
}
});
panel.add(cancelButton);
// space
panel.add(Box.createRigidArea(new Dimension(5,0)));
// help button
JButton helpButton = new JButton();
helpButton.setText("About");
helpButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event) {
initAboutDialog();
}
});
panel.add(helpButton);
Vector buttons = new Vector(3);
buttons.add(cancelButton);
buttons.add(helpButton);
buttons.add(loginButton);
buttons.removeAllElements(); // simplify gc
return panel;
} // createButtonPanel()
private void initAboutDialog()
{
JOptionPane.showMessageDialog( this,"应用数学系2002级信息管理与信息系统\n 黄振威","关于作者",JOptionPane.INFORMATION_MESSAGE );
return;
}
private void logindispose()
{
String url = "jdbc:odbc:java"; //数据源名字为java
String username = "sa";
String password = "";
//加载驱动程序以连接数据库
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
loginconnection = DriverManager.getConnection( url, username, password );
}
//捕获加载驱动程序异常
catch ( ClassNotFoundException cnfex )
{
JOptionPane.showMessageDialog ( this ,cnfex ,
"学生选课管理系统",JOptionPane.WARNING_MESSAGE );
System.exit( 1 ); // terminate program
}
//捕获连接数据库异常
catch ( SQLException sqlex )
{
//sqlex.printStackTrace();
JOptionPane.showMessageDialog ( this ,"无法连接到SQL SERVER ,\n请确认SQL SERVER是否运行\n或数据源设置是否正确! " ,
"学生选课管理系统",JOptionPane.WARNING_MESSAGE );
System.exit( 1 ); // terminate program
}
try
{
String loginquery;
String loginusename = userNameTextField.getText();
String loginpassword = passwordField.getText();
if(userNameTextField.getText().equals( "" ))
{
JOptionPane.showMessageDialog( this,"用户名必须为字母、数字和、汉字\n及其组合,不允许为空格键.",
"登陆 ",JOptionPane.WARNING_MESSAGE );
//setTitle( "无记录显示" );
return;
}
if(登陆类型.equals("教师登陆"))
loginquery = "SELECT * FROM 教师表 WHERE(登陆帐号='"+loginusename +"' AND 登陆密码 ='"+loginpassword+"')";
else if(登陆类型.equals("系统维护"))
loginquery = "SELECT * FROM 管理员 WHERE(用户名='"+loginusename +"' AND 密码 ='"+loginpassword+"')";
else //(登陆类型.equals("学生登陆"))
loginquery = "SELECT * FROM 学生基本信息表 WHERE(学号='"+loginusename +"' AND 密码 ='"+loginpassword+"')";
loginstatement = loginconnection.createStatement();
loginresultSet = loginstatement.executeQuery( loginquery );
boolean Records = loginresultSet.next();
if ( ! Records )
{
JOptionPane.showMessageDialog( this,"没有此用户或密码错误" );
//return;
}
else
{
login = 1 ;
}
loginconnection.close();
}
catch(SQLException sqlex)
{
//sqlex.printStackTrace();
JOptionPane.showMessageDialog ( this ,sqlex ,
"学生选课管理系统",JOptionPane.WARNING_MESSAGE );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -