【问题标题】:How can I use SwingExplorer to navigate Applet content?如何使用 SwingExplorer 浏览 Applet 内容?
【发布时间】:2013-10-17 04:45:39
【问题描述】:

此站点http://www.swingexplorer.com/ 有 SwingExplorer 工具,用于导航摇摆内容,但如何将其应用到 Applet?,特别是如果我想将它集成到 eclipse-plugin 如何配置运行配置?

我想您需要将要运行的小程序的参数提供给 AppletViwer 并让 SwingExplorer 导航 AppletViewer(它反过来运行您的小程序类),但我不知道如何将此类参数传递给AppletViwer,谁能解释我如何做到这一点?

请注意,仅仅在小程序之上创建新框架并让它像往常一样运行 Swing 应用程序是不行的,因为它需要在类似浏览器的环境中运行。

【问题讨论】:

  • “需要在类似浏览器的环境中操作” 具体是为了什么? ..JavaScript? “感谢您的回答” 感谢您抽出宝贵时间通过输入此常用短语的所有 5 个字母来帮助您进行交流。 ..哦,你没有。 -1 表示你的懒惰。
  • 例如,抛出 java.lang.NullPointerException 因为当我尝试在 Swing 应用程序中运行时,扩展的方法 Applet.getDocumentBase() 返回 null。它是遗留代码,程序相当很大,我不想更改现有程序来解决所有这些问题。谢谢,希望您能理解。
  • “谢谢,希望您能理解。” 我知道您没有收到或忽略我关于正确拼写单词的信息。既然你不介意这样做,我的时间最好花在其他地方。
  • 好的,对不起,我不知道你在谈论我的拼写(我想你指的是我如何解释我的情况,正如你所说的我误读了它),谢谢。
  • 好的。现在我们已经解决了这个问题,请参阅我的答案。

标签: java swing applet appletviewer swingutilities


【解决方案1】:

可以为框架中托管的小程序(桌面应用程序)提供基本小程序存根。 applet context 的几个方法很容易在应用程序中重现。其他的要么更难实现,不切实际,要么与基于桌面的小程序无关。

此示例可以作为嵌入在 HTML 中的小程序或小程序查看器运行,也可以作为嵌入在桌面组件中的小程序运行(特别是 JOptionPane,因为代码更短)。

该示例改编自 OP 对小程序参数更感兴趣的示例。此版本还增加了对报告文档和代码库的支持。

/*
<applet code='DesktopEmbeddedApplet' width='400' height='100'>
<param name='param' value='embedded in applet viewer or the browser'>
</applet>
*/
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.io.File;
import java.net.URL;
import java.util.HashMap;

public class DesktopEmbeddedApplet extends JApplet {

    public void init() {
        setLayout(new GridLayout(0,1));
        String param = getParameter("param");
        System.out.println("parameter: " + param);
        add(new JLabel(param));
        add(new JLabel("" + getDocumentBase()));
        add(new JLabel("" + getCodeBase()));
    }

    public static void main(String[] args) {
        ApplicationAppletStub stub = new ApplicationAppletStub();
        stub.addParameter("param", "embedded in application");
        DesktopEmbeddedApplet pa = new DesktopEmbeddedApplet();
        pa.setStub(stub);

        pa.init();
        pa.start();
        pa.setPreferredSize(new java.awt.Dimension(400,100));
        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() {
        URL url = null;
        try {
            url = new File(".").toURI().toURL();
        } catch(Exception e) {
            System.err.println("Error on URL formation!  null returned." );
            e.printStackTrace();
        }
        return url;
    }

    public URL getCodeBase() {
        URL url = null;
        try {
            url = new File(".").toURI().toURL();
        } catch(Exception e) {
            System.err.println("Error on URL formation!  null returned." );
            e.printStackTrace();
        }
        return url;
    }

    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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 2012-11-09
    • 2012-02-26
    • 2019-07-11
    • 2013-11-21
    • 2014-08-10
    相关资源
    最近更新 更多