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

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

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 cat.client.CatChatroom;
import cat.function.CatBean;
import cat.function.ClientBean;
import cat.util.CatUtil;

public class CatLogin extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;
    public static HashMap<String, ClientBean> onlines;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    // 启动登陆界面
                    CatLogin frame = new CatLogin();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public CatLogin() {
        setTitle("Landing cat chat room,最代码官方验证通过该代码\n");
        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() + "/";
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(350, 250, 450, 300);
        contentPane = new JPanel() {
            private static final long serialVersionUID = 1L;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(new ImageIcon(classRootPath + "images/\u767B\u9646\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(128, 153, 104, 21);
        textField.setOpaque(false);
        contentPane.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setForeground(Color.BLACK);
        passwordField.setEchoChar('*');
        passwordField.setOpaque(false);
        passwordField.setBounds(128, 189, 104, 21);
        contentPane.add(passwordField);

        final JButton btnNewButton = new JButton();
        btnNewButton.setIcon(new ImageIcon(classRootPath + "images\\\u767B\u9646.jpg"));
        btnNewButton.setBounds(246, 227, 50, 25);
        getRootPane().setDefaultButton(btnNewButton);
        contentPane.add(btnNewButton);

        final JButton btnNewButton_1 = new JButton();
        btnNewButton_1.setIcon(new ImageIcon(classRootPath + "images\\\u6CE8\u518C.jpg"));
        btnNewButton_1.setBounds(317, 227, 50, 25);
        contentPane.add(btnNewButton_1);

        // 提示信息
        final JLabel lblNewLabel = new JLabel();
        lblNewLabel.setBounds(60, 220, 151, 21);
        lblNewLabel.setForeground(Color.red);
        getContentPane().add(lblNewLabel);

        // 监听登陆按钮
        btnNewButton.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();
                if (file.length() != 0) {

                    if (userPro.containsKey(u_name)) {
                        String u_pwd = new String(passwordField.getPassword());
                        if (u_pwd.equals(userPro.getProperty(u_name))) {

                            try {
                                Socket client = new Socket("localhost", 8520);

                                btnNewButton.setEnabled(false);
                                CatChatroom frame = new CatChatroom(u_name,
                                        client);
                                frame.setVisible(true);// 显示聊天界面
                                setVisible(false);// 隐藏掉登陆界面

                            } catch (UnknownHostException e1) {
                                // TODO Auto-generated catch block
                                errorTip("The connection with the server is interrupted, please login again");
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                errorTip("The connection with the server is interrupted, please login again");
                            }

                        } else {
                            lblNewLabel.setText("您输入的密码有误!");
                            textField.setText("");
                            passwordField.setText("");
                            textField.requestFocus();
                        }
                    } else {
                        lblNewLabel.setText("您输入昵称不存在!");
                        textField.setText("");
                        passwordField.setText("");
                        textField.requestFocus();
                    }
                } else {
                    lblNewLabel.setText("您输入昵称不存在!");
                    textField.setText("");
                    passwordField.setText("");
                    textField.requestFocus();
                }
            }
        });

        //注册按钮监听
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnNewButton_1.setEnabled(false);
                CatResign frame = new CatResign();
                frame.setVisible(true);// 显示注册界面
                setVisible(false);// 隐藏掉登陆界面
            }
        });
    }

    protected void errorTip(String str) {
        // TODO Auto-generated method stub
        JOptionPane.showMessageDialog(contentPane, str, "Error Message",
                JOptionPane.ERROR_MESSAGE);
        textField.setText("");
        passwordField.setText("");
        textField.requestFocus();
    }
}
最近下载更多
moyuzc  LV1 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日
最近浏览更多
dsczs666 8月18日
暂无贡献等级
柳咪华沙  LV7 6月28日
Po1aris 6月23日
暂无贡献等级
18853847682 6月17日
暂无贡献等级
暂无贡献等级
ethan_ 5月29日
暂无贡献等级
都是纯牛奶 5月28日
暂无贡献等级
求学的熊猫  LV8 5月27日
周鸣郝  LV2 5月26日
moyuzc  LV1 5月7日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友