标签:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; public class Person1 extends JFrame{ private static JTextArea p1ta; private JTextField p1tf; private JButton p1b; //构造方法 public Person1() { super("小明"); } public void init1() { this.setLayout(null); p1ta = new JTextArea(); p1tf = new JTextField(); p1b = new JButton("发送"); p1ta.setBounds(0, 0, 500, 300); p1tf.setBounds(0, 315, 400, 50); p1b.setBounds(420, 320, 60, 40); buttonLisenter(); this.add(p1ta); this.add(p1tf); this.add(p1b); this.setSize(500, 400); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public JTextArea getP1ta() { return p1ta; } //按钮监听器 private void buttonLisenter() { p1b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String str = p1tf.getText(); p1tf.setText(""); p1ta.append("我:"+"\r\n"+str + "\r\n"); try { Socket soc = new Socket(InetAddress.getLocalHost(),8888); OutputStream ots = soc.getOutputStream(); PrintStream ps = new PrintStream(ots); ps.print(str+"\r\n"); ps.close(); ots.close(); } catch (UnknownHostException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } }); } //测试类 public static void main(String[] args) { Person1 p1 = new Person1(); p1.init1(); Thread t1 = new Thread(new RunableP1(p1ta)); t1.start(); } }
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; public class Person2 extends JFrame{ private static JTextArea p2ta; private JTextField p2tf; private JButton p2b; public void init2() { this.setLayout(null); p2ta = new JTextArea(); p2tf = new JTextField(); p2b = new JButton("发送"); p2ta.setBounds(0, 0, 500, 300); p2tf.setBounds(0, 315, 400, 50); p2b.setBounds(420, 320, 60, 40); buttonLisenter(); this.add(p2ta); this.add(p2tf); this.add(p2b); this.setSize(500, 400); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public JTextArea getP2ta() { return p2ta; } //无参构造 public Person2() { super("小芳"); } //按钮监听器 private void buttonLisenter() { p2b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String str = p2tf.getText(); p2tf.setText(""); p2ta.append("我:"+"\r\n"+str + "\r\n"); try { Socket soc = new Socket(InetAddress.getLocalHost(),9999); OutputStream ots = soc.getOutputStream(); PrintStream ps = new PrintStream(ots); ps.print(str+"\r\n"); ps.close(); ots.close(); } catch (UnknownHostException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } }); } //测试类 public static void main(String[] args) { Person2 p2 = new Person2(); p2.init2(); Thread t2 = new Thread(new RunableP2(p2ta)); t2.start(); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import javax.swing.JTextArea; public class RunableP1 implements Runnable{ private JTextArea p1ta; public RunableP1(JTextArea p1ta) { this.p1ta = p1ta; } @Override public void run() { ServerSocket ss; try { ss = new ServerSocket(9999); while(true){ Socket sk = ss.accept(); InputStream is = sk.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); p1ta.append("小芳:\r\n"+br.readLine()+ "\r\n"); br.close(); } } catch (IOException e) { e.printStackTrace(); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import javax.swing.JTextArea; public class RunableP2 implements Runnable{ private JTextArea p2ta; public RunableP2(JTextArea p2ta) { this.p2ta = p2ta; } @Override public void run() { ServerSocket ss; try { ss = new ServerSocket(8888); while(true){ Socket sk = ss.accept(); InputStream is = sk.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); p2ta.append("小明:\r\n"+br.readLine()+ "\r\n"); br.close(); } } catch (IOException e) { e.printStackTrace(); } } }
标签:
原文地址:http://www.cnblogs.com/junzhao/p/4873335.html