1、import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;public class Login extends JFrame private static final long serialVersionUID = 1L; private JLabel jlUsername; private JLabel jlPwd; private JTextField tx
2、tUsername; /用户名 private JTextField txtPassword; /密码 private JButton jbOk; private JButton jbCancel; public JLabel getJlUsername() if(jlUsername=null) jlUsername=new JLabel(账号); jlUsername.setForeground(Color.BLACK); jlUsername.setBounds(118, 40, 85, 30); return jlUsername; public JLabel getJlPwd() i
3、f(jlPwd=null) jlPwd=new JLabel(密码 jlPwd.setForeground(Color.BLACK); jlPwd.setBounds(118, 100, 85, 30); return jlPwd; public JTextField getTxtUsername() if(txtUsername=null) txtUsername=new JTextField(20); txtUsername.setBounds(180,40 , 200, 30); return txtUsername; public JTextField getTxtPassword()
4、 if(txtPassword=null) txtPassword=new JPasswordField(20); txtPassword.setBounds(180, 100, 200, 30); return txtPassword; public JButton getJbOk() if(jbOk=null) jbOk=new JButton(登陆 jbOk.setBounds(150, 180, 90, 30); /jbOk.addActionListener(this); return jbOk; public JButton getJbCancel() if(jbCancel=nu
5、ll) jbCancel=new JButton(取消 jbCancel.setBounds(300, 180, 90, 30); return jbCancel; public Login(String title) super(title); /JFrame frame=new JFrame(); Container cantainer=this.getContentPane(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ImageIcon img = new ImageIcon(F:Java传习srcLibraryZFXY
6、H48I_U)C4M2C4.jpg/这是背景图片 JLabel imgLabel = new JLabel(img);/将背景图放在标签里。 this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE);/注意这里是关键,将背景标签添加到jfram的LayeredPane面板里。 imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight();/设置背景标签的位置 cantainer.setLayout(new BorderLayout(); imgLabel.
7、setLayout(null); imgLabel.add(getJlUsername(); imgLabel.add(getTxtUsername(); imgLabel.add(getJlPwd(); imgLabel.add(getTxtPassword(); imgLabel.add(getJbOk(); imgLabel.add(getJbCancel(); cantainer.add(imgLabel,BorderLayout.NORTH); (JPanel)cantainer).setOpaque(false); /注意这里,将内容面板设为透明。这样LayeredPane面板中的
8、背景才能显示出来。 this.setSize(img.getIconWidth(),img.getIconHeight(); myEvent(); this.setVisible(true); this.setResizable(true); private void myEvent() jbCancel.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) Login.this.dispose(); ); jbOk.addActionListener(new ActionListen
9、er() Connection conn=null; Statement stm=null; ResultSet st=null; try Class.forName(com.mysql.jdbc.Driver conn=DriverManager.getConnection(jdbc:mysql:/localhost:3306/library,sa123456 stm=conn.createStatement(); String sql=select * from user; /String sql1=select psw from user st=stm.executeQuery(sql)
10、; / st1=stm.executeQuery(sql1); if(st.next() if(txtUsername.getText().equals(st.getString(id)&txtPassword.getText().equals(st.getString(psw) Login.this.setVisible(false); new Book(管理界面 else JOptionPane.showMessageDialog(null, 您输入的账号或密码不正确 getTxtUsername().setText( getTxtPassword().setText( else JOpt
11、ionPane.showMessageDialog(null,用户名不存在,请重新输入,错误! getTxtUsername().setText( getTxtPassword().setText( catch(ClassNotFoundException e1) e1.printStackTrace(); catch (SQLException e2) / TODO Auto-generated catch block e2.printStackTrace(); finally try stm.close(); catch (Exception e3) / 关闭Connection try conn.close(); catch (Exception e4) public static void main(String args) new Login(图书管理登陆界面 管理界面实现代码import java.awt.event.Actio
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1