appletcommunicate.java

来自「java servlet 示例共有17个示例程序」· Java 代码 · 共 33 行

JAVA
33
字号
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.URL;
/*
<applet code=AppletCommunicate width=250 height=200></applet>
*/
public class AppletCommunicate extends JApplet
{
	JTextArea msg = new JTextArea(200, 150);
	JButton send = new JButton("通讯");
		
	public void init() {
		Container cp = this.getContentPane();
		cp.add(BorderLayout.NORTH, 	new JLabel("点击按钮从Servlet获取数据"));
		cp.add(BorderLayout.CENTER, msg);
		cp.add(BorderLayout.SOUTH, send);
		
		send.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				try {
					DataInputStream in = new DataInputStream(
						new URL("http://localhost/servlet/b").openStream());
						
					String str = in.readLine();
					if(str != null)
						msg.append("From server:" +str +"\n");
				}catch(IOException e){
				}
			}});
	}
}

⌨️ 快捷键说明

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