A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王鑫宇 黑马帝   /  2011-8-4 18:06  /  2791 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

时下,聊天系统有很多,QQ已达到了登峰造极的程度,小弟并没有吃饱了撑的去挑战腾讯,只是想从一个项目中锻炼一下,各个方法的用法,和一个程序员应该有的设计思维。并且不断完善去改进次系统,并完善我们程序员的思维和代码的熟练度。希望有兴趣的马友们多提宝贵意见,意见可以有很多种,例如“增加其他的功能,用更简便的方法去实现已完成的功能”小弟感激不尽。
       好了,话不多说,见代码

       1.ChatClientMain类   //客户端开启类[code=java]package util;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class ChatClientMain {
        public ChatClientMain(){
                new ClientInterface();
        }
        public ChatClientMain(String name,String password){
                new ClientInterface(name,password);
        }
        public static void main(String[] args){               
                SwingUtilities.invokeLater(new Runnable(){
                        public void run(){
                                try {
                                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                                new ChatClientMain();
                        }
                });
        }
}[/code]2.ClientInterface类[code=java]package util;

import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.*;

public class ClientInterface extends JFrame{
       
private String userName="";
private JDialog login;
private JPanel login_pane;
private JLabel msg_label;
private JLabel reg;       
private JDialog register;
private JPanel reg_pane;
private JLabel title;
private JLabel name;
private TextField name_value;
private JLabel name_msg;
private JLabel password;
private JPasswordField password_value;
private JLabel password_msg;
private JLabel password1;
private JPasswordField password_value1;
private JPasswordField password_value2;
private JLabel password_msg1;
private JLabel secondName;
private TextField secondName_value;
private JLabel secondName_msg;
private ButtonGroup bg;
private JRadioButton onLine;
private JRadioButton hide;
private JButton submit;
private JButton reset;
private JButton exit;
private Color c;
private Socket socket;
private DataOutputStream dos;
private DataInputStream dis;       
private String reloginName="";
private String reloginPassword="";
private String state;
       
        public ClientInterface(String name,String password){
                reloginName = name;
                reloginPassword = password;
                new ClientInterface();               
        }
       
        public ClientInterface(){       
                SwingUtilities.invokeLater(new Runnable(){
                        public void run(){
                        try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }
        });
               
                try {
                        login = new JDialog(this,true);
                        login.setTitle("登录");
                        login.addWindowListener(new WindowAdapter(){
                                public void windowClosing(WindowEvent arg0) {
                                        System.exit(0);
                                }
                        });
                        login.setLayout(null);
                        login.setBounds(350,200,300,200);
                        login_pane = new JPanel();
                        login_pane.setBounds(0,0,300,200);
                        login_pane.setLayout(null);
                        login.add(login_pane);
                       
                        msg_label = new JLabel();
                        msg_label.setBounds(50,5,200,20);
                        msg_label.setForeground(Color.red);
                        login_pane.add(msg_label);
                       
                        name = new JLabel("用户名:");
                        name.setBounds(30,30,50,20);
                        login_pane.add(name);
                       
                        name_value = new TextField();
                        name_value.setBounds(80,30,130,20);
                        login_pane.add(name_value);
                        name_value.setText(reloginName);
                        reg = new JLabel("注册帐号");
                        reg.setBounds(225,30,55,20);
                        reg.setForeground(Color.gray);
                        reg.addMouseListener(new MouseAdapter(){
                                public void mouseEntered(MouseEvent e) {
                                        reg.setForeground(Color.red);
                                }
                                public void mouseExited(MouseEvent arg0) {
                                        reg.setForeground(Color.gray);
                                }
                                public void mouseClicked(MouseEvent e) {
                                        register = new JDialog(login);
                                        register.setLayout(null);
                                        register.setBounds(260,200,500,250);
                                       
                                        reg_pane = new JPanel();
                                        reg_pane.setLayout(null);
                                        reg_pane.setBounds(0,0,500,250);
                                        register.add(reg_pane);

                                        title = new JLabel("用户注册");
                                        title.setBounds(220,5,80,20);
                                        reg_pane.add(title);
                                       
                                        name = new JLabel("用户名:");
                                        name.setBounds(25,40,50,20);
                                        reg_pane.add(name);
                                        name_value = new TextField();
                                        name_value.setBounds(75,40,120,20);
                                        name_value.addFocusListener(new FocusAdapter(){
                                                public void focusLost(FocusEvent e) {
                                                        String getStr = name_value.getText();
                                                        if(getStr==null||getStr.equals("")||!Character.isLetter(getStr.charAt(0))||getStr.length()<6||getStr.length()>16){
                                                                name_msg.setForeground(Color.red);
                                                                return;
                                                        }
                                                        char chs[] = getStr.toCharArray();
                                                        for(int i = 0 ; i<chs.length;i++){
                                                                if(!(Character.isLetter(chs)||Character.isDigit(chs))){
                                                                        name_msg.setForeground(Color.red);
                                                                        return;
                                                                }
                                                        }
                                                        name_msg.setForeground(c);
                                                }
                                        });
                                        name_value.addTextListener(new TextListener(){
                                                public void textValueChanged(TextEvent arg0) {
                                                       
                                                        if(!name_value.getText().equals("")){
                                                                if(!Character.isLetter(name_value.getText().charAt(0))||name_value.getText().length()<6||name_value.getText().length()>16){
                                                                        name_msg.setForeground(Color.red);
                                                                        return;
                                                                }
                                                        }
                                                        name_msg.setForeground(c);                                               
                                                }
                                        });
                                        reg_pane.add(name_value);
                                        name_msg = new JLabel("*必须为英文和数字,且英文开头,长度在6-16之间");
                                        name_msg.setBounds(200,40,270,20);
                                        c = name_msg.getForeground();
                                        reg_pane.add(name_msg);
                                       
                                        password = new JLabel("密   码:");
                                        password.setBounds(25,75,50,20);
                                        reg_pane.add(password);
                                        password_value = new JPasswordField();
                                        password_value.setBounds(75,75,120,20);
                                        password_value.addFocusListener(new FocusAdapter(){
                                                public void focusLost(FocusEvent e) {
                                                        char chs[] = password_value.getPassword();
                                                        if(chs.length<6||chs.length>16){
                                                                password_msg.setForeground(Color.red);
                                                                return;
                                                        }
                                                        password_msg.setForeground(c);
                                                }
                                        });
                                        reg_pane.add(password_value);
                                        password_msg = new JLabel("*长度在6-16之间");
                                        password_msg.setBounds(200,75,270,20);
                                        reg_pane.add(password_msg);
                                       
                                        password1 = new JLabel("密码确认:");
                                        password1.setBounds(10,110,65,20);
                                        reg_pane.add(password1);
                                        password_value1 = new JPasswordField();
                                        password_value1.setBounds(75,110,120,20);
                                        password_value1.addFocusListener(new FocusAdapter(){
                                                public void focusLost(FocusEvent e) {
                                                        if(!String.valueOf(password_value1.getPassword()).equals(String.valueOf(password_value.getPassword()))){
                                                                password_msg1.setForeground(Color.red);
                                                                return;
                                                        }
                                                        password_msg1.setForeground(c);
                                                }
                                        });
                                        reg_pane.add(password_value1);
                                        password_msg1 = new JLabel("*重新输入密码,以确认密码");
                                        password_msg1.setBounds(200,110,270,20);
                                        reg_pane.add(password_msg1);
                                       
                                        secondName = new JLabel("呢   称:");
                                        secondName.setBounds(25,145,50,20);
                                        reg_pane.add(secondName);
                                        secondName_value = new TextField();
                                        secondName_value.setBounds(75,145,120,20);
                                        secondName_value.addFocusListener(new FocusAdapter(){
                                                public void focusLost(FocusEvent e) {
                                                        if(secondName_value.getText()==null ||secondName_value.equals("") ||secondName_value.getText().length()>13){
                                                                secondName_msg.setForeground(Color.red);
                                                                return;
                                                               
                                                        }
                                                        secondName_msg.setForeground(c);
                                                }
                                        });
                                        secondName_value.addTextListener(new TextListener(){
                                                public void textValueChanged(TextEvent e) {
                                                        if(secondName_value.getText().length()<13){
                                                                secondName_msg.setForeground(c);
                                                        }else{
                                                                secondName_msg.setForeground(Color.red);
                                                        }
                                                }
                                        });
//未完,见(二)[/code]

评分

参与人数 1技术分 +5 收起 理由
admin + 5 这样的代码最好分享工程让人下载测试

查看全部评分

5 个回复

倒序浏览
黑马网友  发表于 2011-8-4 19:31:06
沙发
额,不知道怎么直接分享工程啊
回复 使用道具 举报
黑马网友  发表于 2011-8-4 19:42:21
藤椅
前辈 你这是学多久的 造诣啊 我什么时候 能做出来呢
回复 使用道具 举报
黑马网友  发表于 2011-8-4 20:36:42
板凳

回复 藤椅 的帖子

正常学几个月吧,我在我们学校学了快俩年,很慢的。
回复 使用道具 举报
黑马网友  发表于 2011-8-4 20:53:39
报纸
要是只写个对话框实现互相对话功能,不难。但要添加各种功能,挺费劲的。而且主要功能写出来了,再写其他功能会挺没劲
回复 使用道具 举报
黑马网友  发表于 2011-8-4 21:04:43
地板
最好是给个工程打个包让我们测试吧
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马