⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frame1.java~23~

📁 JBuilder9 精髓 source code1.rar
💻 JAVA~23~
字号:
package tcpclient;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import com.borland.jbcl.layout.*;

/**
 * <p>Title: no</p>
 * <p>Description: This a Java builder program</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: home</p>
 * @author liujun
 * @version 1.0
 */

public class Frame1 extends JFrame implements Runnable{
  private JPanel contentPane;
  private XYLayout xYLayout1 = new XYLayout();
  private TextArea textArea1 = new TextArea();
  private Label label1 = new Label();
  private TextField textField1 = new TextField();
  private Button button1 = new Button();
  private Button button2 = new Button();
  private Button button3 = new Button();

  Socket socket = null;
  BufferedReader in = null;
  PrintWriter out = null;

  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    textArea1.setText(" ");
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(404, 314));
    this.setTitle("客户端");
    label1.setText("消息:");
    textField1.setText(" ");
    button1.setLabel("开始发送");
    button1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button1_actionPerformed(e);
      }
    });
    button2.setLabel("连接");
    button2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button2_actionPerformed(e);
      }
    });
    button3.setLabel("退出");
    button3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button3_actionPerformed(e);
      }
    });
    contentPane.add(textArea1,   new XYConstraints(11, 7, 383, 199));
    contentPane.add(label1,    new XYConstraints(29, 213, 58, 27));
    contentPane.add(textField1,  new XYConstraints(99, 215, 288, 24));
    contentPane.add(button1,  new XYConstraints(19, 251, 92, 26));
    contentPane.add(button2,  new XYConstraints(225, 251, 94, 24));
    contentPane.add(button3,  new XYConstraints(333, 251, 62, 22));
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      //添加代码
     try{
     out.println("client exit!");
     out.flush();
     }catch(Exception ex){}
     finally{
       System.exit(0);
     }
    }
  }

  void button1_actionPerformed(ActionEvent e) {
    out.println(textField1.getText());
    out.flush();
    textArea1.append("client information:"+textField1.getText()+"\n");
    textField1.setText("");
  }

  void button3_actionPerformed(ActionEvent e) {
    try{
    out.println("clientexit!");
    out.flush();
    }catch(Exception e2){}
    finally{
      System.exit(0);
    }
  }

  void button2_actionPerformed(ActionEvent e) {
    Thread thread = new Thread(this);
    thread.start();
  }
  public void run() {
        try {
          //在5438端口上打开到Home的连接,说明它其实就是你的本地计算机名
            //如果你的计算机的名称不是“Home”,请修改为你的计算机名称
            socket = new Socket(InetAddress.getLocalHost(),5438);
            in = new BufferedReader(new  InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream());
            button1.setEnabled(true);
            receiver r = new receiver();
            Thread t = new Thread(r);
            t.start();
            textArea1.append("system information:have joined to server!\n");
            button2.setEnabled(false);
        }
        catch (IOException ex) {
  //           textArea1.append(ex.toString()+"\n");
      }
    }

    private class receiver implements Runnable{
        public void run(){
          String s1 = null;
          try{
            s1 = in.readLine();
            while(s1!= "client exit!"){
              textArea1.append("client information:  "+s1+"\n");
              s1 = in.readLine();
            }
            in.close();
            out.close();
            socket.close();
          }catch(Exception e){}
           button1.setEnabled(false);
        }
      }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -