当前位置: 首页 > news >正文

java在线聊天项目 客户端登陆窗口LoginDialog的注册用户功能 修改注册逻辑 增空用户名密码的反馈 增加showMessageDialog()提示框...

LoginDialog类的代码修改如下:

package com.swift.frame;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;

import com.swift.jdbc.DBAdd;
import com.swift.jdbc.DBUtil;
import com.swift.jdbc.User;
import com.swift.util.Center;

public class LoginDialog extends JDialog {

    private JPasswordField passwordField_2;
    private JPasswordField passwordField_1;
    private JTextField textField_2;
    private JTextField textField;

    Socket s;
    DataOutputStream dos;
    DataInputStream dis;

    public static void main(String args[]) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);

        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (UnsupportedLookAndFeelException e1) {
            e1.printStackTrace();
        }

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginDialog dialog = new LoginDialog();
                    dialog.addWindowListener(new WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public LoginDialog() {
        super();
        setResizable(false);
        setTitle("在线聊天登录框");
        getContentPane().setLayout(null);
        setBounds(100, 100, 427, 301);// 注册时高度为578,不注册是301

        // 设置窗口居中
        this.setLocation(Center.getPoint(this.getSize()));

        final JButton button_1 = new JButton();
        button_1.setText("登录");
        button_1.setBounds(230, 222, 106, 36);
        getContentPane().add(button_1);

        final JTextField textField_1 = new JTextField();
        textField_1.setBounds(148, 90, 192, 42);
        getContentPane().add(textField_1);

        final JLabel label = new JLabel();
        label.setText("帐    号");
        label.setBounds(76, 102, 66, 18);
        getContentPane().add(label);

        final JLabel label_1 = new JLabel();
        label_1.setText("密    码");
        label_1.setBounds(76, 167, 66, 18);
        getContentPane().add(label_1);

        final JPasswordField passwordField = new JPasswordField();
        passwordField.setBounds(148, 155, 192, 42);
        getContentPane().add(passwordField);

        final JButton button = new JButton();
        button.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                if (LoginDialog.this.getHeight() == 301) {
                    LoginDialog.this.setSize(427, 578);
                } else {
                    LoginDialog.this.setSize(427, 301);
                }
                // 设置窗口不断居中
                LoginDialog.this.setLocation(Center.getPoint(LoginDialog.this.getSize()));
            }
        });
        button.setText("注册");
        button.setBounds(76, 222, 106, 36);
        getContentPane().add(button);

        final JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBorder(new TitledBorder(null, "注册用户", TitledBorder.DEFAULT_JUSTIFICATION,
                TitledBorder.DEFAULT_POSITION, null, null));
        panel.setBounds(10, 278, 401, 226);
        getContentPane().add(panel);

        final JLabel label_2 = new JLabel();
        label_2.setBounds(44, 41, 65, 18);
        label_2.setText("手机号:");
        panel.add(label_2);

        textField = new JTextField();
        textField.setBounds(115, 35, 225, 30);
        panel.add(textField);

        final JButton button_2 = new JButton();
        button_2.setText("发送验证");
        button_2.setBounds(243, 75, 97, 30);
        panel.add(button_2);

        textField_2 = new JTextField();
        textField_2.setBounds(115, 104, 95, 30);
        panel.add(textField_2);

        final JLabel label_3 = new JLabel();
        label_3.setText("验证码:");
        label_3.setBounds(44, 110, 65, 18);
        panel.add(label_3);

        passwordField_1 = new JPasswordField();
        passwordField_1.setBounds(115, 143, 231, 30);
        panel.add(passwordField_1);

        passwordField_2 = new JPasswordField();
        passwordField_2.setBounds(115, 175, 231, 30);
        panel.add(passwordField_2);

        final JLabel label_4 = new JLabel();
        label_4.setText("密        码:");
        label_4.setBounds(44, 149, 65, 18);
        panel.add(label_4);

        final JLabel label_5 = new JLabel();
        label_5.setText("验证密码:");
        label_5.setBounds(44, 181, 65, 18);
        panel.add(label_5);

        final JButton button_3 = new JButton();
        button_3.setBounds(47, 510, 97, 30);
        getContentPane().add(button_3);
        button_3.setText("放弃");

        final JButton button_4 = new JButton();
        button_4.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {

                String phone = textField.getText();
                String password = null;
                String str1 = new String(passwordField_1.getPassword()).trim();
                String str2 = new String(passwordField_2.getPassword()).trim();
                if (!phone.equals("") && !str1.equals("") && !str2.equals("")) {
                    if (str1.equals(str2)) {
                        password = new String(passwordField_2.getPassword()).trim();
                        try {
                            dos.writeUTF(phone);
                            dos.writeUTF(password);
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    } else {
                        javax.swing.JOptionPane.showMessageDialog(LoginDialog.this, "输入密码不一致...");
                        System.out.println("输入密码不一致...");
                        passwordField_1.setText("");
                        passwordField_2.setText("");
                    }
                    
                }else {
                    javax.swing.JOptionPane.showMessageDialog(LoginDialog.this, "用户名密码必须填写...");
                    return;
                }
            }
        });

        button_4.setBounds(262, 510, 97, 30);
        getContentPane().add(button_4);
        button_4.setText("注册用户");

        connect();
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                disconnect();
            }
        });
    }

    public void connect() {
        try {
            s = new Socket("127.0.0.1", 8888);
            System.out.println("一个客户端登陆中....!");
            dos = new DataOutputStream(s.getOutputStream());
            dis = new DataInputStream(s.getInputStream());

        } catch (ConnectException e) {
            System.out.println("服务端异常.........");
            System.out.println("请确认服务端是否开启.........");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void disconnect() {
        try {
            if (dos != null)
                dos.close();
            if (s != null)
                s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

 

转载于:https://www.cnblogs.com/qingyundian/p/8038813.html

相关文章:

  • 八个维度,谈谈产品经理的分类与发展方向
  • 第一课 安装 登陆CentOS 7
  • 创建和使用数据库
  • CSS中使用expression完美设置页面最小宽度
  • Android 从SetContentView()谈起
  • 为什么volatile不能保证原子性而Atomic可以?
  • SQL优化常用方法44
  • NoSQL
  • session再次理解
  • 陈杰:无服务器架构,让云端开发更纯粹
  • Redis的发布订阅及.NET客户端实现
  • 动态载入数据的无刷新TreeView控件(8)
  • ubuntu-基本命令篇-13-用户管理
  • 四大中三家已面向客户推出机器人业务解决方案?别逗了,先用机器人自我革命吧! post by 上海嘉冰信息技术...
  • python基础实践(二)
  • #Java异常处理
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • CAP理论的例子讲解
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • JS题目及答案整理
  • LeetCode29.两数相除 JavaScript
  • linux学习笔记
  • React 快速上手 - 07 前端路由 react-router
  • spring-boot List转Page
  • 服务器之间,相同帐号,实现免密钥登录
  • 解析 Webpack中import、require、按需加载的执行过程
  • 日剧·日综资源集合(建议收藏)
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 如何设计一个微型分布式架构?
  • UI设计初学者应该如何入门?
  • ​2021半年盘点,不想你错过的重磅新书
  • ​比特币大跌的 2 个原因
  • ###STL(标准模板库)
  • #微信小程序(布局、渲染层基础知识)
  • (01)ORB-SLAM2源码无死角解析-(56) 闭环线程→计算Sim3:理论推导(1)求解s,t
  • (3)llvm ir转换过程
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (Bean工厂的后处理器入门)学习Spring的第七天
  • (第27天)Oracle 数据泵转换分区表
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理 第13章 项目资源管理(七)
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (一)WLAN定义和基本架构转
  • (转载)从 Java 代码到 Java 堆
  • **PHP二维数组遍历时同时赋值
  • .[hudsonL@cock.li].mkp勒索加密数据库完美恢复---惜分飞
  • .desktop 桌面快捷_Linux桌面环境那么多,这几款优秀的任你选
  • .NET BackgroundWorker
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .NET Core 中插件式开发实现
  • .net framework4与其client profile版本的区别
  • .NET 将多个程序集合并成单一程序集的 4+3 种方法
  • .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(二)...
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件
  • .netcore 6.0/7.0项目迁移至.netcore 8.0 注意事项
  • .net安装_还在用第三方安装.NET?Win10自带.NET3.5安装