일대일 채 팅 프로그램의 실현
                                            
 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("    ! ");
		}
	}
}이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.