嵌入第三方JApplet的在一个Swing GUI和放大器;它传递参数 [英] Embed a 3rd-party JApplet in a Swing GUI & pass it parameters

查看:141
本文介绍了嵌入第三方JApplet的在一个Swing GUI和放大器;它传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个第三方的小程序,我想在我的Swing应用程序中嵌入。基本上,我想它只是另一个面板。此applet使许多参数的使用,例如

There's a third-party applet that I'd like to embed in my Swing application. Basically, I'd like it to be just another panel. This applet makes use of many parameters, e.g.

final String config_filename = getParameter(XXX);

我见过许多关于如何通过HTML发送参数值的文档,但你如何通过code(或者属性文件)呢?任何帮助将是AP preciated!

I've seen lots of documentation about how to send parameters values via HTML, but how do you do it via code (or perhaps property files)? Any help would be appreciated!

推荐答案

实施的 AppletStub &安培;将其设置为小应用程序实例的存根。例如。

Implement an AppletStub & set it as the stub of the applet instance. E.G.

/*
<applet code='ParamApplet' width='200' height='200'>
<param name='param' value='foo'>
</applet>
*/
import java.applet.*;
import javax.swing.*;
import java.net.URL;
import java.util.HashMap;

public class ParamApplet extends JApplet {

    public void init() {
        String param = getParameter("param");
        System.out.println("parameter: " + param);
        add(new JLabel(param));
    }

    public static void main(String[] args) {
        ApplicationAppletStub stub = new ApplicationAppletStub();
        stub.addParameter(args[0], args[1]);
        ParamApplet pa = new ParamApplet();
        pa.setStub(stub);

        pa.init();
        pa.start();
        pa.setPreferredSize(new java.awt.Dimension(200,200));
        JOptionPane.showMessageDialog(null, pa);
    }
}

class ApplicationAppletStub implements AppletStub {

    HashMap<String,String> params = new HashMap<String,String>();

    public void appletResize(int width, int height) {}
    public AppletContext getAppletContext() {
        return null;
    }

    public URL getDocumentBase() {
        return null;
    }

    public URL getCodeBase() {
        return null;
    }

    public boolean isActive() {
        return true;
    }

    public String getParameter(String name) {
        return params.get(name);
    }

    public void addParameter(String name, String value) {
        params.put(name, value);
    }
}

典型的I / O

prompt>java ParamApplet param "apples & oranges"
parameter: apples & oranges

prompt>java ParamApplet param 42
parameter: 42

prompt>

这篇关于嵌入第三方JApplet的在一个Swing GUI和放大器;它传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆