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

📄 chatbase.java

📁 实现TCP/UDP的socket编程
💻 JAVA
字号:
package business;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.util.Calendar;
import java.util.Enumeration;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import Form.*;
public class ChatBase extends JFrame implements ActionListener{

	/**
	 * Launch the application
	 * @param args
	 */
	protected int userNum;
	protected int friendNum;
	protected String userName;
	protected boolean openRecrod = false;
	
	protected JLabel infomation;
	protected JTextArea input;
	protected JTextArea output;
	protected JPanel pnlButtom;
	protected JPanel pnlCenter;
	protected JButton btnSend;
	protected JButton btnCancel;
	protected JButton btnRecord;
	protected Client parent;
	protected JScrollPane panel;
	protected JTextArea recrod;
	//用于初始控件和布局
	public void setup()
	{
		infomation = new JLabel("");
		String info = User.getInfo(friendNum);
		infomation.setText(info);
		input = new JTextArea();
		output = new JTextArea();
		JScrollPane jspInput = new JScrollPane(input,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		JScrollPane jspOutput = new JScrollPane(output,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		pnlButtom = new JPanel();
		pnlCenter = new JPanel();
		pnlButtom.setLayout(new FlowLayout());
		btnSend = new JButton("发送");
		btnCancel = new JButton("取消");
		btnRecord = new JButton("聊天记录");
		btnSend.addActionListener(this);
		btnCancel.addActionListener(this);
		btnRecord.addActionListener(this);
		pnlCenter.setLayout(new BorderLayout());
		pnlCenter.add("Center",jspInput);
		pnlCenter.add("South",jspOutput);
		javax.swing.JSplitPane js = new JSplitPane(JSplitPane.VERTICAL_SPLIT,jspInput,jspOutput);
		js.setDividerSize(4);
		jspInput.setPreferredSize(new Dimension(200,200));
		
		pnlButtom.add(btnSend);
		pnlButtom.add(btnCancel);
		pnlButtom.add(btnRecord);
		
		recrod = new JTextArea();
		recrod.setPreferredSize(new Dimension(200,280));
		panel = new JScrollPane(recrod,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		Read();
		this.getContentPane().add(panel,"East");
		
		getContentPane().add("North",infomation);
		getContentPane().add("Center",pnlCenter);
		getContentPane().add("South",pnlButtom);
		getContentPane().add(js);
		input.setEditable(false);
		
	}
	//写聊天记录信息
	public void Save()
	{
		String recrod = input.getText();
		File f = new File("recrods\\"+userNum+"to"+friendNum+".txt");
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(f,true);
			fos.write(recrod.getBytes());
		} 
		catch (FileNotFoundException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}
	
	//读聊天记录信息
	public void Read()
	{
		int width = 200;
		int height = 280;
		int row = -10;
		recrod.setText("");
		File f = new File("recrods\\"+userNum+"to"+friendNum+".txt");
		if(!f.exists())
		{
			recrod.append("          ");
			return;
		}
		FileReader fis;
		try {
			fis = new FileReader(f);
			BufferedReader bif = new BufferedReader(fis);
			String temp;
			while((temp=bif.readLine())!=null)
			{
				recrod.append(temp+"\n");
				row ++;
				if(row>0)
				{
					height +=20;
					recrod.setPreferredSize(new Dimension(200,height));
				}
			}
		} catch (FileNotFoundException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}
	
	public ChatBase() 
	{
		
	}
	
	public void setMessage(String string) {
	}
	
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成方法存根
	}
}

⌨️ 快捷键说明

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