标签:
package addresslist; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.ImageIcon; import javax.swing.JButton; 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.border.EmptyBorder; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; /* * 封装的太差啦! * 要好好理解继承封装多态的使用 * 不然代码就没法看了 * 其实是后期修改太困难啦 * 通用模版的使用 * 代码的可复用性 * 考虑好了之后再动手敲代码 * 不然写了维护起来也比较困难最后只好放弃 * * 然后就是代码的重构与优化 * */ public class login extends JFrame { private JPanel contentPane; private JTextField textField; private JPasswordField passwordField; public Judge judge=new Judge(); static String username; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JFrame fra = new login(); fra.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public void setall(String n,String p){ textField.setText(n); passwordField.setText(p); } public login() { setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new NewPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); JLabel label = new JLabel("\u7528\u6237\u540D"); textField = new JTextField(); textField.setColumns(10); JLabel label_1 = new JLabel("*"); textField.addFocusListener(new FocusAdapter() { @Override public void focusLost(FocusEvent e) { try { String s=textField.getText().toString(); if(!judge.exist(s)){ label_1.setText("no such nickname!"); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } @Override public void focusGained(FocusEvent e) { label_1.setText("*"); } }); JLabel label_2 = new JLabel("\u5BC6\u7801"); passwordField = new JPasswordField(); JButton btnLogin = new JButton("login"); btnLogin.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==10){ btnLogin.doClick(); } } }); btnLogin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String s=new String(passwordField.getPassword()); String n=textField.getText().toString(); try { if(judge.canlogin(n,s)){ username=n; mainFrame frame=new mainFrame(); frame.setVisible(true); dispose(); } else{ JOptionPane.showMessageDialog(null, "密码或用户名错误"); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); JButton btnRegister = new JButton("register"); btnRegister.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { register rs=new register(); rs.setVisible(true); } }); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(66) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(label) .addComponent(label_2)) .addGap(18) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false) .addComponent(passwordField) .addComponent(textField, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)) .addGap(28) .addComponent(label_1)) .addGroup(gl_contentPane.createSequentialGroup() .addGap(80) .addComponent(btnLogin) .addGap(37) .addComponent(btnRegister))) .addContainerGap(151, Short.MAX_VALUE)) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(48) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(label) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addComponent(label_1) .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) .addGap(29) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(label_2) .addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(48) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(btnLogin) .addComponent(btnRegister)) .addContainerGap(61, Short.MAX_VALUE)) ); contentPane.setLayout(gl_contentPane); } class NewPanel extends JPanel{ public NewPanel() {} public void paintComponent(Graphics g) { int x=0,y=0; java.net.URL imgURL=getClass().getResource("login.jpg"); //test.jpg是测试图片,与Demo.java放在同一目录下 ImageIcon icon=new ImageIcon(imgURL);//this is really useful g.drawImage(icon.getImage(),x,y,getSize().width,getSize().height,this); while(true) { g.drawImage(icon.getImage(),x,y,this); if(x>getSize().width && y>getSize().height)break; //这段代码是为了保证在窗口大于图片时,图片仍能覆盖整个窗口 if(x>getSize().width) { x=0; y+=icon.getIconHeight(); } else x+=icon.getIconWidth(); } } } }
package addresslist; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JPasswordField; import javax.swing.JButton; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class register extends JFrame { private JPanel contentPane; private JTextField textField; private JPasswordField passwordField; private JPasswordField passwordField_1; public Judge judge=new Judge(); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { register frame = new register(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public register() { setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); JLabel label = new JLabel("\u7528\u6237\u540D"); textField = new JTextField(); textField.setColumns(10); JLabel label_1 = new JLabel("*"); textField.addFocusListener(new FocusAdapter() { @Override public void focusGained(FocusEvent e) { label_1.setText("*"); } @Override public void focusLost(FocusEvent e) { try { if(!judge.exist(textField.getText().toString())){ label_1.setText("is valiable"); } else{ label_1.setText("the name is used!"); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); JLabel label_2 = new JLabel("\u5BC6\u7801"); passwordField = new JPasswordField(); JLabel label_3 = new JLabel("\u786E\u8BA4\u5BC6\u7801"); passwordField_1 = new JPasswordField(); JLabel label_4 = new JLabel("*"); passwordField_1.addFocusListener(new FocusAdapter() { @Override public void focusGained(FocusEvent e) { label_4.setText("*"); } @Override public void focusLost(FocusEvent e) { if(new String(passwordField.getPassword()).equals(new String(passwordField_1.getPassword()))){ label_4.setText("*"); } else{ label_4.setText("the password is not the same,please check it!"); } } }); JButton btnRegister = new JButton("register"); btnRegister.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { if(!judge.exist(textField.getText().toString())){ String n=textField.getText().toString(); String p=new String(passwordField.getPassword()); judge.regist(n,p); login lg=new login(); lg.setVisible(true); lg.setall(n,p); dispose(); } else{ JOptionPane.showMessageDialog(null, "the name is exist please try another!"); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(39) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addComponent(btnRegister) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false) .addGroup(gl_contentPane.createSequentialGroup() .addGap(47) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addComponent(label_2) .addComponent(label)) .addGap(18) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false) .addComponent(passwordField) .addComponent(textField, GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))) .addGroup(gl_contentPane.createSequentialGroup() .addGap(39) .addComponent(label_3) .addGap(18) .addComponent(passwordField_1)))) .addGap(18) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(label_1) .addComponent(label_4)) .addContainerGap(199, Short.MAX_VALUE)) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(46) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(label) .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(label_1)) .addGap(18) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(label_2) .addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(26) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(label_3) .addComponent(passwordField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(label_4)) .addGap(34) .addComponent(btnRegister) .addContainerGap(41, Short.MAX_VALUE)) ); contentPane.setLayout(gl_contentPane); } }
package addresslist; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class Judge { public static void main(String[] args) throws Exception { String url ="jdbc:mysql://localhost:3306/login"; //数据库连接字符串 Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序 Connection conn= (Connection) DriverManager.getConnection(url,"tan","tn5201314"); //建立连接 Statement stmt=(Statement) conn.createStatement(); //创建SQL容器 String sql="select * from user"; //表为teacher ResultSet rs=stmt.executeQuery(sql); //获得结果集 while( rs.next() ) { //处理结果集 System.out.print(rs.getString("name")+" "); System.out.print(rs.getString("password")+"\n"); } rs.close(); stmt.close(); conn.close(); //关闭次序 } public Connection getConnection()throws Exception{ String url ="jdbc:mysql://localhost:3306/login"; //数据库连接字符串 Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序 Connection conn= DriverManager.getConnection(url,"tan","tn5201314"); return conn; } public boolean exist(String s) throws Exception{ Connection conn= getConnection(); //建立连接 Statement stmt=conn.createStatement(); //创建SQL容器 String sql="select * from user where name=‘"+s+"‘"; //表为user ResultSet rs=stmt.executeQuery(sql); //获得结果集 if( rs.next() ) { rs.close(); stmt.close(); conn.close(); return true; } else { rs.close(); stmt.close(); conn.close(); return false; } //关闭次序 } public boolean canlogin(String n,String p) throws Exception{ Connection conn=getConnection(); //建立连接 Statement stmt=conn.createStatement(); //创建SQL容器 String sql="select password from user where name=‘"+n+"‘"; //表为user ResultSet rs=stmt.executeQuery(sql); //获得结果集 if(rs.next()){ if(rs.getString("password").equals(p)){ rs.close(); stmt.close(); conn.close(); return true; } else{ rs.close(); stmt.close(); conn.close(); return false; } } else{ rs.close(); stmt.close(); conn.close(); return false; } } public void regist(String n,String p) throws Exception{ Connection conn=getConnection(); //建立连接 Statement stmt=conn.createStatement(); //创建SQL容器 String sql="insert into user values(‘"+n+"‘,‘"+p+"‘)"; //表为user stmt.executeUpdate(sql); stmt.close(); conn.close(); } }
标签:
原文地址:http://www.cnblogs.com/tannian/p/4316955.html