swing java应用程序和iframe-双向通信

qzlgjiam  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(356)

我有一个javaswing应用程序,希望在iframe中呈现html页面。根据我在iframe中所做的工作,参数或数据能否传回我的javaswing应用程序?
谢谢

bfrts1fy

bfrts1fy1#

您可以看看jxbrowser库,它允许将GoogleChromium引擎嵌入JavaSwing应用程序。
它为java到javascript到java的双向通信提供api:http://www.teamdev.com/downloads/jxbrowser/docs/jxbrowser-pguide.html#javascript- java 桥
下面的代码演示了如何嵌入浏览器组件、加载url、在加载的网页上调用javascript代码以及在javascript端注册每次javascript调用时都将调用的java函数:

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserFactory;
import com.teamdev.jxbrowser.chromium.BrowserFunction;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;

/**
 * The sample demonstrates how to register a new JavaScript function and
 * map it to a Java method that will be invoked every time when the JavaScript
 * function is invoked.
 */
public class JavaScriptJavaSample {
    public static void main(String[] args) {
        Browser browser = BrowserFactory.create();
        // Register "MyFunction" JavaScript function and associate Java callback with it
        browser.registerFunction("MyFunction", new BrowserFunction() {
            public JSValue invoke(JSValue... args) {
                for (JSValue arg : args) {
                    System.out.println("arg = " + arg);
                }
                return JSValue.create("Hello!");
            }
        });

        // Create JFrame and embed Browser component to display web pages
        JFrame frame = new JFrame();
        frame.add(browser.getView().getComponent(), BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        // Register Load listener to get notification when web page is loaded completely
        browser.addLoadListener(new LoadAdapter() {
            @Override
            public void onFinishLoadingFrame(FinishLoadingEvent event) {
                if (event.isMainFrame()) {
                    Browser browser = event.getBrowser();
                    // Invoke our registered JavaScript function
                    JSValue returnValue = browser.executeJavaScriptAndReturnValue(
                            "MyFunction('Hello JxBrowser!', 1, 2, 3, true);");
                    System.out.println("return value = " + returnValue);
                }
            }
        });
        browser.loadURL("about:blank");
    }
}
yqhsw0fo

yqhsw0fo2#

通常您会使用一个jeditorpane来显示html。swing支持软件monkey所说的html3.2(wilbur)。您可以在以下网址找到此已过时(1996)版html的官方文档:http://www.w3.org/tr/rec-html32.html
java 7文档主题:http://docs.oracle.com/javase/7/docs/api/javax/swing/text/html/package-summary.html
尽管值得注意的是,它没有明确提到这个信息对于其他swing组件是有效的。
根据您的要求,有两种方法:
swing组件实际上被添加到编辑器窗格中。因此,一旦文档被解析并且编辑器窗格被重新验证,您应该能够得到添加到编辑器窗格的所有组件的列表。您可以检查类名以找到所需的组件。
htmldocument包含关于添加的每个组件的属性,包括组件模型。因此,您可以搜索文档以获取每个复选框的模型。
以下是一些开始使用的通用代码:

import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class GetComponent extends JFrame
{
    public GetComponent()
        throws Exception
    {
        FileReader reader = new FileReader("form.html");

        JEditorPane editor = new JEditorPane();
        editor.setContentType( "text/html" );
        editor.setEditable( false );
        editor.read(reader, null);

        JScrollPane scrollPane = new JScrollPane( editor );
        scrollPane.setPreferredSize( new Dimension(400, 300) );
        add( scrollPane );

        setDefaultCloseOperation( EXIT_ON_CLOSE );
        pack();
        setLocationRelativeTo( null );
        setVisible(true);

        //  display the attributes of the document

        HTMLDocument doc = (HTMLDocument)editor.getDocument();
        ElementIterator it = new ElementIterator(doc);
        Element element;

        while ( (element = it.next()) != null )
        {
            System.out.println();

            AttributeSet as = element.getAttributes();
            Enumeration enumm = as.getAttributeNames();

            while( enumm.hasMoreElements() )
            {
                Object name = enumm.nextElement();
                Object value = as.getAttribute( name );
                System.out.println( "\t" + name + " : " + value );

                if (value instanceof DefaultComboBoxModel)
                {
                    DefaultComboBoxModel model = (DefaultComboBoxModel)value;

                    for (int j = 0; j < model.getSize(); j++)
                    {
                        Object o = model.getElementAt(j);
                        Object selected = model.getSelectedItem();
                        System.out.print("\t\t");

                        if ( o.equals( selected ) )
                            System.out.println( o + " : selected" );
                        else
                            System.out.println( o );
                    }
                }
            }
        }

        //  display the components added to the editor pane

        for (Component c: editor.getComponents())
        {
            Container parent = (Container)c;
            System.out.println(parent.getComponent(0).getClass());
        }
    }

    public static void main(String[] args)
        throws Exception
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    GetComponent frame = new GetComponent();
                }
                catch(Exception e) { System.out.println(e); }
            }
        });
    }
}

相关问题