新的浏览器窗口,其中包含 Java 小程序生成的信息

发布于 2024-08-11 16:24:01 字数 236 浏览 2 评论 0原文

我有一个 Java 小程序。

用户将数据输入到Java小程序控件中后,他按下Java小程序中的按钮,Java小程序执行一些计算,结果是一个表格(为简单起见,表格可以被视为String[][])。

我想显示新的浏览器窗口并将其中的表格输出为 html 。 这怎么能做到呢? 首选方法是在需要时使用 javascript,但不涉及某些服务器端处理。

I have a Java applet.

After user inputs data into the Java applet controls he presses button in Java applet and Java applet performs some calculating and result is a table (for simplicity table can be considered as String[][]).

I want to show new browser window and output this table in it as html <table>.
How can this be done?
Preferred way is to use javascript if needed, but not involving some server side handling.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满栀 2024-08-18 16:24:01

嗯,必须使用 JavaScript 来打开窗口并填充 HTML - 这是小程序无法访问的内容。

幸运的是,JavaScript 可以轻松调用 Java applet 提供的任何公共方法。不幸的是,Java 无法将 String[][] 返回给 JavaScript,但它可以返回 String。

因此,您可以做的是让 Java 小程序返回 String[][] 数组的 JSON 序列化。然后,JavaScript 可以使用 JSON.parse 函数将该 JSON 转换为 JavaScript 数组(在大多数现代浏览器上都可用,但在 处也有可用的库) json.org

因此,这里是我正在讨论的示例(至少适用于 Java 5 和 Chrome):

Applet 代码

import java.applet.*;
public class Test extends Applet {
    String data[][];
    public void init() {
        data = new String[5][2];
        data[0] = new String[] { "Peter", "Griffin"};
        data[1] = new String[] { "Glen", "Quagmire"};
        data[2] = new String[] { "Joe", "Something"};
        data[3] = new String[] { "Cleveland", "Brown"};
        data[4] = new String[] { "Buzz", "Killington"};
    }

    public String getData() {
        return toJSON(data);
    }

    /* Quick and dirty, you may want to look
    * online for a 'complete' converter
    *
    * This returns [["Peter", "Griffin"], ["Glen", "Quagmire"], ... etc
    */
    protected String toJSON(String data[][]) {
        int x, y;
        String s = "[";
        for (y = 0;y < data.length;y += 1) {
            s += "[";
            for (x = 0;x < data[y].length;x += 1) {
                s += "\""+data[y][x].replaceAll("\"","\\\"")+"\"";
                if (x < data[y].length-1) {
                    s += ", ";
                }
            }
            s += "]";
            if (y < data.length-1) {
                s += ", ";
            }
        }
        s += "]";

        return s;
    }
}

JavaScript 代码

<html>
<body>
<p><applet id="applet" code="Test"></applet></p>
<input id="button" type="button" value="Click me"/>

<script type="text/javascript">
(function () {
    var button = document.getElementById("button"), applet = document.getElementById("applet");
    button.onclick = function () {
        var html = [], newWindow, data = JSON.parse(applet.getData()), j;

        html.push("<table><tr><th>First Name</th><th>Last Name</th></tr>");
        for (j = 0;j < data.length;j += 1) {
            html.push("<tr><td>"+data[j][0]+"</td><td>"+data[j][1]+"</td></tr>");
        }
        html.push("</table>");

        newWindow = window.open();
        newWindow.document.firstChild.innerHTML = html.join("");
    };
}());
</script>

如果您需要进一步说明,请告诉我!

Well, JavaScript will have to be used to open the window and populate the HTML - this is something the applet simply has no access to.

Luckily, JavaScript can easily call any public methods your Java applet provides. Unfortunately, Java cannot return a String[][] to JavaScript, but it CAN return a String.

So what you can do is have your Java applet return a JSON serialization of your String[][] array. JavaScript can then convert that JSON to a JavaScript array using the JSON.parse function (available on most modern browsers, but there are also libaries available at json.org)

So here's an example of what I am talking about (that works with at least Java 5 and Chrome):

The Applet Code

import java.applet.*;
public class Test extends Applet {
    String data[][];
    public void init() {
        data = new String[5][2];
        data[0] = new String[] { "Peter", "Griffin"};
        data[1] = new String[] { "Glen", "Quagmire"};
        data[2] = new String[] { "Joe", "Something"};
        data[3] = new String[] { "Cleveland", "Brown"};
        data[4] = new String[] { "Buzz", "Killington"};
    }

    public String getData() {
        return toJSON(data);
    }

    /* Quick and dirty, you may want to look
    * online for a 'complete' converter
    *
    * This returns [["Peter", "Griffin"], ["Glen", "Quagmire"], ... etc
    */
    protected String toJSON(String data[][]) {
        int x, y;
        String s = "[";
        for (y = 0;y < data.length;y += 1) {
            s += "[";
            for (x = 0;x < data[y].length;x += 1) {
                s += "\""+data[y][x].replaceAll("\"","\\\"")+"\"";
                if (x < data[y].length-1) {
                    s += ", ";
                }
            }
            s += "]";
            if (y < data.length-1) {
                s += ", ";
            }
        }
        s += "]";

        return s;
    }
}

The JavaScript Code

<html>
<body>
<p><applet id="applet" code="Test"></applet></p>
<input id="button" type="button" value="Click me"/>

<script type="text/javascript">
(function () {
    var button = document.getElementById("button"), applet = document.getElementById("applet");
    button.onclick = function () {
        var html = [], newWindow, data = JSON.parse(applet.getData()), j;

        html.push("<table><tr><th>First Name</th><th>Last Name</th></tr>");
        for (j = 0;j < data.length;j += 1) {
            html.push("<tr><td>"+data[j][0]+"</td><td>"+data[j][1]+"</td></tr>");
        }
        html.push("</table>");

        newWindow = window.open();
        newWindow.document.firstChild.innerHTML = html.join("");
    };
}());
</script>

Let me know if you need further clarification!

离旧人 2024-08-18 16:24:01

首选方法是在需要时使用 JavaScript,但不涉及某些服务器端处理。

如果您确实不能进行任何服务器端交互,则必须是 jQuery 隐藏/显示情况。

如果您可以承担一些服务器端工作,请使用与 servlet 协作的 applet 来完成。小程序不会进行计算; servlet 将会。完成后,servlet 将结果添加到输出页面并将输出流重定向到该页面。

Preferred way is to use javascript if needed, but not involving some server side handling.

If you really must not have any server side interaction, it'll have to be a jQuery hide/show situation.

If you can bear some server side work, do it with an applet collaborating with servlet. The applet won't do the calculation; the servlet will. After it's complete, the servlet adds the result to the output page and redirects the output stream to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文