- import java.io.*;
- import java.net.*;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- class clientThread extends EdClient implements Runnable
- {
- public void run() {
- // TODO Auto-generated method stub
- try {
- Thread.sleep(20);
- receiveString = bufferReader.readLine();
- System.out.println("receive:"+receiveString);
- receiveText.setText(receiveString);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public class EdClient {
- /**
- * this is client
- */
- JFrame frame;
- JPanel panel;
- static JTextField ipText;
- static JTextField portText;
- static String ip = "127.0.0.1"; //ip address
- static int port=5000; //port
- static JButton sentConfirm;
- static Socket sock;
- static BufferedReader bufferReader;
- static String receiveString,transferString;
- static JTextField transferText,receiveText;
- static boolean flag = false;
- public void ui()
- {
- frame = new JFrame("聊天客户端_徐方鑫");
- panel = new JPanel();
- frame.add(panel);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Box panelBox = Box.createVerticalBox();
- panel.add(panelBox);
- Box ipmain = Box.createHorizontalBox();
- panelBox.add(new JLabel(""));
- panelBox.add(ipmain);
- {
- JLabel ipLabel = new JLabel("ip:");
- ipText = new JTextField("127.0.0.1",15);
- ipText.setHorizontalAlignment(JTextField.CENTER); //align center
- ipmain.add(ipLabel);
- ipmain.add(ipText);
- JLabel portLabel = new JLabel("port:");
- portText = new JTextField("5000",8);
- portText.setHorizontalAlignment(JTextField.CENTER);
- ipmain.add(portLabel);
- ipmain.add(portText);
- }
- Box ipconfirm = Box.createHorizontalBox();
- panelBox.add(ipconfirm);
- /*
- JButton ipButton1 = new JButton("连接");
- {
- ipButton1.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- ip = ipText.getText();
- port=Integer.valueOf(portText.getText()).intValue();
- System.out.println(ip);
- System.out.println(port);
- Runnable threadServer = new clientThread();
- final Thread mythread = new Thread(threadServer);
- netConnect();
- mythread.start();
- }
- });
- }
- */
- JButton ipButton2 = new JButton("重置");
- {
- ipButton2.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- ipText.setText("127.0.0.1");
- portText.setText("5000");
- try {
- sock.close();
- //transferText.setText(null);
- //receiveText.setText(null);
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- });
- }
- //ipconfirm.add(ipButton1);
- //ipconfirm.add(ipButton2);
- Box receiveMain = Box.createHorizontalBox();
- panelBox.add(receiveMain);
- {
- JLabel receive = new JLabel("接收:");
- receiveMain.add(receive);
- receiveText = new JTextField(20);
- receiveMain.add(receiveText);
- receiveText.enable(false);
- }
- Box transferMain = Box.createHorizontalBox();
- panelBox.add(transferMain);
- {
- JLabel transfer = new JLabel("发送:");
- transferMain.add(transfer);
- transferText = new JTextField(20);
- transferMain.add(transferText);
- }
- Box talkConfirm = Box.createHorizontalBox();
- panelBox.add(talkConfirm);
- {
- sentConfirm = new JButton("发送");
- talkConfirm.add(sentConfirm);
- talkConfirm.add(ipButton2);
- }
- frame.setSize(400,150);
- frame.setVisible(true);
- }
- public static void netConnect()
- {
- try{
- sock = new Socket(ip,port);
- InputStreamReader reader = new InputStreamReader(sock.getInputStream());
- bufferReader = new BufferedReader(reader);
- }catch(IOException ex)
- {
- ex.printStackTrace();
- JOptionPane.showMessageDialog(null, "连接失败");
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- EdClient ed = new EdClient();
- ed.ui();
- sentConfirm.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- try {
- ip = ipText.getText();
- port=Integer.valueOf(portText.getText()).intValue();
- System.out.println(ip);
- System.out.println(port);
- Runnable threadServer = new clientThread();
- final Thread mythread = new Thread(threadServer);
- netConnect();
- mythread.start();
- transferString = transferText.getText();
- System.out.println("sent:"+transferString);
- PrintWriter writer = new PrintWriter(sock.getOutputStream());
- writer.println(transferString);
- writer.flush();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- });
- }
- }