数据库课程设计_实验室管理系统_登录

来源:互联网 发布:百无一用是书生知乎 编辑:程序博客网 时间:2024/06/06 12:25
package labor;import java.awt.BorderLayout;import java.awt.Choice;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import java.awt.Panel;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;import javax.swing.SwingConstants;public class Load_Labor extends JFrame implements SwingConstants{private String select="管理员";String nameresult[]=new String[10];String passresult[]=new String[50];boolean Flag=false;private JLabel welcomela=null;private JLabel userla=null;private JLabel passwordla=null;private JLabel power=null;private JComboBox jcb=null;private JTextField userjt=null;private JPasswordField passwordjp=null;private JButton loadbtn=null;private JButton canclebtn=null;private JPanel inputJp=null;private JPanel btnJp=null;private JPanel panel=null;private JPanel imagePanel=null;private ImageIcon background=null;public Load_Labor(){super("用户登陆");init();}public void init(){this.setLayout(new FlowLayout());inputJp=new JPanel();btnJp=new JPanel();inputJp.setLayout(new GridLayout(3,2));welcomela=new JLabel("欢迎使用实验室管理系统");welcomela.setFont(new Font("欢迎使用实验室管理系统",1, 16));welcomela.setHorizontalAlignment(JLabel.CENTER);welcomela.setForeground(Color.RED);userla=new JLabel("用户名:");passwordla=new JLabel("密    码:");power=new JLabel("权限:");userjt=new JTextField(10);passwordjp=new JPasswordField(10);jcb=new JComboBox();jcb.addItem("管理员");jcb.addItem("访客");loadbtn=new JButton("登陆");canclebtn=new JButton("退出");inputJp.add(userla,0);inputJp.add(userjt,1);inputJp.add(passwordla,2);inputJp.add(passwordjp,3);inputJp.add(power,4);inputJp.add(jcb,5);btnJp.add(loadbtn);btnJp.add(canclebtn);panel=new JPanel(new FlowLayout());panel.add(inputJp);panel.add(btnJp);background = new ImageIcon("E:/workspace/database/src/ImageFolder/password.png");// 背景图片JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面// 把标签的大小位置设置为图片刚好填充整个面板label.setBounds(0, 0, background.getIconWidth(),background.getIconHeight());// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明imagePanel=(JPanel)this.getContentPane();imagePanel.setOpaque(false);// 内容窗格默认的布局管理器为BorderLayoutimagePanel.setLayout(new BorderLayout());imagePanel.add(welcomela,BorderLayout.NORTH );imagePanel.add(panel,BorderLayout.SOUTH);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.getLayeredPane().setLayout(null);// 把背景图片添加到分层窗格的最底层作为背景this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));this.setSize(background.getIconWidth(),background.getIconHeight());this.setVisible(true);this.show();jcb.addItemListener(new ItemListener(){public void itemStateChanged(ItemEvent ite) {if(ite.getStateChange() == ItemEvent.SELECTED){ try{ select = ite.getItem().toString();//选中的值 }catch (Exception e){         } } }});loadbtn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){int i=0;String userName=userjt.getText().trim();String passwordStr=passwordjp.getText().trim();DBConnection db=new DBConnection();String sql="select * from loadInfo";ResultSet rs;try {rs = db.executeQuery(sql);while(rs.next()){nameresult[i]=rs.getString("laborname").trim();passresult[i]=rs.getString("laborpass").trim();i++;}} catch (SQLException e1) {e1.printStackTrace();}if(select.equals("访客")){dispose();EquSearch_Labor vl=new EquSearch_Labor("访客");}else{for(int j=0;j<i;j++){if(nameresult[j].equals(userName)){if(passresult[j].equals(passwordStr)){Flag=true;break;}}}for(int j=0;j<i;j++){if((!nameresult[j].equals(userName))||(!passresult[j].equals(passwordStr))){JOptionPane.showMessageDialog(null, "用户或密码输入错误,请重新输入");userjt.setText(null);passwordjp.setText(null);}}System.out.println(Flag);if(Flag==true){dispose();Mana_Labor ul=new Mana_Labor();}}}});canclebtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.exit(0);}});this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);}public static void main(String args[]){Load_Labor ld=new Load_Labor();}}