client2.java

来自「该原代码为<Java编程基础、应用与实例>的附盘代码」· Java 代码 · 共 39 行

JAVA
39
字号
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
public class Client2 extends Frame{
  private TextArea msgView=new TextArea();
  private TextField sendBox=new TextField();
  private PrintWriter writer;
  Socket socket;
  public Client2(String title){
    super(title);
    msgView.setEditable(false);
    add(msgView,"North");
    add(sendBox,"South");
    sendBox.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae){
        writer.println(sendBox.getText());
        msgView.append(sendBox.getText()+"\n");
        sendBox.setText("");
      }  
    });
    pack();
  }
  private void connect(){
    try{
      msgView.append("辑滚客狼 楷搬阑 矫档钦聪促.\n");
      socket=new Socket("127.0.0.1", 7777);
      msgView.append("楷搬登菌嚼聪促. 傈价且 巩磊凯阑 涝仿窍技夸.\n");
      writer=new PrintWriter(socket.getOutputStream(), true);
    }catch(Exception e){
      msgView.append("楷搬 角菩..");  
    }
  }
  public static void main(String[] args){
    Client2 client=new Client2("辑滚肺 单捞磐 焊郴扁");
    client.setVisible(true);
    client.connect();
  }
}

⌨️ 快捷键说明

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