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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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

7.ClientChatGroup类[code=java]package util.doTask;

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.Date;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import util.ClientMainWindow;

public class ClientChatGroup extends ClientMainWindow {
        private JLabel ps;//温馨提示文字
        private static JLabel number;//在线人数文字
        private JScrollPane main;//主显示滚动面板
        private static JTextArea mainText;//主显示文本区
        private JLabel my;//我的信息文字
        private JScrollPane second;//次显示滚动面板
        private static JTextArea secondText;//次显示文本区
        private static JList memberList;//显示全部在线用户名称的控件
        private JLabel to;//对文字
        private JTextField talkTo;//显示聊天对象的文本框
        private JComboBox face;//聊天表情选择下拉列表
        private String faces[]={"正经地","悄悄地","开心地","难过地","严肃地"};//向下拉列表中添加的文字,用数组表示
        private JRadioButton publicTalk;//公聊单选框
        private JRadioButton privateTalk;//私聊单选框
        private ButtonGroup publicANDprivate;//确定单选按钮分组的辅助控件
        private JCheckBox detach;//确认是否实现分屏的检查框
        private JLabel psDetach;//分屏并不起保密作用,要保密请选择私聊!文字
        private JLabel say;//说文字
        private JTextArea saying;//输入聊天信息的文本框
        private JScrollPane saying_pane;
        private JButton send;//发送按钮
        private JLabel changeColor;//换肤文字
        private JComboBox toChangeColor;//换肤选择框
        private String color[]={"默认皮肤","绿色心情","粉红浪漫"};//设置下拉列表中的皮肤
        private JComboBox bugOff;//滚屏选择框
        private String bugoffORnot[]={"滚屏","停止滚屏"};//设置下拉列表中的是否滚屏
        private JButton exit;//推出系统按钮
        private Timer timer;
        private Date dateTime;
        private PrintStream print;
       
        private ClientMainWindow cmw;
       
       
        public ClientChatGroup(ClientMainWindow c){
                cmw = c;
                chatInit();
        }


        private void chatInit(){
                this.setBounds(100, 60, 800, 650);
                this.setTitle("UNIX 聊天 当前用户:"+mySecondName);
                this.setResizable(false);
                this.setVisible(true);
                this.setLayout(null);
                this.addWindowListener(new WindowAdapter(){                       
                        public void windowClosing(WindowEvent e) {
                                exit();                       
                        }
                });       
               
               
               
               
               
               
               
               
                ps=new JLabel("温馨提示:文明聊天是一种美德");
                ps.setBounds(30,20,300,20);
                this.add(ps);
               
                number=new JLabel();
                number.setBounds(550,20,150,20);
                this.add(number);
               
                mainText=new JTextArea();
                mainText.setFocusable(false);
                main=new JScrollPane(mainText);
                main.setBounds(30, 50, 480, 430);
                this.add(main);
               
                my=new JLabel("我的信息");
                my.setBounds(30, 305, 300, 20);
                this.add(my);
               
                secondText=new JTextArea();
                secondText.setFocusable(false);
                second=new JScrollPane(secondText);
                second.setBounds(30,330,480,150);
                second.setVisible(false);
                this.add(second);
               
                memberList=new JList();
                memberList.setBounds(550,50,210,430);
                memberList.addListSelectionListener(new ListSelectionListener(){
                        public void valueChanged(ListSelectionEvent arg0) {
                                if(privateTalk.isSelected())
                                        talkTo.setText((String)memberList.getSelectedValue());
                        }
                });
                this.add(memberList);       

                memberList.setModel(model);
                userCount=model.getSize();
                this.number.setText("当前在线人数:"+userCount);
               
               
                to=new JLabel("对:");
                to.setBounds(30, 490, 30, 20);
                this.add(to);
               
                talkTo=new JTextField("所有人");
                talkTo.setBounds(65,490,60,20);
                talkTo.setFocusable(false);
                this.add(talkTo);
               
                face=new JComboBox(faces);
                face.setBounds(130, 490, 90, 20);
                this.add(face);
               
                publicTalk=new JRadioButton("公聊");
                publicTalk.setSelected(true);
                publicTalk.setBounds(225, 490, 55, 20);
                publicTalk.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent arg0) {
                                talkTo.setText("所有人");
                               
                        }
                });
                this.add(publicTalk);
                privateTalk=new JRadioButton("私聊");
                privateTalk.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e) {
                                memberList.setSelectedIndex(0);
                        }
                });
                privateTalk.setBounds(280, 490, 55, 20);
                this.add(privateTalk);
                publicANDprivate=new ButtonGroup();
                publicANDprivate.add(publicTalk);
                publicANDprivate.add(privateTalk);
               
                detach=new JCheckBox("分屏显示");
                detach.setBounds(335, 490, 100, 20);
                detach.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e) {
                                if(detach.isSelected()){
                                        main.setSize(480,250);
                                        main.validate();
                                        second.setVisible(true);
                                        my.setVisible(true);
                                        psDetach.setVisible(true);
                                }
                                else if(!detach.isSelected()){
                                        main.setSize(480,430);
                                        main.validate();
                                        second.setVisible(false);
                                        my.setVisible(false);
                                        psDetach.setVisible(false);
                                }
                        }
                }
                );
                this.add(detach);
               
                psDetach=new JLabel("分屏并不起保密作用,要保密请选择私聊!");
                psDetach.setForeground(Color.red);
                psDetach.setBounds(435, 490, 250, 20);
                psDetach.setVisible(false);
                this.add(psDetach);
               
                say=new JLabel("说:");
                say.setBounds(30, 520, 30, 20);
                this.add(say);
               
                saying=new JTextArea(20,3);
                saying_pane = new JScrollPane(saying);
                saying_pane.setBounds(60, 520, 375, 60);
                this.add(saying_pane);
               
                send=new JButton("发送");
                send.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent arg0) {
                                String temp = saying.getText();
                                if(temp.trim().equals("")){return;}
                                if(talkTo.getText().equals(userName)){return;}
                                if(talkTo.getText().equals("")){return;}
                                else if(talkTo.getText().equals("所有人")){
                                        try {
                                                publicTalk.setSelected(true);
                                                dos.writeUTF("[公聊]  <"+userName+">  "+face.getSelectedItem().toString()+" 对<"+talkTo.getText()+">"+"说: "+temp+"\n");                                               
                                                secondText.append("[公聊]  我  "+face.getSelectedItem().toString()+" 对<"+talkTo.getText()+">"+"说: "+temp+"\n");
                                        } catch (IOException e) {
                                                e.printStackTrace();
                                        }
                                }
                                else if(!talkTo.getText().equals("所有人")){
                                        try{
                                                dos.writeUTF("[私聊]  <"+userName+"> "+face.getSelectedItem().toString()+" 对 <"+allUserName.get(memberList.getSelectedIndex())+"> "+"说: "+temp+"\n");
                                                mainText.append("[私聊]  我  "+face.getSelectedItem().toString()+" 对 <"+talkTo.getText()+"> "+ "说:"+temp+"\n");
                                                secondText.append("[私聊]  我  "+face.getSelectedItem().toString()+" 对 <"+talkTo.getText()+"> "+ "说:"+temp+"\n");
                                                //print.print("[私聊]  我  "+face.getSelectedItem().toString()+" 对 <"+talkTo.getText()+"> "+ "说:"+temp+"\n");
                                        }catch(Exception e){
                                                e.printStackTrace();
                                        }                                       
                                }
                                saying.setText("");
                        }
                });
                send.setBounds(445, 530, 60, 20);
                this.add(send);
               
                changeColor=new JLabel("换肤");
                changeColor.setBounds(30, 590, 30, 20);
                this.add(changeColor);
               
                toChangeColor=new JComboBox(color);
                toChangeColor.setBounds(60, 590, 100, 20);
                toChangeColor.addItemListener(new ItemListener(){
                        public void itemStateChanged(ItemEvent e) {
                                if(toChangeColor.getSelectedIndex()==0){
                                        getContentPane().setBackground(new Color(234,234,234));
                                        publicTalk.setBackground(new Color(234,234,234));
                                        privateTalk.setBackground(new Color(234,234,234));
                                        detach.setBackground(new Color(234,234,234));
                                }else if(toChangeColor.getSelectedIndex()==1){
                                        getContentPane().setBackground(Color.green);
                                        publicTalk.setBackground(Color.green);
                                        privateTalk.setBackground(Color.green);
                                        detach.setBackground(Color.green);
                                }else if(toChangeColor.getSelectedIndex()==2){
                                        getContentPane().setBackground(Color.pink);
                                        publicTalk.setBackground(Color.pink);
                                        privateTalk.setBackground(Color.pink);
                                        detach.setBackground(Color.pink);
                                }
                        }                       
                });
                this.add(toChangeColor);
               
                bugOff=new JComboBox(bugoffORnot);
                bugOff.setBounds(170, 590, 80, 20);
                this.add(bugOff);
               
                exit=new JButton("关闭");
                exit.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent arg0) {
                                int num = (JOptionPane.showConfirmDialog(csmi,"确定退出?","提示信息",0,1));
                                if(num==0)
                                        exit();
                                return;
                        }
                });
                exit.setBounds(445, 560, 60, 20);
                this.add(exit);
               
                this.validate();
                }
        public void exit(){
                timer.stop();
                this.dispose();
                ccg = null;
        }
        public static void add(String msg){
                if(msg.charAt(0)=='0'||msg.charAt(0)=='1'){
                        mainText.append(msg.substring(1));
                        memberList.setModel(model);
                        memberList.validate();
                        number.setText("当前在线人数:"+userCount);
                }
                if(msg.charAt(0)=='2'){
                        mainText.append(msg.substring(1));
                }
                if(msg.charAt(0)=='3'){
                        mainText.append(msg.substring(1,msg.lastIndexOf("<")+1)+" 你 "+msg.substring(msg.lastIndexOf(">")));
                        secondText.append(msg.substring(1,msg.lastIndexOf("<")+1)+" 你 "+msg.substring(msg.lastIndexOf(">")));
                }
               
        }

}[/code]

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马