- package cn.itcast.regedit;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.HashSet;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- import cn.itcast.tools.GUITools;
- /*
- * 注册页面
- */
- public class RegistFrame extends JFrame {
- public JLabel registLabel1 = new JLabel("Sign Up"); // 注册标签1
- public JLabel registLabel2 = new JLabel("It's free and always will be."); // 注册标签2
- public JLabel userNameLabel = new JLabel("用户名"); // 用户名标签
- public JTextField userNameField = new JTextField(); // 用户名文本框
- public JLabel passwordLabel = new JLabel("密码"); // 密码标签
- public JPasswordField passwordField = new JPasswordField(); // 密码文本框
- public JLabel rePasswordLabel = new JLabel("重复密码"); // 重复密码标签
- public JPasswordField rePasswordField = new JPasswordField(); // 重复密码文本框
- public JLabel birthdayLabel = new JLabel("出生日期"); // 生日标签
-
- public JComboBox<String> yearField = new JComboBox<String>(); // 年份选择框
- public JComboBox<String> monthField = new JComboBox<String>(); // 月份选择框
- public JComboBox<String> dayField = new JComboBox<String>(); // 日期选择框
-
- public JButton registBtn = new JButton("注\t册");// 注册按钮
- public JButton resetBtn = new JButton("重\t置");// 重置按钮
- HashSet<User> date = new HashSet<User>();
-
- public RegistFrame() {
- initDate();
- init(); // 初始化窗口
- addComponent(); // 添加组件
- addlistener(); // 事件加载
- }
- private void initDate(){
- date.add(new User("a","1",new Date()));
- date.add(new User("b","2",new Date()));
- date.add(new User("c","3",new Date()));
- }
- // 初始化窗口
- private void init() {
- setTitle("注册");
- setSize(600, 360);
- GUITools.center(this);
- }
- private void addComponent() {
- // 去除布局
- setLayout(null);
- // 添加注册标题
- registLabel1.setFont(new Font("楷体", Font.BOLD, 20));
- registLabel1.setBounds(220, 20, 100, 30);
- this.add(registLabel1);
- registLabel2.setBounds(185, 50, 200, 20);
- this.add(registLabel2);
- // 用户名文本框
- userNameLabel.setBounds(100, 80, 60, 25);
- userNameField.setBounds(160, 80, 120, 25);
- this.add(userNameLabel);
- this.add(userNameField);
- // 密码文本框
- passwordLabel.setBounds(100, 110, 60, 25);
- passwordField.setBounds(160, 110, 120, 25);
- this.add(passwordLabel);
- this.add(passwordField);
- // 密码文本框
- rePasswordLabel.setBounds(290, 110, 60, 25);
- rePasswordField.setBounds(350, 110, 120, 25);
- this.add(rePasswordLabel);
- this.add(rePasswordField);
- // 生日
- birthdayLabel.setBounds(100, 140, 60, 25);
-
- yearField.setBounds(160, 140, 60, 25);
- monthField.setBounds(220, 140, 60, 25);
- dayField.setBounds(280, 140, 60, 25);
-
- for (int i = 1900; i <= 2099; i++) {
- yearField.addItem(i+"");
- }
- for (int i = 1; i <= 12; i++) {
- monthField.addItem(i+"");
- }
- for (int i = 1; i <= 31; i++) {
- dayField.addItem(i+"");
- }
-
- this.add(birthdayLabel);
- this.add(yearField);
- this.add(monthField);
- this.add(dayField);
-
- // 注册按钮
- registBtn.setBounds(160, 240, 80, 25);
- this.add(registBtn);
- }
- // 添加监听器(事件加载)
- public void addlistener() {
- //注册按钮,事件,动作事件ActionListener
- registBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- regedit();
- } catch (Exception e1) {
-
- e1.printStackTrace();
- }
- }
- });
- }
- public void regedit()throws Exception{
- //获取用户输入的所有数据内容
- //用户名
- String username = userNameField.getText();
- //获取密码
- String password = new String(passwordField.getPassword());
- //重复密码
- String repassword = new String(rePasswordField.getPassword());
- //获取生日的,三个下拉菜单 get
- String year = yearField.getSelectedItem()+"";//2001
- String month = monthField.getSelectedItem()+"";//5
- String day = dayField.getSelectedItem()+"";
-
- //判断两次输入的密码,是否相同
- //String类方法 equals
- if(!password.equals(repassword)){
- //密码不同
- JOptionPane.showMessageDialog(this, "两次密码不同");
- return ;
- }
- //将字符串转成日期的对象
- String dateString = year+"-"+month+"-"+day;
- DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- Date birthdayDate = df.parse(dateString);
-
- //用户的数据,用户名,密码,日期,封装成User对象
- User u = new User(username,password,birthdayDate);
- //判断用户名是否已经存在
- //直接将对象,存储到HashSet中,存储成功,用户名没有使用过
- //存储失败了,用户名已经被占有
-
- //将User对象,存储到集合中
- boolean b = date.add(u);
- if(b){
- JOptionPane.showMessageDialog(this, "注册成功");
- }else{
- JOptionPane.showMessageDialog(this, "失败失败,用户名已经被占有");
- }
-
-
-
- }<div class="blockcode"><blockquote>package cn.itcast.regedit;
- public class Test {
- public static void main(String[] args) {
- new RegistFrame().setVisible(true);
- }
- }
复制代码
这里面有个年月日选择的框.你看一下.
|