📄 login.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class Login extends JFrame {
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JPanel panel;
private JTextField textField;
private JPasswordField passwordField;
private String user,pwd,type;
private boolean found = false;
private int fault;
private JLabel label1,label2;
private JButton login,logout;
public Login()
{
super( "登录" );
container = getContentPane();
layout = new GridBagLayout();
constraints = new GridBagConstraints();
panel = new JPanel();
panel.setLayout( layout );
label1 = new JLabel( "用户名" );
label2 = new JLabel( "密码" );
textField = new JTextField( 12 );
passwordField = new JPasswordField( 12 );
login = new JButton( "登录" );
logout = new JButton( "退出" );
// JRadioButton manager = new JRadioButton( "管理员", true );
// JRadioButton seller = new JRadioButton( "售货员", false );
addComponent( label1, 0, 0, 4, 1 );
addComponent( label2, 1, 0, 4, 1 );
addComponent( textField, 0, 4, 6, 1 );
addComponent( passwordField, 1, 4, 6, 1 );
// addComponent( manager, 2, 0, 5, 1 );
// addComponent( seller, 2, 5, 5, 1 );
addComponent( login, 3, 0, 5, 1 );
addComponent( logout, 3, 5, 5, 1 );
// ButtonGroup radioGroup = new ButtonGroup();
// radioGroup.add( manager );
// radioGroup.add( seller );
login.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
user = textField.getText();
pwd = new String( passwordField.getPassword() );
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:access";
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="select * from User";
ResultSet rs = statement.executeQuery(sql);
while( rs.next() ){
if( user.compareTo( "" ) == 0 ) fault = 0;
else if( pwd.compareTo( "" )==0 ) fault = 1;
else if( user.compareTo( rs.getString( "User_id" ) ) == 0 ){
if( pwd.compareTo( rs.getString( "Password" ) ) == 0 ){
type = rs.getString( "User_type" );
found = true;
}
else fault = 2;
}
else fault = 3;
}
if( found ){
if( type.compareTo( "M" ) == 0 )
Manager1.main( null );
else if( type.compareTo( "S" ) == 0 ) Sell.main( null );
Login.this.setVisible( false );
}
else if( fault == 0 ){
JOptionPane.showMessageDialog( null,"请输入用户名","错误",JOptionPane.ERROR_MESSAGE );
passwordField.setText("");
}
else if( fault == 1 ){
JOptionPane.showMessageDialog( null,"请输入密码","错误",JOptionPane.ERROR_MESSAGE );
passwordField.setText("");
}
else if( fault == 2 ){
JOptionPane.showMessageDialog( null,"密码错误,请重输","错误",JOptionPane.ERROR_MESSAGE );
passwordField.setText("");
}
else if( fault == 3 ){
JOptionPane.showMessageDialog( null,"用户名不存在,请重输","错误",JOptionPane.ERROR_MESSAGE );
textField.setText( null );
passwordField.setText( null );
}
rs.close();
connection.close();
}
catch(Exception ex){
System.out.println(ex);
}
}
}
);
logout.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
System.exit( 0 );
}
}
);
container.add( panel );
setSize( 500,500 );
setVisible( true );
}
private void addComponent( Component component, int row,
int column, int width, int height )
{
constraints.gridx = column;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints( component, constraints );
panel.add( component );
}
public static void main( String args[] )
{
Login application = new Login();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -