首页>代码>java swing开发大猫聊天室源码,简单易懂,适合java swing初学者>/java swing开发大猫聊天室源码/CatClient/src/cat/login/CatResign.java
package cat.login;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.URLDecoder;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cat.util.CatUtil;

public class CatResign extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;
    private JLabel lblNewLabel;

    public CatResign() {
        setTitle("Registered cat chat room,最代码官方验证通过该代码\n");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(350, 250, 450, 300);

        String classPath = getClass().getResource("/").getPath();
        File classFile = null;
        try {
            classFile = new File(URLDecoder.decode(classPath, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        final String classRootPath = classFile.getAbsolutePath() + "/";

        contentPane = new JPanel() {
            private static final long serialVersionUID = 1L;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(new ImageIcon(classRootPath + "images\\\u6CE8\u518C\u754C\u9762.jpg").getImage(), 0, 0, getWidth(), getHeight(), null);
            }
        };
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(150, 42, 104, 21);
        textField.setOpaque(false);
        contentPane.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setEchoChar('*');
        passwordField.setOpaque(false);
        passwordField.setBounds(190, 98, 104, 21);
        contentPane.add(passwordField);

        passwordField_1 = new JPasswordField();
        passwordField_1.setBounds(192, 152, 104, 21);
        passwordField_1.setOpaque(false);
        contentPane.add(passwordField_1);

        //注册按钮
        final JButton btnNewButton_1 = new JButton();
        btnNewButton_1.setIcon(new ImageIcon(classRootPath + "images\\注册1.jpg"));
        btnNewButton_1.setBounds(320, 198, 80, 40);
        getRootPane().setDefaultButton(btnNewButton_1);
        contentPane.add(btnNewButton_1);

        //返回按钮
        final JButton btnNewButton = new JButton("");
        btnNewButton.setIcon(new ImageIcon(classRootPath + "images\\返回.jpg"));
        btnNewButton.setBounds(230, 198, 80, 40);
        contentPane.add(btnNewButton);

        //提示信息
        lblNewLabel = new JLabel();
        lblNewLabel.setBounds(55, 218, 185, 20);
        lblNewLabel.setForeground(Color.red);
        contentPane.add(lblNewLabel);

        //返回按钮监听
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnNewButton.setEnabled(false);
                //返回登陆界面
                CatLogin frame = new CatLogin();
                frame.setVisible(true);
                setVisible(false);
            }
        });

        //注册按钮监听
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Properties userPro = new Properties();
                File file = new File("Users.properties");
                CatUtil.loadPro(userPro, file);

                String u_name = textField.getText();
                String u_pwd = new String(passwordField.getPassword());
                String u_pwd_ag = new String(passwordField_1.getPassword());

                // 判断用户名是否在普通用户中已存在
                if (u_name.length() != 0) {

                    if (userPro.containsKey(u_name)) {
                        lblNewLabel.setText("用户名已存在!");
                    } else {
                        isPassword(userPro, file, u_name, u_pwd, u_pwd_ag);
                    }
                } else {
                    lblNewLabel.setText("用户名不能为空!");
                }
            }

            private void isPassword(Properties userPro,
                                    File file, String u_name, String u_pwd, String u_pwd_ag) {
                if (u_pwd.equals(u_pwd_ag)) {
                    if (u_pwd.length() != 0) {
                        userPro.setProperty(u_name, u_pwd_ag);
                        try {
                            userPro.store(new FileOutputStream(file),
                                    "Copyright (c) Boxcode Studio");
                        } catch (FileNotFoundException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        btnNewButton_1.setEnabled(false);
                        //返回登陆界面
                        CatLogin frame = new CatLogin();
                        frame.setVisible(true);
                        setVisible(false);
                    } else {
                        lblNewLabel.setText("密码为空!");
                    }
                } else {
                    lblNewLabel.setText("密码不一致!");
                }
            }
        });
    }
}
最近下载更多
moyuzc  LV1 2024年5月7日
lalalla159  LV3 2023年12月2日
lxsnh123  LV3 2022年11月15日
aaaaachi  LV1 2022年6月27日
渔客不知鱼乐  LV2 2022年6月18日
微信网友_6003487859068928  LV5 2022年6月15日
微信网友_5945726344056832  LV2 2022年5月5日
zhendong  LV7 2022年3月10日
罐瓶  LV2 2022年2月27日
yuxinnan  LV4 2022年2月4日
最近浏览更多
Arvin001 12月8日
暂无贡献等级
Vincter  LV3 2024年12月25日
13133117021  LV5 2024年12月24日
zjjxy2312040436 2024年12月23日
暂无贡献等级
dsczs666 2024年8月18日
暂无贡献等级
柳咪华沙  LV7 2024年6月28日
Po1aris 2024年6月23日
暂无贡献等级
18853847682 2024年6月17日
暂无贡献等级
大喊大叫凶手 2024年6月1日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友