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

📄 myclient.java

📁 用java写的用于C/S服务的网络应用程序
💻 JAVA
字号:
package myclient;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class MyClient
    extends JFrame {
  Socket ClientSocket;
  PrintStream os;
  DataInputStream is;
  String s;

  JPanel contentPane;
  BorderLayout borderLayout1 = new BorderLayout();
  JLabel Mylabel = new JLabel();
  JScrollPane jScrollPane1 = new JScrollPane();
  JButton MyButton = new JButton();
  JTextArea textArea = new JTextArea();

  //Construct the frame
  public MyClient() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
      connect();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception {
    contentPane = (JPanel)this.getContentPane();
    Mylabel.setEnabled(true);
    Mylabel.setPreferredSize(new Dimension(132, 16));
    Mylabel.setText("欢迎使用本机器提供的信息");
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("Client Window");
    MyButton.setText("发送");
    MyButton.addActionListener(new MyClient_MyButton_actionAdapter(this));
    contentPane.add(Mylabel, BorderLayout.NORTH);
    contentPane.add(jScrollPane1, BorderLayout.CENTER);
    jScrollPane1.getViewport().add(textArea, null);
    contentPane.add(MyButton, BorderLayout.SOUTH);
  }

  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      try {
        os.println("Bye");
        os.flush();
        is.close();
        os.close();
        ClientSocket.close();
        //((Frame)(e.getSource())).dispose();
        dispose();
        System.exit(0);
      }
      catch (Exception ex) {}
    }

  }

  public void connect() {
    try {
      ClientSocket = new Socket("127.0.0.1", 8000);
      os = new PrintStream(new BufferedOutputStream
                           (ClientSocket.getOutputStream()));
      is = new DataInputStream(new BufferedInputStream
                               (ClientSocket.getInputStream()));
      s = is.readLine();
      textArea.append(s + "\n");
    }
    catch (Exception e) {}
  }

  void MyButton_actionPerformed(ActionEvent e) {

    if (e.getSource() == MyButton) {
      try {
        os.print(textArea.getText());
        os.flush();
      }
      catch (Exception ex) {}

    }

  }

}

class MyClient_MyButton_actionAdapter
    implements java.awt.event.ActionListener {
  MyClient adaptee;

  MyClient_MyButton_actionAdapter(MyClient adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.MyButton_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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