Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 又有新问题了(判断密码 达不到效果)代码补全了
jialichao





发贴: 12
积分: 0
于 2006-05-28 18:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
package mypackage;

import javax.swing.*;

import java.sql.*;
import java.awt.*;
import java.awt.event.*;

public class FramSet extends JFrame {

  /**
   * @param args
   */
  JLabel username;
  JLabel password;
  JLabel repassword;
  JLabel age;
  JLabel name;
  JLabel lname;
  JTextField usertext;
  JPasswordField passwordtext;
  JPasswordField repasswordtext;
  JTextField agetext;
  JButton submit;
  JRadioButton man;
  JRadioButton woman;
  String sex="男";

  public FramSet()
  {
    
    Container c=this.getContentPane();
  
    GridBagLayout gbl=new GridBagLayout();
    GridBagConstraints gbc= new GridBagConstraints();
  
    c.setLayout(gbl);
    gbc.anchor=GridBagConstraints.WEST;
    username=new JLabel("用 户 名 : ");
    gbc.gridx=0;
    gbc.gridy=0;
    gbl.setConstraints(username,gbc);
    c.add(username);
    
    usertext=new JTextField(15);
    gbc.gridx=1;
    gbc.gridy=0;
    gbl.setConstraints(usertext,gbc);
    c.add(usertext);
    
    password=new JLabel("密 码 : ");
    gbc.gridx=0;
    gbc.gridy=1;
    gbl.setConstraints(password,gbc);
    c.add(password);
    
    passwordtext=new JPasswordField(15);
    passwordtext.setEchoChar('*');
    gbc.gridx=1;
    gbc.gridy=1;
    gbl.setConstraints(passwordtext,gbc);
    c.add(passwordtext);
    
    repassword=new JLabel("确认密码 : ");
    gbc.gridx=0;
    gbc.gridy=2;
    gbl.setConstraints(repassword,gbc);
    c.add(repassword);
    
    repasswordtext=new JPasswordField(15);
    passwordtext.setEchoChar('*');
    gbc.gridx=1;
    gbc.gridy=2;
    gbl.setConstraints(repasswordtext,gbc);
    c.add(repasswordtext);
    
    name=new JLabel("性 别 :");
    gbc.gridx=0;
    gbc.gridy=3;
    gbl.setConstraints(name,gbc);
    c.add(name);
    
    Panel p=new Panel();
    p.setLayout(new FlowLayout());
    final ButtonGroup bg=new ButtonGroup();
    man=new JRadioButton("男",true);
    woman=new JRadioButton("女");
    p.add(man);
    p.add(woman);
    c.add(p);
    bg.add(man);
    bg.add(woman);
    gbc.gridx=1;
    gbc.gridy=3;
    gbl.setConstraints(p,gbc);
    
    age=new JLabel("年 龄 :");
    gbc.gridx=0;
    gbc.gridy=4;
    gbl.setConstraints(age,gbc);
    c.add(age);
    
    agetext=new JTextField(15);
    gbc.gridx=1;
    gbc.gridy=4;
    gbl.setConstraints(agetext,gbc);
    c.add(agetext);
    
   lname=new JLabel(" ");
    gbc.gridx=1;
    gbc.gridy=5;
    gbl.setConstraints(lname,gbc);
    c.add(lname);
    
    submit=new JButton("注册");
    gbc.gridx=1;
    gbc.gridy=6;
    gbl.setConstraints(submit,gbc);
    c.add(submit);
    
    final ImageIcon p1=new ImageIcon("SmsMsgSomeFail.gif");
    final ImageIcon p2=new ImageIcon("SmsMsgFail.gif");
    
    class MyListener implements ActionListener
    {
      public void actionPerformed(ActionEvent e)
      {
      try
      {
        if(e.getSource()==submit)
        {
          
          Connection con;
          PreparedStatement ptm;
          Statement sm;
          ResultSet rs;
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          String url="jdbc:odbc:mydb";
          con=DriverManager.getConnection(url,"sa","");
          
                  
          String name,age,pas,repas,temp;
          name=usertext.getText();
          pas=new String(passwordtext.getPassword());
          repas=new String(repasswordtext.getPassword());
          age=agetext.getText();
          
          
          
          boolean flag=true;
          ptm=con.prepareStatement("select name from users1");
          rs=ptm.executeQuery("select name from users1");
          String str;
          while(rs.next()){
            str=rs.getString("name");
            if(name.equals(str)){
              flag=false;
              break;
            }
            
            
          }
          
          if(flag){
           if(pas.equals(repas)){
            ptm=con.prepareStatement("insert into users1 values(?,?,?,?)");
            ptm.setString(1,name);
            ptm.setString(2,pas);
            ptm.setString(3,age);
            ptm.setString(4,sex);
            ptm.executeUpdate();
            JOptionPane.showMessageDialog(null,"注册成功 !欢迎您的加入 !","信息提示",JOptionPane.PLAIN_MESSAGE,p1);    
            ptm.close();
            con.close();
           }else{
             JOptionPane.showMessageDialog(null, "两次输入密码不相同 !", "错误提示",JOptionPane.PLAIN_MESSAGE,p2);
           }
          }else{
            JOptionPane.showMessageDialog(null, "用户名已经被注册 !", "错误提示",JOptionPane.PLAIN_MESSAGE,p2);
          }
          
          
          
          
          
        }
        else
        {
          sex=((JRadioButton)(e.getSource())).getText();
        }

      }
      catch(Exception ae){}
      }

      
    }
    

    
    MyListener mylistener=new MyListener();
    submit.addActionListener(mylistener);
    man.addActionListener(mylistener);
    woman.addActionListener(mylistener);
  }
  public static void main(String[] args) {
    // TODO 自动生成方法存根
    FramSet enrolfram=new FramSet();
    enrolfram.setTitle("新用户注册");
    enrolfram.setResizable(false);
    enrolfram.setSize(300,300);
    enrolfram.setVisible(true);

  }

}

全部代码在这里了,大家看看有什么问题。我试过用==号判断字符串是否相同是可以的,equals()是判断两个对象是否相同的吧,我觉得放这里用不是太合适啊。


jialichao edited on 2006-05-29 22:58


一个程序员的奋斗历程

话题树型展开
人气 标题 作者 字数 发贴时间
4008 又有新问题了(判断密码 达不到效果)代码补全了 jialichao 11294 2006-05-28 18:01
3639 Re:又有新问题了 lisliefor 65 2006-05-28 19:19
3175 Re:又有新问题了(判断密码 达不到效果) why 95 2006-05-28 21:40

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923