ChatArea.java

package chatroom;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
public class ChatArea extends Panel implements ActionListener,Runnable{
 Socket socket=null;
 DataInputStream in=null;
 DataOutputStream out=null;
 Thread threadMessage=null;
 TextArea publicChat,privateChat;
 TextField msg=null;
 Button b1,b2,b3;
 Label tishi=null;
 String name=null;
 Hashtable listTable;
 List listComponent=null;
 Choice privateChatList;
 int width,height;
 
 public ChatArea (String name,Hashtable listTable,int width,int height){
  setLayout(null);
  setBackground(Color.orange);
  this.width=width;
  this.height=height;
  setSize(width,height);
  this.listTable=listTable;
  this.name=name;
  threadMessage=new Thread(this);//用来监听来自服务器端的信息
  publicChat=new TextArea(10,10);
  privateChat=new TextArea(10,10);
  b1=new Button("送出信息到:");
  b2=new Button("刷新谈话区:");
  b3=new Button("刷新私聊区: ");
  tishi=new Label("双击可以私聊",Label.CENTER);
  msg=new TextField(28);
     b1.addActionListener(this);
     msg.addActionListener(this);
     b2.addActionListener(this);
     b3.addActionListener(this);
     listComponent=new List();
     listComponent.addActionListener(this);//双击可以私聊
    
     privateChatList=new Choice();
     privateChatList.add("大家(*)");
     privateChatList.select(0);
 
     add(publicChat);
     publicChat.setBounds(10,10,(width-120)/2,(height-120));
     add(privateChat);
     privateChat.setBounds(10+(width-120)/2,10,(width-120)/2,(height-120));
     add(listComponent);
     listComponent.setBounds(10+(width-120),10,100,(height-160));
     add(tishi);
     tishi.setBounds(10+(width-120),10+(height-160),110,40);
    
     Panel pSouth=new Panel();
     pSouth.add(msg);
     pSouth.add(b1);
     pSouth.add(privateChatList);
     pSouth.add(b2);
     pSouth.add(b3);
     add(pSouth);
     pSouth.setBounds(10,20+(height-120),width-20,60);
 }
 
 public void setName(String s){
  name=s;
 }
 
 public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out){
  this.socket=socket;
  this.in=in;
  this.out=out;
  try{
   threadMessage.start();
  }catch(Exception e){
  }
 }
 
 public void actionPerformed(ActionEvent e){
  System.out.print("hahahaha1");
  if(e.getSource()==b1||e.getSource()==msg){
   String message="";
   String people=privateChatList.getSelectedItem();
   people=people.substring(0,people.indexOf("("));
   message=msg.getText();
   if(message.length()>0){
    try{
     if(people.equals("大家"))
               out.writeUTF("公共聊天内容:"+name+"说:"+message);
              else
               out.writeUTF("私人聊天内容:"+name+"悄悄地说:"+message+"#"+people);
    }catch(IOException ee){
    }
   }
  }
  else if(e.getSource()==listComponent){//处理双击事件
   privateChatList.insert(listComponent.getSelectedItem(), 0);
   privateChatList.repaint();
  }else if(e.getSource()==b2){
   publicChat.setText(null);}
  else if(e.getSource()==b3){
   privateChat.setText(null);}
 }
   
 
 
 public void run(){
  while(true){
   String s=null;
   try{
    s=in.readUTF();
    if(s.startsWith("公共聊天内容:")){
     String content=s.substring(s.indexOf(":")+1);
     publicChat.append("/n"+content);
    }
    if(s.startsWith("私人聊天内容:")){
     String content=s.substring(s.indexOf(":")+1);
     privateChat.append("/n"+content);
    }else if(s.startsWith("聊天者:")){
     String people=s.substring(s.indexOf(":")+1,s.indexOf("性别"));
     String sex=s.substring(s.indexOf("性别")+2);
     listTable.put(people, people+"("+sex+")");
     listComponent.add((String)listTable.get(people));
     listComponent.repaint();
    }else if(s.startsWith("用户离线:")){
     String awayPeopleName=s.substring(s.indexOf(":")+1);
     listComponent.remove((String)listTable.get(awayPeopleName));
     listComponent.repaint();
     publicChat.append("/n"+(String)listTable.get(awayPeopleName)+"");
     listTable.remove(awayPeopleName);
    }
    Thread.sleep(5);
   }catch(IOException e){
    listComponent.removeAll();
    listComponent.repaint();
    listTable.clear();
    publicChat.setText("和服务器的连接已经断开/n必须刷新浏览器才能再次进入聊天室。");
    break;
   }catch(InterruptedException e){}
  }
  
 }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package Socker; import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class Server extends JFrame { private JTextArea chatArea; private JTextField inputField; private final int port = 8000; private Socket clientSocket; private BufferedReader reader; private PrintWriter writer; public Server() { setDefaultCloseOperation(EXIT_ON_CLOSE); setTitle("这里是服务器!"); setBounds(750, 100, 800, 600); setAlwaysOnTop(true); JPanel chatPanel = new JPanel(new BorderLayout()); chatArea = new JTextArea(); chatArea.setEditable(false); JScrollPane jScrollPane = new JScrollPane(chatArea); chatPanel.add(jScrollPane, BorderLayout.CENTER); JPanel inputPanel = new JPanel(new FlowLayout()); inputField = new JTextField(50); inputField.setPreferredSize(new Dimension(100, 30)); JButton sendButton = new JButton("发送"); sendButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { sendMessage(inputField.getText()); } }); inputPanel.add(inputField); inputPanel.add(sendButton); Container container = getContentPane(); container.add(chatPanel, BorderLayout.CENTER); container.add(inputPanel, BorderLayout.SOUTH); setVisible(true); startServer(); } private void sendMessage(String text) { if (!text.isEmpty()) { String message = "【服务器】: " + text + "\n"; chatArea.append(message); inputField.setText(""); sendToClient(message); } } private void sendToClient(String message) { writer.println(message); } private void startServer() { try { ServerSocket serverSocket = new ServerSocket(port); System.out.println("服务器已启动,等待客户端连接"); clientSocket = serverSocket.accept(); System.out.println("客户端连接成功:" + clientSocket); reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); writer = new PrintWriter(clientSocket.getOutputStream(), true); new Thread(new ClientListener()).start(); } catch (IOException e) { e.printStackTrace(); } } private class ClientListener implements Runnable { @Override public void run() { try { String message; while ((message = reader.readLine()) != null) { chatArea.append(message + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { clientSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Server().startServer(); } }); } }这个代码为何无界面显示
05-31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值