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

swing java应用程序和iframe-双向通信,java,html,swing,iframe,Java,Html,Swing,Iframe,我有一个JavaSwing应用程序,希望在iframe中呈现html页面。根据我在iframe中所做的工作,参数或数据可以传回我的JavaSwing应用程序吗 谢谢一般来说,您会使用一个JEditorPane来显示HTML。Swing支持软件Monkey所说的HTML3.2(Wilbur)。您可以在以下网址找到此过时(1996)版HTML的官方文档: 关于此主题的Java 7文档: 尽管值得注意的是,它没有明确提及此信息,但它对其他Swing组件有效 根据您的要求,有两种方法: Swing组件实

我有一个JavaSwing应用程序,希望在iframe中呈现html页面。根据我在iframe中所做的工作,参数或数据可以传回我的JavaSwing应用程序吗


谢谢

一般来说,您会使用一个JEditorPane来显示HTML。Swing支持软件Monkey所说的HTML3.2(Wilbur)。您可以在以下网址找到此过时(1996)版HTML的官方文档:

关于此主题的Java 7文档:

尽管值得注意的是,它没有明确提及此信息,但它对其他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); }
            }
        });
    }
}
import java.awt.*;
导入java.util.*;
导入java.io.*;
导入javax.swing.*;
导入javax.swing.text.*;
导入javax.swing.text.html.*;
公共类GetComponent扩展了JFrame
{
公共GetComponent()
抛出异常
{
FileReader=newFileReader(“form.html”);
JEditorPane编辑器=新的JEditorPane();
setContentType(“text/html”);
editor.setEditable(false);
editor.read(reader,null);
JScrollPane scrollPane=新的JScrollPane(编辑器);
scrollPane.setPreferredSize(新维度(400300));
添加(滚动窗格);
setDefaultCloseOperation(关闭时退出);
包装();
setLocationRelativeTo(空);
setVisible(真);
//显示文档的属性
HTMLDocument文档=(HTMLDocument)编辑器.getDocument();
ElementIterator it=新的ElementIterator(文档);
元素;
while((element=it.next())!=null)
{
System.out.println();
AttributeSet as=element.getAttributes();
枚举enumm=as.getAttributeNames();
while(enumm.hasMoreElements())
{
对象名称=enumm.nextElement();
对象值=as.getAttribute(名称);
System.out.println(“\t”+名称+”:“+值);
if(DefaultComboBoxModel的值实例)
{
DefaultComboxModel=(DefaultComboxModel)值;
对于(int j=0;j
您可以查看允许将Google Chromium引擎嵌入Java Swing应用程序的库

它为Java到JavaScript到Java的双向通信提供API:

下面的代码演示了如何嵌入浏览器组件、加载URL、在加载的网页上调用JavaScript代码以及在JavaScript端注册Java函数,每次JavaScript调用该函数时都会调用该函数:

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

谢谢Dipali,将信息传递回我的swing应用程序怎么样?例如,html内容中的某些按钮或链接会触发我的swing应用程序中的行为(也会传回数据)