⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatroom.java

📁 java编写聊天室
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.util.StringTokenizer;
import javax.swing.*;

class chatRoom extends JFrame  implements Runnable,ActionListener,ItemListener  
{  
	//聊天主界面控件的添加
	Panel p,p0;
	JLabel id,msge,saying,tip,image,face;
    List list;
    JTextArea chat_txt;
    JTextArea msg_txt;
    ScrollPane sp,sp0;
    Choice ch1,ch2,ch3;
    Button send,openfile;
    
    ImageIcon img;
    JCheckBox c1,c2;
    JRadioButton b1,b2;
    ButtonGroup bg;

     Socket soc=null;
     PrintStream writer=null;
     BufferedReader reader;
     String U_Name;
     StringTokenizer st;
     Boolean keyBoard=false;
    
     
     //添加表情按钮
     JButton p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20;
     
     public chatRoom(Socket socket,String Uname) 
     {
    	 
    	super("聊天室");
    	this.soc=socket;
    	this.U_Name=Uname;
    	
        Init();    //调用初始化函数;
    	try
    	{
    		writer=new PrintStream(soc.getOutputStream());
    		reader=new BufferedReader(new InputStreamReader(soc.getInputStream()));	
    	}
    	catch(IOException e)
    	{
    		
    	}
    	
    }
     
     //初始化图形界面
     public void Init()
     {
    	 //创建所有的组件
    	  
    	  list=new List();
      	  c1=new JCheckBox("黑体",false);
      	  c2=new JCheckBox("斜体",false);
      	  b1=new JRadioButton("私聊",false);
      	  b2=new JRadioButton("群聊",true);
           chat_txt=new JTextArea();
           chat_txt.setLineWrap(true);
           msg_txt=new JTextArea();
           sp=new ScrollPane(0);
           
           p=new Panel();
           p.setLayout(new GridLayout(0,1));
           p.add( msg_txt);
           sp.add(p);
           
           p0=new Panel();
           sp0=new ScrollPane(0);
           p0.setLayout(new GridLayout(0,1));
           p0.add( chat_txt);
           sp0.add(p0);
           msg_txt.setText("请输入聊天信息");
           msg_txt.setLineWrap(true);
           tip=new JLabel();
           tip.setText("请选择你喜欢的字体颜色");
           img=new ImageIcon("images/bg.gif");
           image=new JLabel(img);
           saying=new JLabel();
          
          send=new Button();
          ButtonGroup bg=new ButtonGroup();
      	   bg.add(b1);
      	   bg.add(b2);;
          
          
          /*引入表情图片*/
  		ImageIcon h1=new ImageIcon("images/1.gif");
  		ImageIcon h2=new ImageIcon("images/2.gif");
  		ImageIcon h3=new ImageIcon("images/3.gif");
  		ImageIcon h4=new ImageIcon("images/4.gif");
  		ImageIcon h5=new ImageIcon("images/5.gif");
  		
  		ImageIcon h6=new ImageIcon("images/6.gif");
  		ImageIcon h7=new ImageIcon("images/7.gif");
  		ImageIcon h8=new ImageIcon("images/8.gif");
  		ImageIcon h9=new ImageIcon("images/9.gif");
  		ImageIcon h10=new ImageIcon("images/10.gif");
  		
  		ImageIcon h11=new ImageIcon("images/11.gif");
  		ImageIcon h12=new ImageIcon("images/12.gif");
  		ImageIcon h13=new ImageIcon("images/13.gif");
  		ImageIcon h14=new ImageIcon("images/14.gif");
  		ImageIcon h15=new ImageIcon("images/15.gif");
  		
  		ImageIcon h16=new ImageIcon("images/16.gif");
  		ImageIcon h17=new ImageIcon("images/17.gif");
  		ImageIcon h18=new ImageIcon("images/18.gif");
  		ImageIcon h19=new ImageIcon("images/19.gif");
  		ImageIcon h20=new ImageIcon("images/20.gif");
          
          
  		face=new JLabel(h1);
          openfile=new Button();
          openfile.setLabel(" 打开文件");
          send.setLabel(" 发送");
          send.setSize(70,30);
      	id=new JLabel("在线用户",JLabel.CENTER);
          msge=new JLabel("聊天信息",JLabel.CENTER);
          
         // 设置监听 
      	openfile.addActionListener(this);
      	openfile.setActionCommand("open");
      	send.addActionListener(this);
      	b1.addActionListener(this);
      	b2.addActionListener(this);
      	c1.addActionListener(this);
      	c2.addActionListener(this);
 
      	
      	//创建下拉列表
          ch1=new Choice();
          ch1.addItem("轻声细语");
          ch1.addItem("不好意思");
          ch1.addItem("得意洋洋");
          ch1.addItem("慢条斯理");
          ch1.addItem("无精打采");
          ch1.addItem("兴高采烈");
          ch1.addItem("慢条斯理");
          ch1.addItem("幸灾乐祸");
          ch1.addItem("哭笑不得");
          ch1.addItem("开怀大笑");
          ch1.addItem("欲哭无泪");
           
          ch1.addItemListener(this);   
         
          ch2=new Choice();
          ch2.addItem("沉静黑色");
          ch2.addItem("热情红色");
          ch2.addItem("拘谨紫色");
          ch2.addItem("开朗蓝色");
          ch2.addItem("灰色轨迹");
          ch2.addItem("浪漫桃色");
          ch2.addItem("青春绿色");
          ch2.addItem("清爽青色");
          //ch2.select("开朗蓝色");
                                    
        ch2.addItemListener(this);   
       
      	chat_txt.setEditable(false);
      	saying.setText("地说");
      	
      	ch3=new Choice();
      	GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
     	    String fontName[]=ge.getAvailableFontFamilyNames();
     	     for(int i=0;i<fontName.length;i++)
     	     {
     	          ch3.addItem(fontName[i]);
     	          }
     	   ch3.addItemListener(this);
     	   
      	//字体的设置
      	Font myFont=new Font("TimesToman",Font.BOLD,18);
      	id.setFont(myFont);
      	msge.setFont(myFont);
      	
      	//得到内容面板
      	
        Container container=getContentPane();
        //设置布局管理器
       	container.setLayout(null);
       	//添加组件到内容面板
       	container.add(ch3);
       	container.add(ch2);
       	container.add(ch1);
       	container.add(saying);
       	container.add(sp);
       	container.add(send);
  		container.add(id);
  		container.add(msge);
  		container.add(list);
  		container.add(sp0);
  		container.add(image);
  		container.add(face);
  		container.add(tip);
  		container.add(openfile);
  		container.add(c1);
		container.add(c2);
		container.add(b1);
		container.add(b2);
		//container.add(scrollpane);
		//container.add(scrollpane1);
      	
		//设置组件在面板中的位置和大小
      	id.setBounds(50,  30, 150, 30);
      	msge.setBounds(400, 30, 150, 30);
      	list.setBounds(50, 60, 150,200 );
      	sp0.setBounds(260, 60, 500,200 );
      	ch1.setBounds(170, 350, 200, 30);
      	ch2.setBounds(170, 440, 200, 30);
      	ch3.setBounds(170, 380, 200, 30);
      	saying.setBounds(380, 350, 50, 30);
        sp.setBounds(420, 320, 290, 250);
      	send.setBounds(720, 530, 70, 30);
      	face.setBounds(720, 350, 70, 30);
      	openfile.setBounds(720, 480, 70, 30);
         image.setBounds(0, 300, 800, 20);
      	tip.setBounds(180, 400, 200, 30);
      	c1.setBounds(170, 480, 70, 30);
    	c2.setBounds(260, 480, 70, 30);
    	b1.setBounds(200, 30, 70, 30);
    	b2.setBounds(280, 30, 70, 30);
    	//scrollpane.setBounds(760, 60, 20,200 );
    	//scrollpane1.setBounds(710, 320, 20,250);
   	/*************************添加表情代码*************************/
  	   p1=new JButton(h1);
  	   p2=new JButton(h2);
  	   p3=new JButton(h3);
  	   p4=new JButton(h4);
  	   p5=new JButton(h5);
  	   
  	   p6=new JButton(h6);
  	   p7=new JButton(h7);
  	   p8=new JButton(h8);
  	   p9=new JButton(h9);
  	   p10=new JButton(h10);
  	   
  	   p11=new JButton(h11);
  	   p12=new JButton(h12);
  	   p13=new JButton(h13);
  	   p14=new JButton(h14);
  	   p15=new JButton(h15);
  	   
  	   p16=new JButton(h16);
  	   p17=new JButton(h17);
  	   p18=new JButton(h18);
  	   p19=new JButton(h19);
  	   p20=new JButton(h20);
  	   
  	   
  	   //设置按钮提示文本
  	   p1.setToolTipText("调皮");
  	   p2.setToolTipText("再见");
  	   p3.setToolTipText("嘘..");
  	   p4.setToolTipText("鲜花");
  	   p5.setToolTipText("疑问");
  	   p6.setToolTipText("生气");
  	   p7.setToolTipText("闭嘴");
  	   p8.setToolTipText("惊奇");
  	   p9.setToolTipText("下雨");
  	   p10.setToolTipText("太阳");
  	   
  	   p11.setToolTipText("大哭");
  	   p12.setToolTipText("偷笑");
  	   p13.setToolTipText("握手");
  	   p14.setToolTipText("厉害");
  	   p15.setToolTipText("睡觉");
  	   p16.setToolTipText("汗..");
  	   p17.setToolTipText("酷!");
  	   p18.setToolTipText("可爱");
  	   p19.setToolTipText("爱情");
  	   p20.setToolTipText("敲打");
  	   
  	   //给按钮添加监听事件
  	   p1.addActionListener(this);
  	   p2.addActionListener(this);
  	   p3.addActionListener(this);
  	   p4.addActionListener(this);
  	   p5.addActionListener(this);
  	   p6.addActionListener(this);
  	   p7.addActionListener(this);
  	   p8.addActionListener(this);
  	   p9.addActionListener(this);
  	   p10.addActionListener(this);
  	   
  	   p11.addActionListener(this);
  	   p12.addActionListener(this);
  	   p13.addActionListener(this);
  	   p14.addActionListener(this);
  	   p15.addActionListener(this);
  	   p16.addActionListener(this);
  	   p17.addActionListener(this);
  	   p18.addActionListener(this);
  	   p19.addActionListener(this);
  	   p20.addActionListener(this);
  	   
  	   //
  	   container.add(p1);
  	   container.add(p2);
  	   container.add(p3);
  	   container.add(p4);
  	   container.add(p5);
  	   container.add(p6);
  	   container.add(p7);
  	   container.add(p8);
  	   container.add(p9);
  	   container.add(p10);
  	   
  	   container.add(p11);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -