文档

Java™教程
隐藏目录
定义和使用Applet参数
路径: 部署
课程: Java Applets
章节: 更多 Applet 操作

定义和使用 Applet 参数

参数对于Java小程序来说,就像命令行参数对于应用程序一样。它们使用户能够自定义小程序的操作。通过定义参数,您可以增加小程序的灵活性,使小程序能在多种情况下工作,而无需重新编码和重新编译。

指定小程序的输入参数

您可以在小程序的Java网络启动协议(JNLP)文件或<applet>标签的<parameter>元素中指定小程序的输入参数。通常最好在小程序的JNLP文件中指定参数,这样即使小程序部署在多个网页上,参数也可以保持一致。如果小程序的参数会因网页而异,则应在<applet>标签的<parameter>元素中指定参数。

如果您对JNLP不熟悉,请参阅Java网络启动协议主题获取更多信息。

考虑一个接受三个参数的小程序。在小程序的JNLP文件applettakesparams.jnlp中指定了paramStrparamInt参数。

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
    <!-- ... -->
    <applet-desc
         name="Applet Takes Params"
         main-class="AppletTakesParams"
         width="800"
         height="50">
             <param name="paramStr"
                 value="someString"/>
             <param name="paramInt" value="22"/>
     </applet-desc>
     <!-- ... -->
</jnlp>

AppletPage.html中的部署工具脚本的runApplet函数中的parameters变量中指定了paramOutsideJNLPFile参数。

<html>
  <head>
    <title>Applet Takes Params</title>
    <meta http-equiv="Content-Type" content="text/html;
        charset=windows-1252">
  </head>
  <body>
    <h1>Applet Takes Params</h1>

    <script
      src="https://www.java.com/js/deployJava.js"></script>
    <script>
        var attributes = { code:'AppletTakesParams.class',
            archive:'applet_AppletWithParameters.jar',
            width:800, height:50 };
        var parameters = {jnlp_href: 'applettakesparams.jnlp',
            paramOutsideJNLPFile: 'fooOutsideJNLP' };
        deployJava.runApplet(attributes, parameters, '1.7');
    </script>

  </body>
</html>

请查看部署Applet了解有关runApplet函数的更多信息。

检索Applet的输入参数

您可以使用Applet类的getParameter方法来检索Applet的输入参数。 AppletTakesParams.java Applet检索并显示其所有输入参数(paramStrparamIntparamOutsideJNLPFile)。

import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;

public class AppletTakesParams extends JApplet {
    public void init() {
        final String  inputStr = getParameter("paramStr");        
        final int inputInt = Integer.parseInt(getParameter("paramInt"));
        final String inputOutsideJNLPFile = getParameter("paramOutsideJNLPFile");

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI(inputStr, inputInt, inputOutsideJNLPFile);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI未能成功完成");
        }
    }
    private void createGUI(String inputStr, int inputInt, String inputOutsideJNLPFile) {
        String text = "Applet的参数是 - inputStr:" + inputStr +
                ",inputInt:" + inputInt +
                ",paramOutsideJNLPFile:" + inputOutsideJNLPFile;
        JLabel lbl = new JLabel(text);
        add(lbl);
    }
}

接下来显示AppletTakesParams Applet。


注意:  如果您看不到Applet运行,请安装至少Java SE Development Kit(JDK)6 update 10版本。

注意: 如果您没有看到示例运行,您可能需要在浏览器中启用JavaScript解释器,以便Deployment Toolkit脚本能够正常工作。

下载带参数的Applet示例的源代码以进一步实验。


上一页:查找和加载数据文件
下一页:显示简短状态字符串