일대일 채 팅 프로그램의 실현

4975 단어 자바
TestServer.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class TestServer extends JFrame implements ActionListener {
	DataInputStream dis;
	DataOutputStream dos;
	JTextField tf;
	JTextArea ta;
	public TestServer(){
		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.add(jp);
		this.add("South",p);
		this.setSize(350,250);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		tf.requestFocus();
		this.connect();//    
		this.createReadThread();//        
		
	}
	public void connect(){
		try{
			ServerSocket ss = new ServerSocket(911);//      
			Socket s2 = ss.accept();
			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();
	}
	public void actionPerformed(ActionEvent e){
			try{
				String s = tf.getText();
				dos.writeUTF(s);
				ta.append("   ; "+s	);
				ta.append("
"); tf.setText(""); tf.requestFocus(); }catch(IOException e1){ e1.printStackTrace(); } } public static void main(String[] args){ new TestServer(); } }
Login.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Login extends JFrame implements ActionListener {
	JLabel l1;
	JLabel l2;
	JTextField tf1;
	JTextField tf2;
	JButton b1;
	public Login(){
		l1=new JLabel("       :");
		l2=new JLabel("        :");
		tf1=new JTextField("  ");
		tf2=new JTextField("127.0.0.1");
		tf1.addActionListener(this);
		tf2.addActionListener(this);
		b1=new JButton("Login");
		b1.addActionListener(this);
		JPanel p1 = new JPanel();
		p1.add(l1);
		p1.add(tf1);
		JPanel p2 =new JPanel();
		p2.add(l2);
		p2.add(tf2);
		this.add(p1,"North");
		this.add(p2);
		this.add(b1,"South");
		this.setSize(300,200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
	}
	public void actionPerformed(ActionEvent e){
		if(!tf1.getText().equals("")&&!tf2.getText().equals("")){
			TestClient c=new TestClient(tf1.getText(),tf2.getText());//         
			this.setVisible(false);
		}
	}
	public static void main(String[] args){
		new Login();
	}

}
TestClient.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
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);
		JPanel p =new JPanel();
		tf = new JTextField(20);
		JButton b =new JButton("  ");
		b.addActionListener(this);
		tf.addActionListener(this);
		p.add(tf);
		p.add(b);
		jp.setViewportView(ta);
		this.add(jp);
		this.add(p,"South");
		
		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();
	}

	public void actionPerformed(ActionEvent e) {
		try{
			String s =tf.getText();
			dos.writeUTF(s11+" : "+ s);
			ta.append("   : " +s);
			ta.append("
"); tf.setText(""); tf.requestFocus(); }catch(IOException e1){ e1.printStackTrace(); } } }
ReadThread.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class ReadThread extends Thread {

	JTextArea ta;
	DataInputStream dis;
	public ReadThread(JTextArea t,DataInputStream d){
		this.ta = t;
		this.dis =d;
	}
	public void run(){
		try{
			while(true){
				ta.append("   : "+dis.readUTF());//         
				ta.append("
"); } }catch(IOException e){ System.out.println(" ! "); } } }

좋은 웹페이지 즐겨찾기