代码如下: /** @version 1.11 2001-06-27 @author Cay Horstmann */import java.net.*; import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*;/** This applet displays weather data from a NOAA server. The data is mostly in text format, so it can be displayed in a text area. */ public class WeatherApplet extends JApplet { public void init() { Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); // Set up the lists of choices for states and reports JPanel comboPanel = new JPanel(); state = makeCombo(states, comboPanel); report = makeCombo(reports, comboPanel); contentPane.add(comboPanel, BorderLayout.NORTH); // Add the text area weather = new JTextArea(20, 80); weather.setFont(new Font("Courier", Font.PLAIN, 12)); // Add the report button contentPane.add(new JScrollPane(weather), BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); JButton reportButton = new JButton("Get report"); reportButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { weather.setText(""); new Thread() { public void run() { getWeather(getItem(state, states), getItem(report, reports)); } }.start(); } }); buttonPanel.add(reportButton); contentPane.add(buttonPanel, BorderLayout.SOUTH); } /** Makes a combo box. @param items the array of strings whose first column contains the combo box display entries @param parent the parent to add the combo box to */ public JComboBox makeCombo(String[][] items, Container parent) { JComboBox combo = new JComboBox(); for (int i = 0; i < items.length; i++) combo.addItem(items[i][0]); parent.add(combo); return combo; }
/** Gets the query string for a combo box display entry. @param box the combo box @param items the array of strings whose first column contains the combo box display entries and whose second column contains the corresponding query strings @return the query string */ public String getItem(JComboBox box, String[][] items) { return items[box.getSelectedIndex()][1]; } /** Puts together the URL query and fills the text area with the data. @param state the state part of the query @param report the report part of the query */ public void getWeather(String state, String report) { String r = new String(); try { String queryBase = getParameter("queryBase"); String query = queryBase + state + "/" + report + ".html"; URL url = new URL(query); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String line; while ((line = in.readLine()) != null) weather.append(removeTags(line) + "\n"); } catch(IOException e) { showStatus("Error " + e); } } /** Removes HTML tags from a string. @param s a string @return s with <...> tags removed. */ public static String removeTags(String s) { while (true) { int lb = s.indexOf('<'); if (lb < 0) return s; int rb = s.indexOf('>', lb); if (rb < 0) return s; s = s.substring(0, lb) + " " + s.substring(rb + 1); } } private JTextArea weather; private JComboBox state; private JComboBox report; private String[][] states = { { "Alabama", "al" }, { "Alaska", "ak" }, { "Arizona", "az" }, { "Arkansas", "ar" }, { "California", "ca" }, { "Colorado", "co" }, { "Connecticut", "ct" }, { "Delaware", "de" }, { "Florida", "fl" }, { "Georgia", "ga" }, { "Hawaii", "hi" }, { "Idaho", "id" }, { "Illinois", "il" }, { "Indiana", "in" }, { "Iowa", "ia" }, { "Kansas", "ks" }, { "Kentucky", "ky" }, { "Lousisiana", "la" }, { "Maine", "me" }, { "Maryland", "md" }, { "Massachusetts", "ma" }, { "Michigan", "mi" }, { "Minnesota", "mn" }, { "Mississippi", "ms" }, { "Missouri", "mo" }, { "Montana", "mt" }, { "Nebraska", "ne" }, { "Nevada", "nv" }, { "New Hampshire", "nh" }, { "New Jersey", "nj" }, { "New Mexico", "nm" }, { "New York", "ny" }, { "North Carolina", "nc" }, { "North Dakota", "nd" }, { "Ohio", "oh" }, { "Oklahoma", "ok" }, { "Oregon", "or" }, { "Pennsylvania", "pa" }, { "Rhode Island", "ri" }, { "South Carolina", "sc" }, { "South Dakota", "sd" }, { "Tennessee", "tn" }, { "Texas", "tx" }, { "Utah", "ut" }, { "Vermont", "vt" }, { "Virginia", "va" }, { "Washington", "wa" }, { "West Virginia", "wv" }, { "Wisconsin", "wi" }, { "Wyoming", "wy" } }; private String[][] reports = { { "Hourly (State Weather Roundup)", "hourly" }, { "State Forecast", "state" }, { "Zone Forecast", "zone" }, { "Short Term (NOWCASTS)", "shortterm" }, { "Forecast Discussion", "discussion" }, { "Weather Summary", "summary" }, { "Public Information", "public" }, { "Climate Data", "climate" }, { "Hydrological Data", "hydro" }, { "Watches", "watches" }, { "Special Weather Statements", "special" }, { "Warnings and Advisories", "allwarnings" } }; } 网页如下:<HTML> <HEAD> <TITLE>Cay Horstmann's Weather Report Applet</TITLE> </HEAD> <BODY> <H1>A Weather Report Applet </H1> <P>Select a state and report type, and click on the "Get report" button. You will get an up-to-date weather report, courtesy of the National Oceanic and Atmospheric Administration. </P> <P>The point of this applet is not to get a beautiful report--there are many web sites that do that--but to show how an applet can retrieve information from another web server. </P> <HR> <APPLET CODE="WeatherApplet.class" WIDTH="600" HEIGHT="400"> <PARAM NAME="queryBase" VALUE="http://iwin.nws.noaa.gov/iwin/"> </APPLET> <HR> <UL> <LI><A HREF="WeatherApplet.java">The source.</A> </LI> <LI><A HREF="ProxySrv.java">The source for the proxy server.</A> </LI> </UL> </BODY> </HTML>
/**
@version 1.11 2001-06-27
@author Cay Horstmann
*/import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;/**
This applet displays weather data from a NOAA server.
The data is mostly in text format, so it can be displayed
in a text area.
*/
public class WeatherApplet extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout()); // Set up the lists of choices for states and reports
JPanel comboPanel = new JPanel();
state = makeCombo(states, comboPanel);
report = makeCombo(reports, comboPanel);
contentPane.add(comboPanel, BorderLayout.NORTH); // Add the text area
weather = new JTextArea(20, 80);
weather.setFont(new Font("Courier", Font.PLAIN, 12)); // Add the report button
contentPane.add(new JScrollPane(weather),
BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton reportButton = new JButton("Get report");
reportButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
weather.setText("");
new
Thread()
{
public void run()
{
getWeather(getItem(state, states),
getItem(report, reports));
}
}.start();
}
}); buttonPanel.add(reportButton);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
} /**
Makes a combo box.
@param items the array of strings whose first column
contains the combo box display entries
@param parent the parent to add the combo box to
*/
public JComboBox makeCombo(String[][] items, Container parent)
{
JComboBox combo = new JComboBox();
for (int i = 0; i < items.length; i++)
combo.addItem(items[i][0]);
parent.add(combo);
return combo;
}
/**
Gets the query string for a combo box display entry.
@param box the combo box
@param items the array of strings whose first column
contains the combo box display entries and whose second
column contains the corresponding query strings
@return the query string
*/
public String getItem(JComboBox box, String[][] items)
{
return items[box.getSelectedIndex()][1];
} /**
Puts together the URL query and fills the text area with
the data.
@param state the state part of the query
@param report the report part of the query
*/
public void getWeather(String state, String report)
{
String r = new String();
try
{
String queryBase = getParameter("queryBase");
String query
= queryBase + state + "/" + report + ".html";
URL url = new URL(query);
BufferedReader in = new BufferedReader(new
InputStreamReader(url.openStream())); String line;
while ((line = in.readLine()) != null)
weather.append(removeTags(line) + "\n");
}
catch(IOException e)
{
showStatus("Error " + e);
}
} /**
Removes HTML tags from a string.
@param s a string
@return s with <...> tags removed.
*/
public static String removeTags(String s)
{
while (true)
{
int lb = s.indexOf('<');
if (lb < 0) return s;
int rb = s.indexOf('>', lb);
if (rb < 0) return s;
s = s.substring(0, lb) + " " + s.substring(rb + 1);
}
} private JTextArea weather;
private JComboBox state;
private JComboBox report; private String[][] states =
{
{ "Alabama", "al" },
{ "Alaska", "ak" },
{ "Arizona", "az" },
{ "Arkansas", "ar" },
{ "California", "ca" },
{ "Colorado", "co" },
{ "Connecticut", "ct" },
{ "Delaware", "de" },
{ "Florida", "fl" },
{ "Georgia", "ga" },
{ "Hawaii", "hi" },
{ "Idaho", "id" },
{ "Illinois", "il" },
{ "Indiana", "in" },
{ "Iowa", "ia" },
{ "Kansas", "ks" },
{ "Kentucky", "ky" },
{ "Lousisiana", "la" },
{ "Maine", "me" },
{ "Maryland", "md" },
{ "Massachusetts", "ma" },
{ "Michigan", "mi" },
{ "Minnesota", "mn" },
{ "Mississippi", "ms" },
{ "Missouri", "mo" },
{ "Montana", "mt" },
{ "Nebraska", "ne" },
{ "Nevada", "nv" },
{ "New Hampshire", "nh" },
{ "New Jersey", "nj" },
{ "New Mexico", "nm" },
{ "New York", "ny" },
{ "North Carolina", "nc" },
{ "North Dakota", "nd" },
{ "Ohio", "oh" },
{ "Oklahoma", "ok" },
{ "Oregon", "or" },
{ "Pennsylvania", "pa" },
{ "Rhode Island", "ri" },
{ "South Carolina", "sc" },
{ "South Dakota", "sd" },
{ "Tennessee", "tn" },
{ "Texas", "tx" },
{ "Utah", "ut" },
{ "Vermont", "vt" },
{ "Virginia", "va" },
{ "Washington", "wa" },
{ "West Virginia", "wv" },
{ "Wisconsin", "wi" },
{ "Wyoming", "wy" }
}; private String[][] reports =
{
{ "Hourly (State Weather Roundup)", "hourly" },
{ "State Forecast", "state" },
{ "Zone Forecast", "zone" },
{ "Short Term (NOWCASTS)", "shortterm" },
{ "Forecast Discussion", "discussion" },
{ "Weather Summary", "summary" },
{ "Public Information", "public" },
{ "Climate Data", "climate" },
{ "Hydrological Data", "hydro" },
{ "Watches", "watches" },
{ "Special Weather Statements", "special" },
{ "Warnings and Advisories", "allwarnings" }
};
}
网页如下:<HTML> <HEAD>
<TITLE>Cay Horstmann's Weather Report Applet</TITLE>
</HEAD> <BODY> <H1>A Weather Report Applet </H1> <P>Select a state and report type, and click on the "Get report"
button. You will get an up-to-date weather report, courtesy of the
National Oceanic and Atmospheric Administration. </P> <P>The point of this applet is not to get a beautiful report--there are
many web sites that do that--but to show how an applet can retrieve
information from another web server. </P>
<HR> <APPLET
CODE="WeatherApplet.class" WIDTH="600" HEIGHT="400">
<PARAM NAME="queryBase" VALUE="http://iwin.nws.noaa.gov/iwin/">
</APPLET>
<HR>
<UL>
<LI><A HREF="WeatherApplet.java">The source.</A> </LI>
<LI><A HREF="ProxySrv.java">The source for the proxy server.</A> </LI>
</UL>
</BODY>
</HTML>
注意你的屏幕右下角的系统托盘,如果这个Applet能在IE中运行,那么
系统托盘中必然会启动一个JAVA的“图标”!
不知道你的有没有,如果没有,那么这个Applet就没有正确运行,
即便是IE打开了,Applet也没有被IE打开。先开始,我的Applet被IE打开时也没有启动那个“图标”,所以什么都不显示!后来,我到Java的网站上去找了个插件
因为我在一些文章上看到,现在的是所谓的Java2,其Applet要在IE中显示,
必须有插件因为一些applet的内容涉及到了Swing。我看到的是说要下一个叫Java Plug-in的插件!
我就到JAVA网站上找,结果我看到,好像说Java Plug-in本身就包含在jre中,
而jre在我安装j2sdk1.4.1的时候就应该一并安装了,难道Java Plug-in
没有安装!不管那么多,找到Java Plug-in,在页面:
http://java.sun.com/products/archive/index.html中
可以看到最新的是Java TM Plug-In 1.1.3_007,
我就将其下载下来,一共5M多,然后单独安装了一边,然后重新编辑了个新的Applet文件,运行,就可以在IE中显示了,
每次用IE打开Applet的时候, 右下角的系统托盘中都会启动那个
JAVA的“图标”!(这也就是我上面形容的Apllet正确运行的效果)不过也请大家注意,我是“重新编辑了个新的Applet文件”,
因为我发现,我原来那个Applet还是不能正常显示!
虽然前后两个Applet的内容相同,只是类名和文件名不同罢了。
这一点我暂时还不能解释原因,谁知道,还麻烦告诉我。
新写的都可以了,哪怕是代码一样,也要重新写过,这也是我还比较奇怪的地方!再补充一下,注意那个“applet正确运行时的状态”
我是说系统托盘出现java图标,不出现当然不行。还有在“控制面板”里检查一下plug的设置!
反正最重要的是 程序要重新写,重新编译,特别是那个html更要重新写,
千万别拷贝!
更不必说有托盘了。
打开他,里面的设置看看!奇怪!你还安装了其他什么?比如JBuilder?有吗?总之检查一下你的机子上有几个JRE!
如何改为Java 的VM???????
Microsoft VM !可能是。那个Plug in Control Panel打开以后好想有设置吧。
关键还是看IE高级设置的选项中,是将什么jdk的那个版本用于applet!有时候是因为你安装了多个jdk,而造成的冲突!