testclient.java

来自「用java写的聊天程序。」· Java 代码 · 共 94 行

JAVA
94
字号
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class TestClient extends JFrame implements ActionListener {
	
	DataInputStream dis;
	DataOutputStream dos;
	JTextField tf;
	JTextArea ta;
	String s11,s22;
	
	public TestClient(String s1,String s2)
	{
		this.setTitle("聊天程序客户端");
		JScrollPane jp=new JScrollPane();
		ta=new JTextArea(10,10);
		Panel p=new Panel();
		tf=new JTextField(20);
		JButton b=new JButton("发送");
		b.addActionListener(this);
		tf.addActionListener(this);
		p.add(tf);
		p.add(b);
		jp.setViewportView(ta);
		this.getContentPane().add(jp);
		this.getContentPane().add("South",p);
		this.setSize(350,250);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.s11=s1;
		this.s22=s2;
		this.setVisible(true);
		tf.requestFocus();
		this.connect();
		this.createReadThread();
	}
	
	public void connect()
	{
		try
		{
			Socket s2=new Socket(s22,911);
			InputStream is=s2.getInputStream();
			dis=new DataInputStream(is);
			OutputStream os=s2.getOutputStream();
			dos=new DataOutputStream(os);
		}
		catch(IOException e)
		{
			System.out.println("连接服务器故障!");
		}
	}
	
	public void createReadThread()
	{
		ReadThread rt=new ReadThread(this.ta,this.dis);
		rt.start();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		try
		{
			String s=tf.getText();
			dos.writeUTF(s11+"说:"+s);
			ta.append("自己说:"+s);
			ta.append("\n");
			tf.setText("");
			tf.requestFocus();
		}
		catch(IOException e1)
		{
			e1.printStackTrace();
		}
		// TODO Auto-generated method stub

	}

}

⌨️ 快捷键说明

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