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

📄 sendapplet.java

📁 java网络高级编程的配套源码,java网络高级编程为清华出版社出版.
💻 JAVA
字号:
/*程序清单5-19*/import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.io.*;import java.net.*;public class SendApplet extends JApplet{  boolean isStandalone = false;  //用于提醒用户输入  Label sendLabel = new Label();  XYLayout xYLayout1 = new XYLayout();  //用于返回用户输入  Label messageInfo = new Label();  //用于发送用户的输入信息  Button sendButton = new Button();  TextField sendText = new TextField();  TextField messageText = new TextField();  //用于与Servlet连接  URLConnection connect;  //用于保存用户的输入信息  String message;  URL chatURL;  /**Get a parameter value*/  public String getParameter(String key, String def)  {    return isStandalone ? System.getProperty(key, def) :      (getParameter(key) != null ? getParameter(key) : def);  }  /**Construct the applet*/  public SendApplet()  {  }  /**Initialize the applet*/  public void init()  {    try    {      jbInit();    }    catch(Exception e)    {      e.printStackTrace();    }  }  /**Component initialization*/  private void jbInit() throws Exception  {    sendLabel.setText("请输入信息:");    this.getContentPane().setLayout(xYLayout1);    messageInfo.setText("您输入的信息是:");    sendButton.setLabel("发送");    sendButton.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        sendButton_actionPerformed(e);      }    });    messageText.setEditable(false);    xYLayout1.setWidth(400);    xYLayout1.setHeight(129);    sendText.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        sendText_actionPerformed(e);      }    });    this.getContentPane().add(sendLabel, new XYConstraints(0, 0, 400, -1));    this.getContentPane().add(messageInfo, new XYConstraints(9, 58, 388, 19));    this.getContentPane().add(sendText, new XYConstraints(6, 24, 205, -1));    this.getContentPane().add(sendButton, new XYConstraints(210, 24, 187, 22));    this.getContentPane().add(messageText, new XYConstraints(5, 89, 400, -1));    chatURL = getCodeBase();  }  /**Get Applet information*/  public String getAppletInfo()  {    return "Applet Information";  }  /**Get parameter info*/  public String[][] getParameterInfo()  {    return null;  }  public synchronized void start()  {  }  public synchronized void stop()  {  }  public synchronized void destroy()  {  }  //方法Send把用户在Applet中输入的字符串送给Servlet  //然后将Servlet的处理结果显示  void Send()  {    message = sendText.getText();    //清除用户的输入信息    sendText.setText("");    showStatus("Message sent!");    //把输入的字符转化为x-www-form-urlencode格式    String queryString = "servlet/ReceiveServlet?message=" + URLEncoder.encode(message);    //建立与Servlet的连接,并取得Servlet的输出信息    try    {	connect = (new URL(chatURL,queryString)).openConnection();	showStatus("Open Connection!");	//下次连接不用Cache	connect.setDefaultUseCaches(false);	//这次连接不用Cache	connect.setUseCaches(false);	//打开的流用于读取数据	connect.setDoInput(true);	//不能用于些数据	connect.setDoOutput(false);	//服务器与客户的连接	connect.connect();	showStatus("打开流!");	DataInputStream in = new DataInputStream(connect.getInputStream());	showStatus("读数据!");	message = in.readLine();	//接收Servlet的输出信息	while (message!=null)	{  	  messageText.setText(message);	  message = in.readLine();	}    }    catch (MalformedURLException e2)    {      System.err.println("MalformedURLException!");      e2.printStackTrace(System.err);      showStatus("MalformedURLException!");    }    catch (IOException e1)    {      System.err.println("IOException!");      e1.printStackTrace(System.err);      showStatus("IOException!");    }   }  void sendButton_actionPerformed(ActionEvent e)  {    Send();  }  void sendText_actionPerformed(ActionEvent e)  {    Send();  }}

⌨️ 快捷键说明

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