[编辑]
跟随安德鲁汤普森的建议并写了一个剧本。但是,我仍然无法使应用程序工作。它确实看起来像参数是错误的。
<![CDATA[
var attributes =
{
code: 'site.SiteSelector',
archive: '../src/site/site.jar',
width: 280,
height: 280
};
var parameters = {title0: 'Java Home Page', location0: 'http://www.oracle.com/technetwork/java/',
title1: 'Deitel', location1: 'http://www.deitel.com/', title2: 'JGuru', location2: 'http://www.guru.com/'
, title3: 'JavaWorld', location3: 'http://www.javaworld.com/'};
deployJava.runApplet(attributes, parameters, '1.7');
]]>
[/编辑]
我正在尝试从编写的html5中获取<param>
的值。问题是每次我都会收到null作为getParameter()
的返回值。而且,我不知道我是否正确地写了对象。该程序使用<applet>
标记。
package site;
import java.applet.AppletContext;
import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class SiteSelector extends JApplet {
private HashMap<String, URL> sites;
private ArrayList<String> siteNames;
private JList<String> siteChooser;
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void init() {
sites = new HashMap<String, URL>();
siteNames = new ArrayList<String>();
getSitesFromHTMLParameters();
add(new JLabel("Choose a site to browse"), BorderLayout.NORTH);
siteChooser = new JList(siteNames.toArray());
siteChooser.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
URL newDocument = sites.get(siteChooser.getSelectedValue());
AppletContext browser = getAppletContext();
browser.showDocument(newDocument);
}
});
add(new JScrollPane(siteChooser), BorderLayout.CENTER);
}
private void getSitesFromHTMLParameters() {
String title = null;
URL location = null;
int count = 0;
title = getParameter("title" + count);
while (title != null) {
try {
location = new URL(getParameter("location" + count));
sites.put(title, location);
siteNames.add(title);
} catch (MalformedURLException urlException) {
JOptionPane.showMessageDialog(SiteSelector.this, String.format(
"Error: %s.Message: %s\n", urlException.getClass()
.getName(), urlException.getMessage()),
"Error", JOptionPane.ERROR_MESSAGE);
}
++count;
title = getParameter("title" + count);
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Site Selector</title>
</head>
<body>
<object id="siteselectorapplet-object"
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="256"
height="256"
codebase="http://java.sun.com/update/1.6.0/jinstall-6u30-windows-i586.cab#version=1,6,0,0">
<param name="archive" value="../src/site/site.jar">
<param name="code" value="site.SiteSelector">
<param name="title0" value="Java Home Page">
<param name="location0"
value="http://www.oracle.com/technetwork/java/">
<param name="title1" value="Deitel">
<param name="location1" value="http://www.deitel.com/">
<param name="title2" value="JGuru">
<param name="location2" value="http://www.guru.com/">
<param name="title3" value="JavaWorld">
<param name="location3" value="http://www.javaworld.com/">
</object>
</body>