RegisterFrame.java 5.0 KB
Newer Older
RunAtWorld's avatar
RunAtWorld 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
package com.yzu.daydayrun.ui;

import com.yzu.daydayrun.db.DoRegister;
import com.yzu.daydayrun.entity.User;
import com.yzu.daydayrun.utils.ValidCode;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class RegisterFrame extends JFrame {
    JLabel userLabel;
    JLabel pwdLabel;
    JLabel validcode;
    JTextField userText;
    JPasswordField pwdText;
    JButton loginBtn, cancelBtn;
    String username;
    String userpwd;
    private ValidCode vcode;
    File f1;
    URL url;
    URI uri;
    AudioClip aau;

     JTextField jtf_code;

    public RegisterFrame() {
        f1 = new File("D:/study/run-day/sound/background.wav");
        uri = f1.toURI();
        try {
            url = uri.toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        aau = Applet.newAudioClip(url);

        aau.loop();

        userLabel = new JLabel("用户名");
        userLabel.setBounds(40, 160, 150, 30);
        this.add(userLabel);

        pwdLabel = new JLabel("密码");
        pwdLabel.setBounds(40, 210, 150, 30);
        this.add(pwdLabel);


          validcode = new JLabel("验证码:");
        validcode.setBounds(40, 260, 150, 30);
        this.add(validcode);

        vcode = new ValidCode();
        vcode.setBounds(250, 255, 120, 25);
        this.add(vcode);

        userText = new JTextField(10);
        userText.setBounds(90, 165, 120, 25);

        userText.setFocusable(true);

        userText.setBorder(BorderFactory.createLoweredBevelBorder());
        this.add(userText);

        pwdText = new JPasswordField();
        pwdText.setBounds(90, 215, 120, 25);
        pwdText.setFocusable(true);

        pwdText.setBorder(BorderFactory.createLoweredBevelBorder());
        this.add(pwdText);

         jtf_code = new JTextField();
        jtf_code.setBounds(90, 265, 120, 25);
//        jtf_code.setFocusable(true);

        jtf_code.setBorder(BorderFactory.createLoweredBevelBorder());
        this.add(jtf_code);

        loginBtn = new JButton("确定");
        loginBtn.setBounds(80, 295, 65, 30);

        loginBtn.setForeground(Color.BLUE);

        loginBtn.setBorder(BorderFactory.createLineBorder(Color.BLUE));

        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {


                if (!isValidCodeRight()) {
                    JOptionPane.showMessageDialog(null, "验证码错误!");
                }

                if (isValidCodeRight()) {

                    if (userText.getText().length() == 0 || pwdText.getText().length() == 0) {
                        JOptionPane.showMessageDialog(null, "用户名或密码为空");
                    } else {
                        DoRegister rg = new DoRegister();
                        User u = rg.RegisterUser(userText.getText(), pwdText.getText());
                        if (rg.getFlag() != 0) {
                            JOptionPane.showMessageDialog(null, "注册成功!请返回");
                        } else {
                            JOptionPane.showMessageDialog(null, "注册失败!账号重复");
                        }
                    }
                }
            }
        });
        this.add(loginBtn);

        cancelBtn = new JButton("返回");
        cancelBtn.setBounds(155, 295, 65, 30);
        cancelBtn.setForeground(Color.blue);
        cancelBtn.setBorder(BorderFactory.createLineBorder(Color.blue));

        cancelBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                aau.stop();
                new LoginFrame();
            }
        });
        this.add(cancelBtn);

        BackImage back = new BackImage();
        back.setBounds(0, 0, 599, 360);
        this.add(back);

        this.setLayout(null);
        this.setSize(599, 360);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setIconImage(new ImageIcon("D:/study/run-day/image/title.png").getImage());
        this.setVisible(true);
    }

    public static void main(String[] args) {
        RegisterFrame frame = new RegisterFrame();
    }

    /**
     * 验证码的校验
     *
     * @return
     */
    public boolean isValidCodeRight() {

        if (jtf_code == null) {
            return false;
        }
        if (vcode == null) {
            return true;
        }
        if (vcode.getCode().equals(jtf_code.getText())) {
            return true;
        }
        return false;
    }

}