📄 login.java
字号:
package com.vbank;
import com.vbank.admin.WorkBench;
import com.vbank.db.*;
import com.vbank.operate.ServiceFrame;
import javax.swing.SwingUtilities;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Color;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Login extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JTextField nameField = null;
private JLabel jLabel2 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JTextField passwordField = null;
private JLabel errorLabel = null;
private JRadioButton jRadioButton = null;
private JLabel jLabel3 = null;
private JRadioButton jRadioButton1 = null;
private JLabel jLabel4 = null;
/**
* This method initializes jLabel
*
* @return javax.swing.JLabel
*/
private JLabel getJLabel() {
if (jLabel == null) {
jLabel = new JLabel();
jLabel.setText(" 登录");
jLabel.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setBounds(new Rectangle(78, 23, 186, 18));
}
return jLabel;
}
/**
* This method initializes nameField
*
* @return javax.swing.JTextField
*/
private JTextField getNameField() {
if (nameField == null) {
nameField = new JTextField();
nameField.setBounds(new Rectangle(133, 97, 122, 22));
}
return nameField;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setFont(new Font("Dialog", Font.BOLD, 12));
jButton.setHorizontalTextPosition(SwingConstants.CENTER);
jButton.setLocation(new Point(63, 196));
jButton.setSize(new Dimension(67, 29));
jButton.setText("登录");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(e.getSource().equals(jButton)) {
//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
String name=nameField.getText().trim();
String pwd=passwordField.getText();
String isadmin="n";
if(jRadioButton.isSelected())
isadmin="y";
if(name!=null&&pwd!=null) {
int f=verifyUser(name,pwd,isadmin);
closeLoginFrame();
switch(f) {
case -1:{reEnter("帐号或密码错误!");break;}
case 0:{
String slq="select * from operator where operatenum='"+name+"' and operatepwd='"+pwd+"' and isadmin='y'";
int t=verifyUser(name,pwd,"y");
if(t==0)
new WorkBench(name,isadmin ).setVisible(true);
else reEnter("请输入管理员帐号!");
break;}
case 1:new ServiceFrame(name,isadmin).setVisible(true);
}
}
else reEnter("请输入帐号和密码!");
}
}
});
}
return jButton;
}
private void closeLoginFrame() {
this.dispose();
}
/**
*
*/
private int verifyUser(String name,String pwd,String isadmin) {
int flag=-1;
if(name!=""&&pwd!=""){
String sql="select * from operator where operateNum='"
+name+"' and operatePwd='"+pwd+"'and isadmin='"+isadmin+"'";
//System.out.println(sql);
DBManager.getConnection();
ResultSet rs=DBManager.executeSelect(sql);
try {
if(rs.next()) {//System.out.println(rs.getString("isadmin").equalsIgnoreCase("y"));
if(rs.getString("isadmin").equalsIgnoreCase("y"))
flag=0;
else if(rs.getString("isadmin").equalsIgnoreCase("n"))
flag=1;
}
} catch (SQLException e1) {
// TODO 自动生成 catch 块
System.out.println("connection error");
e1.printStackTrace();
}
}
return flag;
}
private void reEnter(String err) {
this.repaint();
this.setVisible(true);
errorLabel.setText(err);
errorLabel.setVisible(true);
}
public void paint(Graphics g) {
super.paintComponents(g);
this.setVisible(true);
errorLabel.setText("请输入帐号和密码!");
errorLabel.paintImmediately(errorLabel.getBounds());
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
//this.setVisible(false);
errorLabel.setText("请输入帐号和密码!");
errorLabel.setVisible(true);
errorLabel.paintImmediately(errorLabel.getBounds());
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(134, 197, 71, 29));
jButton1.setHorizontalTextPosition(SwingConstants.LEFT);
jButton1.setText("重置");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
nameField.setText("");
passwordField.setText("");
}
});
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(210, 197, 76, 29));
jButton2.setPreferredSize(new Dimension(65, 29));
jButton2.setText("取消");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
closeLoginFrame();
}
});
}
return jButton2;
}
/**
* This method initializes passwordField
*
* @return javax.swing.JPasswordField
*/
private JTextField getPasswordField() {
if (passwordField == null) {
passwordField = new JPasswordField();
passwordField.setBounds(new Rectangle(135, 137, 119, 22));
}
return passwordField;
}
/**
* This method initializes jRadioButton
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioButton() {
if (jRadioButton == null) {
jRadioButton = new JRadioButton();
jRadioButton.setBounds(new Rectangle(74, 171, 21, 21));
}
return jRadioButton;
}
/**
* This method initializes jRadioButton1
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioButton1() {
if (jRadioButton1 == null) {
jRadioButton1 = new JRadioButton();
jRadioButton1.setBounds(new Rectangle(185, 172, 21, 21));
jRadioButton1.setSelected(true);
}
return jRadioButton1;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Login thisClass = new Login();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public Login() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(360, 289);//在这里可以取显示器大小,使登陆界面居中
int w=Toolkit.getDefaultToolkit().getScreenSize().width;
int h=Toolkit.getDefaultToolkit().getScreenSize().height;
int x=(w-getSize().width)/2;
int y=(h-getSize().height)/2;
this.setLocation(x, y);
//this.setBounds(150, 100, 360, 289);
this.setContentPane(getJContentPane());
this.setTitle("欢迎使用本系统,请登录");
this.setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel4 = new JLabel();
jLabel4.setBounds(new Rectangle(208, 174, 66, 15));
jLabel4.setText("非管理员");
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(99, 173, 46, 15));
jLabel3.setText("管理员");
errorLabel = new JLabel();
errorLabel.setBounds(new Rectangle(98, 61, 133, 18));
errorLabel.setForeground(new Color(242, 1, 3));
errorLabel.setText("JLabel");
errorLabel.setVisible(false);
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(62, 138, 66, 18));
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setText("密码:");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(62, 98, 67, 18));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("帐号:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJLabel(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(getNameField(), null);
jContentPane.add(jLabel2, null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJButton2(), null);
jContentPane.add(getPasswordField(), null);
jContentPane.add(errorLabel, null);
jContentPane.add(getJRadioButton(), null);
jContentPane.add(jLabel3, null);
jContentPane.add(getJRadioButton1(), null);
jContentPane.add(jLabel4, null);
ButtonGroup bg=new ButtonGroup();
bg.add(getJRadioButton());
bg.add(getJRadioButton1());
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="10,10"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -