package canying; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.File; import java.sql.ResultSet; import java.sql.SQLException; import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JPopupMenu; import javax.swing.JTextField; import shujuku.basedao; public class denglu extends JDialog implements ActionListener,KeyListener{ JLabel jl1,jl2,jl3; JTextField jt; JPasswordField jpf; JButton jb1,jb2; public static void main(String[] args)//这个主函数不需要,只是为了调试用 { denglu dg=new denglu(); } public denglu() { jl1=new JLabel("用户名"); jl1.setBounds(60, 190, 150, 30);//设置改标签的位置 this.add(jl1); jl2=new JLabel("(或员工编号)"); jl2.setForeground(Color.red);//设置此对象的前景色 jl2.setBounds(100, 210, 100, 30); this.add(jl2); jl3=new JLabel("密码"); jl3.setBounds(60, 240, 150, 30); this.add(jl3); jt=new JTextField(20); jt.setFocusable(true); jt.setBounds(180, 190, 120, 30);//设置位置 this.add(jt); jt.setBorder(BorderFactory.createLoweredBevelBorder());//设置边框凹下去的效果 jpf=new JPasswordField(); jpf.setFocusable(true); jpf.setBounds(180, 240, 120, 30); jpf.addKeyListener(this); this.add(jpf); jpf.setBorder(BorderFactory.createLoweredBevelBorder()); jb1=new JButton("确定"); jb1.setBounds(105, 300, 75, 30); jb1.setForeground(Color.blue); jb1.addActionListener(this); final String id=jb1.getText(); JPopupMenu pop=new JPopupMenu(id); JMenuItem it1=new JMenuItem ("功能1"); it1.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { System.out.println(" 右键功能"); } @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } }); this.add(jb1); jb2=new JButton("取消"); jb2.setBounds(215, 300, 75, 30); jb2.setForeground(Color.blue); jb2.addActionListener(this); this.add(jb2); this.setLayout(null);//空布局是为了各组件在指定的位置显示 backimage bi=new backimage(); bi.setBounds(0, 0, 360, 360); this.add(bi); this.setUndecorated(true);//去掉JFrame的边框 this.setSize(360,360); int width=getToolkit().getDefaultToolkit().getScreenSize().width; int height=getToolkit().getDefaultToolkit().getScreenSize().height; this.setLocation(width/2-200, height/2-200); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==jb1) { String usr=this.jt.getText().trim(); String ps=new String(this.jpf.getPassword()); String sql="select zhiwei from renyuan a,zheze b where a.pK_id=b.pk_id and a.name=? and a.passwd=?"; Object [] ob={usr,ps}; basedao db=new basedao(); ResultSet rs; rs=db.execute(sql, ob, 0); try { if(rs.next()) { String zhiwei=rs.getString(1); if(zhiwei.equals("manager")) { this.dispose(); new caozuo(); } else { JOptionPane.showMessageDialog(this, "很抱歉 你没有权限 请联系管理员"); } } else { JOptionPane.showMessageDialog(this, "用户名或密码错误!"); } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } if(e.getSource()==jb2) { this.dispose(); } } @Override public void keyPressed(KeyEvent arg0) { // TODO Auto-generated method stub if(arg0.getKeyCode()==KeyEvent.VK_ENTER) { String usr=this.jt.getText().trim(); String ps=new String(this.jpf.getPassword()); String sql="select zhiwei from renyuan a,zheze b where a.pK_id=b.pk_id and a.name=? and a.passwd=?"; Object [] ob={usr,ps}; basedao db=new basedao(); ResultSet rs; rs=db.execute(sql, ob, 0); try { if(rs.next()) { String zhiwei=rs.getString(1); if(zhiwei.equals("manager")) { this.dispose(); new caozuo(); } else { JOptionPane.showMessageDialog(this, "很抱歉 你没有权限 请联系管理员"); } } else { JOptionPane.showMessageDialog(this, "用户名或密码错误!"); } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } } class backimage extends JPanel { Image im; public backimage() { try{ im=ImageIO.read(new File("image/0304_5.jpg")); }catch(Exception e){e.printStackTrace();} } public void paint(Graphics g) { g.drawImage(im, 0, 0, 360, 360, this); } }
时间: 2024-10-10 22:42:09