JAVA聊天器_客户端

Code:
  1. import java.io.*;  
  2. import java.net.*;   
  3. import javax.swing.*;  
  4.   
  5. import java.awt.*;  
  6. import java.awt.event.*;  
  7.   
  8. class clientThread extends EdClient implements Runnable  
  9. {  
  10.     public void run() {  
  11.         // TODO Auto-generated method stub  
  12.         try {  
  13.             Thread.sleep(20);  
  14.             receiveString = bufferReader.readLine();  
  15.             System.out.println("receive:"+receiveString);  
  16.             receiveText.setText(receiveString);  
  17.         } catch (IOException e) {  
  18.             // TODO Auto-generated catch block  
  19.             e.printStackTrace();  
  20.         } catch (InterruptedException e) {  
  21.             // TODO Auto-generated catch block  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25. }  
  26.   
  27. public class EdClient {  
  28.     /** 
  29.      * this is client 
  30.      */  
  31.     JFrame frame;  
  32.     JPanel panel;  
  33.     static JTextField ipText;  
  34.     static JTextField portText;  
  35.     static String ip = "127.0.0.1";  //ip address  
  36.     static int port=5000;  //port  
  37.     static JButton sentConfirm;  
  38.     static Socket sock;  
  39.     static BufferedReader bufferReader;  
  40.     static String receiveString,transferString;  
  41.     static JTextField transferText,receiveText;  
  42.     static boolean flag = false;  
  43.       
  44.     public void ui()  
  45.     {  
  46.         frame = new JFrame("聊天客户端_徐方鑫");  
  47.         panel = new JPanel();  
  48.         frame.add(panel);  
  49.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  50.         Box panelBox = Box.createVerticalBox();  
  51.         panel.add(panelBox);  
  52.   
  53.         Box ipmain = Box.createHorizontalBox();  
  54.         panelBox.add(new JLabel(""));  
  55.         panelBox.add(ipmain);  
  56.         {  
  57.             JLabel ipLabel = new JLabel("ip:");  
  58.             ipText = new JTextField("127.0.0.1",15);  
  59.             ipText.setHorizontalAlignment(JTextField.CENTER);  //align center  
  60.             ipmain.add(ipLabel);  
  61.             ipmain.add(ipText);  
  62.             JLabel portLabel = new JLabel("port:");  
  63.             portText = new JTextField("5000",8);  
  64.             portText.setHorizontalAlignment(JTextField.CENTER);  
  65.             ipmain.add(portLabel);  
  66.             ipmain.add(portText);  
  67.         }  
  68.           
  69.         Box ipconfirm = Box.createHorizontalBox();  
  70.         panelBox.add(ipconfirm);  
  71.         /* 
  72.         JButton ipButton1 = new JButton("连接"); 
  73.         { 
  74.             ipButton1.addActionListener(new ActionListener() 
  75.             { 
  76.                 public void actionPerformed(ActionEvent e) 
  77.                 { 
  78.                     ip = ipText.getText(); 
  79.                     port=Integer.valueOf(portText.getText()).intValue(); 
  80.                     System.out.println(ip); 
  81.                     System.out.println(port); 
  82.                     Runnable threadServer = new clientThread(); 
  83.                     final Thread mythread = new Thread(threadServer); 
  84.                     netConnect(); 
  85.                     mythread.start(); 
  86.                 } 
  87.             }); 
  88.         } 
  89.         */  
  90.         JButton ipButton2 = new JButton("重置");  
  91.         {  
  92.             ipButton2.addActionListener(new ActionListener()  
  93.             {  
  94.                 public void actionPerformed(ActionEvent e)  
  95.                 {  
  96.                     ipText.setText("127.0.0.1");  
  97.                     portText.setText("5000");  
  98.                     try {  
  99.                         sock.close();  
  100.                         //transferText.setText(null);  
  101.                         //receiveText.setText(null);  
  102.                     } catch (IOException e1) {  
  103.                         // TODO Auto-generated catch block  
  104.                         e1.printStackTrace();  
  105.                     }  
  106.                 }  
  107.             });  
  108.         }  
  109.         //ipconfirm.add(ipButton1);  
  110.         //ipconfirm.add(ipButton2);  
  111.           
  112.         Box receiveMain = Box.createHorizontalBox();  
  113.         panelBox.add(receiveMain);  
  114.         {  
  115.             JLabel receive = new JLabel("接收:");  
  116.             receiveMain.add(receive);  
  117.             receiveText = new JTextField(20);  
  118.             receiveMain.add(receiveText);  
  119.             receiveText.enable(false);  
  120.         }  
  121.         Box transferMain = Box.createHorizontalBox();  
  122.         panelBox.add(transferMain);  
  123.         {  
  124.             JLabel transfer = new JLabel("发送:");  
  125.             transferMain.add(transfer);  
  126.             transferText = new JTextField(20);  
  127.             transferMain.add(transferText);  
  128.         }  
  129.           
  130.         Box talkConfirm = Box.createHorizontalBox();  
  131.         panelBox.add(talkConfirm);  
  132.         {  
  133.             sentConfirm = new JButton("发送");  
  134.             talkConfirm.add(sentConfirm);  
  135.             talkConfirm.add(ipButton2);  
  136.         }  
  137.           
  138.         frame.setSize(400,150);  
  139.         frame.setVisible(true);  
  140.           
  141.     }  
  142.       
  143.     public static void netConnect()  
  144.     {  
  145.         try{      
  146.         sock = new Socket(ip,port);  
  147.         InputStreamReader reader = new InputStreamReader(sock.getInputStream());  
  148.         bufferReader = new BufferedReader(reader);  
  149.         }catch(IOException ex)  
  150.         {  
  151.             ex.printStackTrace();  
  152.             JOptionPane.showMessageDialog(null"连接失败");  
  153.         }  
  154.     }  
  155.       
  156.     public static void main(String[] args) {  
  157.         // TODO Auto-generated method stub  
  158.         EdClient ed = new EdClient();  
  159.         ed.ui();  
  160.         sentConfirm.addActionListener(new ActionListener()  
  161.         {  
  162.             public void actionPerformed(ActionEvent e)  
  163.             {  
  164.                 try {  
  165.                     ip = ipText.getText();  
  166.                     port=Integer.valueOf(portText.getText()).intValue();  
  167.                     System.out.println(ip);  
  168.                     System.out.println(port);  
  169.                     Runnable threadServer = new clientThread();  
  170.                     final Thread mythread = new Thread(threadServer);  
  171.                     netConnect();  
  172.                     mythread.start();  
  173.                     transferString = transferText.getText();  
  174.                     System.out.println("sent:"+transferString);  
  175.                     PrintWriter writer = new PrintWriter(sock.getOutputStream());  
  176.                     writer.println(transferString);  
  177.                     writer.flush();  
  178.                 } catch (IOException e1) {  
  179.                     // TODO Auto-generated catch block  
  180.                     e1.printStackTrace();  
  181.                 }  
  182.             }  
  183.         });  
  184.           
  185.   
  186.     }  
  187.   
  188. }  

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wi-Fi研习者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值