appletclient.java

来自「java初学例子」· Java 代码 · 共 68 行

JAVA
68
字号
import java.io.*;import java.net.*;import java.awt.BorderLayout;import javax.swing.*;public class AppletClient extends JApplet {  // Label for displaying the visit count  private JLabel jlblCount = new JLabel();  // Indicate if it runs as application  private boolean isStandAlone = false;  // Host name or ip  private String host = "localhost";  /** Initialize the applet */  public void init() {    getContentPane().add(jlblCount);    try {      // Create a socket to connect to the server      Socket socket;      if (isStandAlone)        socket = new Socket(host, 8000);      else        socket = new Socket(getCodeBase().getHost(), 8000);      // Create an input stream to receive data from the server      DataInputStream inputFromServer =        new DataInputStream(socket.getInputStream());      // Receive the count from the server and display it on label      int count = inputFromServer.readInt();      jlblCount.setText("You are visitor number " + count);      // Close the stream      inputFromServer.close();    }    catch (IOException ex) {      ex.printStackTrace();    }  }  /** Run the applet as an application */  public static void main(String[] args) {    // Create a frame    JFrame frame = new JFrame("Applet Client");    // Create an instance of the applet    AppletClient applet = new AppletClient();    applet.isStandAlone = true;    // Get host    if (args.length == 1) applet.host = args[0];    // Add the applet instance to the frame    frame.getContentPane().add(applet, BorderLayout.CENTER);    // Invoke init() and start()    applet.init();    applet.start();    // Display the frame    frame.pack();    frame.setVisible(true);  }}

⌨️ 快捷键说明

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