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

📄 chatclient.java

📁 网络通信系统
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;

public class ChatClient implements ActionListener{
  public static String ip = "192.168.1.101";	
  public static int port = 6666;
  public static String username = "游客";
  Socket socket;
  
  JFrame frame;
  JLabel label;
  JPanel panel;
  
  JScrollPane userscrollpane;//窗口滚动条
  JScrollPane messagescrollpane;//窗口滚动条
  
  JTextArea usershow;//用户列表窗口
  JTextArea messageshow;//消息窗口
  
  Box box1;
  Box box2;
  Box box3;
  
  JLabel label1;
  JLabel label2;
  
  JComboBox userlist;//选择发送消息的接收者
  
  JCheckBox checkbox;//悄悄话
  
  JTextField clientmessage;//客户端消息的发送
  
  JButton sentmessage;//发送消息
  JButton set;//设置IP,端口和用户名
  JButton connect;//连接服务器
  JButton cut;//与服务器断开连接
  JButton exit;//退出程序
  
  Dimension framesize;//窗口的尺寸
  Dimension screensize;//屏幕的尺寸
  
  ObjectOutputStream output;
  ObjectInputStream input;
  
  ClientReceive clientrecsen;
  
  public ChatClient(){
	frame = new JFrame("聊天");
    Container contentpane = frame.getContentPane();
    frame.setLayout(new BorderLayout(5,10));
    
    label = new JLabel("在线人数                      聊天内容");
    contentpane.add(label,BorderLayout.NORTH);
    
    usershow = new JTextArea(20,10);
	usershow.setEnabled(false);
	userscrollpane = new JScrollPane
	(usershow,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
			  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	contentpane.add(userscrollpane,BorderLayout.WEST);
	messageshow = new JTextArea(20,50);
	messageshow.setEnabled(false);
	messagescrollpane = new JScrollPane
	(messageshow,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
			  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	contentpane.add(messagescrollpane,BorderLayout.CENTER);
	
	panel = new JPanel();
	panel.setLayout(new GridLayout(3,1,0,10));
	
	label1 = new JLabel("对");
	label2 = new JLabel("说");
	userlist = new JComboBox();
	userlist.insertItemAt("所有人",0);
	userlist.setSelectedIndex(0);
	checkbox = new JCheckBox("悄悄话");
	checkbox.setSelected(false);
	box1 = Box.createHorizontalBox();
	box1.add(label1);
	box1.add(userlist);
	box1.add(label2);
	box1.add(Box.createHorizontalStrut(480));
	box1.add(checkbox);
    panel.add(box1);
    
    clientmessage = new JTextField();
    sentmessage = new JButton("发送");
    sentmessage.addActionListener(this);
    box2 = Box.createHorizontalBox();
    box2.add(clientmessage);
    box2.add(Box.createHorizontalStrut(20));
    box2.add(sentmessage);
    panel.add(box2);
    
    set = new JButton("设置");
    set.addActionListener(this);
    connect = new JButton("连接");
    connect.addActionListener(this);
    cut = new JButton("断开");
    cut.setEnabled(false);
    cut.addActionListener(this);
    exit = new JButton("关闭");
    exit.addActionListener(this);
    box3 = Box.createHorizontalBox();
    box3.add(Box.createHorizontalStrut(60));
    box3.add(set);
    box3.add(Box.createHorizontalStrut(100));
    box3.add(connect);
    box3.add(Box.createHorizontalStrut(100));
    box3.add(cut);
    box3.add(Box.createHorizontalStrut(100));
    box3.add(exit);
    panel.add(box3);
	
	contentpane.add(panel,BorderLayout.SOUTH);
	
	frame.pack();
    //设置运行时窗口的位置
    framesize = frame.getSize();
    screensize = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int)(screensize.width - framesize.getWidth())/2;
    int y = (int)(screensize.height - framesize.getHeight())/2;
    frame.setLocation(x,y);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    frame.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
          Cut();
    	  System.exit(0);
      }
    });
  }
  
  public void actionPerformed(ActionEvent e){//事件处理
	Object obj = e.getSource();
	if(obj == sentmessage) SentMessage();
	else if(obj == set) new Set(frame);
	else if(obj == connect) Connect();
	else if(obj == cut) {
	  int i = JOptionPane.showConfirmDialog
	          (frame,"真的断开吗?","断开",
	          JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);
      if(i == JOptionPane.YES_OPTION) Cut();
	}
	else if(obj == exit){
	  int i = JOptionPane.showConfirmDialog
	          (frame,"真的要关闭吗?","关闭",
	          JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);
      if(i == JOptionPane.YES_OPTION){
    	Cut();
    	System.exit(0);
      }
	}
  }
  
  public void Connect(){//连接到服务器
	try{
	  socket = new Socket(ip,port);
    }
	catch(Exception e){
	  JOptionPane.showConfirmDialog(frame,"不能连接到指定服务器.\n请确认连接设置是否正确.",
			                        "提示",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
	  return;
	}
	try{
	  set.setEnabled(false);
	  connect.setEnabled(false);
	  cut.setEnabled(true);
	  
	  messageshow.append("连接服务器成功\n");
	  
	  output = new ObjectOutputStream(socket.getOutputStream());
	  output.flush();
	  input = new ObjectInputStream(socket.getInputStream());
	  
	  output.writeObject(username);	
	  output.flush();
	  
	  clientrecsen = new ClientReceive(socket,usershow,messageshow,
			                          userlist,output,input,set,connect,cut);
	  clientrecsen.start();
	}
	catch(Exception e){
	  //System.out.println(e);	
	}
  }
  
  public void  Cut(){//断开与服务器的连接
	set.setEnabled(true);
	connect.setEnabled(true);
	cut.setEnabled(false);
	try{
	  if(socket.isClosed()) return;
	  output.writeObject("用户下线");//发送用户下线消息给服务器
	  output.flush();
	  output.close();
	  input.close();
	  socket.close();
	  messageshow.append("已经与服务器断开连接...");
	}
	catch(Exception e){
	  //System.out.println(e);	
	}
  }
  
  public void SentMessage(){//发送聊天消息
	String tosomebody = userlist.getSelectedItem().toString();
	String status = "";
	if(checkbox.isSelected()) status = "悄悄话";
	String msg = clientmessage.getText();
	if(socket.isClosed()){
	  return;	
	}
	try{
	  output.writeObject("聊天消息");
	  output.flush();
	  output.writeObject(tosomebody);
	  output.flush();
	  output.writeObject(status);
	  output.flush();
	  output.writeObject(msg);
	  output.flush();
	}
	catch(Exception e){
	  //System.out.println(e);	
	}
	clientmessage.setText("");
  }
  
  public static void main(String args[]){
	new ChatClient();  
  }
  
}

⌨️ 快捷键说明

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