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

📄 chatgui.java

📁 java简易在线聊天程序
💻 JAVA
字号:
package chatDog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class chatGUI extends JFrame implements Const,ActionListener{
	private Friend friend;
	private String myName;
	private Sendable sender;
	private JTextArea receiveContent=new JTextArea();
	private JTextArea sendContent=new JTextArea();
	private JButton btnClose=new JButton(" 关闭窗口 ");
	private JButton btnClear=new JButton("清除聊天记录");
	private JButton btnSend=new JButton(" 发送信息 ");
	//private String message=null;
	
	chatGUI(Friend frd,String name){
		friend=frd;
		myName=name;
		this.setSize(600,400);
		this.setLocation(330,200);
		this.setTitle(name+"与"+frd.name+"聊天");
		this.setLayout(new BorderLayout(20,20));
		this.addWindowListener(new WindowAdapter(){
								 public void windowClosing(WindowEvent e){
								 	//message=CHATGUI_CLOSING;
								 	setVisible(false);
								    friend.isWindowShow=false;
								    //friend.chatWindow=null;
								    //System.out.println("WindowClose");
								 }
							   });
		
		btnClose.setActionCommand(BTN_CLOSE);
		btnClear.setActionCommand(BTN_CLEAR);
		btnSend.setActionCommand(BTN_SEND);
		btnClose.addActionListener(this);
		btnClear.addActionListener(this);
		btnSend.addActionListener(this);
		JPanel textPanel=new JPanel();
		JPanel btnPanel=new JPanel();
		receiveContent.setEditable(false);
		JScrollPane rSP=new JScrollPane(receiveContent);
		JScrollPane sSP=new JScrollPane(sendContent);
		
		textPanel.setLayout(new BoxLayout(textPanel,BoxLayout.Y_AXIS));
		textPanel.add(rSP);
		textPanel.add(sSP);
		this.add(textPanel,"Center");
		
		btnPanel.add(btnClose,"South");
		btnPanel.add(btnClear,"South");
		btnPanel.add(btnSend,"South");
		
		this.add(btnPanel,"South");
	}
	public void showGUI(){
		receiveContent.setText(friend.chatNote);
		if(!friend.isWindowShow){
			this.setVisible(true);
			friend.isWindowShow=true;
		}
	}
	public void showGUI(String msg){
		receiveContent.setText(friend.chatNote+'\n'+msg);
		if(!friend.isWindowShow){
			this.setVisible(true);
			friend.isWindowShow=true;
		}
	}
	/*public String getMessage(){
		if(message==null) return null;
		return new String(message);
	}*/
	public boolean sendMessage(String msg){
		Socket sock=null;
		PrintStream spw=null;
		try{
			System.out.println("Socket-SendMessage:"+friend.IP+":"+friend.port);
		    sock=new Socket(friend.IP,friend.port);
		    spw=new PrintStream(sock.getOutputStream());
		    spw.println(msg);
		    System.out.println("Message to Send:"+msg);
		}catch(Exception e){
			try{sock.close();}catch(Exception ee){}
			friend.setIsOnline(false);
			return false;
		}
		try{sock.close();}catch(Exception e){}
		return true;
	}
	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand().equals(BTN_SEND)) {
			friend.chatNote=friend.chatNote+ENTER_CHAR+myName+":"+sendContent.getText();
			String msgToSend=MSG_CHAT+ENTER_CHAR+friend.name+ENTER_CHAR+myName+ENTER_CHAR+
			                 sendContent.getText().replace(ENTER_CHAR,ENTER_REPLACE_CHAR)+ENTER_CHAR;
			receiveContent.setText(friend.chatNote);
			if(!sendMessage(msgToSend)) {
				this.showGUI(sendContent.getText()+"未能成功发送!");
				friend.chatNote=friend.chatNote+ENTER_CHAR+"未能成功发送!";
			}
			sendContent.setText("");
		}
		else if (e.getActionCommand().equals(BTN_CLEAR)) {
			friend.chatNote=null;
			this.showGUI();
		}else if(e.getActionCommand().equals(BTN_CLOSE)) {
			this.setVisible(false);
			friend.isWindowShow=false;
			//friend.chatWindow=null;
		}
	}
}

⌨️ 快捷键说明

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