addMouseListener到程序

时间:2012-03-26 10:32:05

标签: java html swing mouseevent mouselistener

我试图覆盖mouseClicked的{​​{1}}函数,然后我可以在我的程序中使用它,如下所示:

MouseAdapter

但它不起作用。如果我填写表格的任何部分,没有任何反应。

我尝试import java.awt.Component; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.net.URLConnection; import javax.swing.JFrame; import javax.xml.parsers.ParserConfigurationException; import org.lobobrowser.html.HtmlRendererContext; import org.lobobrowser.html.UserAgentContext; import org.lobobrowser.html.domimpl.HTMLLinkElementImpl; import org.lobobrowser.html.gui.HtmlPanel; import org.lobobrowser.html.parser.DocumentBuilderImpl; import org.lobobrowser.html.parser.InputSourceImpl; import org.lobobrowser.html.test.SimpleHtmlRendererContext; import org.lobobrowser.html.test.SimpleUserAgentContext; import org.w3c.dom.Document; import org.w3c.dom.html2.HTMLLinkElement; import org.w3c.dom.html2.HTMLElement; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class test { public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException { String uri = "http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/MouseAdapter.html"; URL url = new URL(uri); URLConnection connection = url.openConnection(); InputStream in = connection.getInputStream(); Reader reader = new InputStreamReader(in); InputSource is = new InputSourceImpl(reader, uri); UserAgentContext uAgent = new SimpleUserAgentContext(); final HtmlPanel htmlPanel = new HtmlPanel(); final HtmlRendererContext rendererContext = (HtmlRendererContext) new LocalHtmlRendererContext(htmlPanel, uAgent); DocumentBuilderImpl builder = new DocumentBuilderImpl(uAgent, rendererContext); Document document = builder.parse(is); JFrame frame = new JFrame(); frame.setContentPane(htmlPanel); htmlPanel.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { System.out.println("adr is equal to" + rendererContext.getCurrentURL()); System.out.println("Clicked!" + e); } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); htmlPanel.setDocument(document, rendererContext); frame.setSize(400, 800); frame.setVisible(true); } private static class LocalHtmlRendererContext extends SimpleHtmlRendererContext { public LocalHtmlRendererContext(HtmlPanel contextComponent, UserAgentContext uAgent) { super(contextComponent); } } } htmlPanel.addMouseListener(new MouseAdapter()但都没有帮助。

编辑:

我尝试了你的建议,我选择了BrowsertPanel而不是HtmlPanel,然后我试着让它的组件如下:

frame.addMouseListener(new MouseAdapter()

我还添加了一个navigationListener,当它导航时,我得到如下事件:

org.lobobrowser.gui.AddressBarPanel[,0,0,0x0,invalid,layout=javax.swing.BoxLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
org.lobobrowser.gui.SharedToolBarPanel[,0,0,0x0,invalid,layout=javax.swing.BoxLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
org.lobobrowser.gui.FillerComponent[,0,0,0x0,invalid,layout=org.lobobrowser.util.gui.WrapperLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=java.awt.Dimension[width=32767,height=32767],minimumSize=java.awt.Dimension[width=0,height=0],preferredSize=java.awt.Dimension[width=32767,height=32767]]
org.lobobrowser.gui.StatusBarPanel[,0,0,0x0,invalid,layout=javax.swing.BoxLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.BevelBorder@c2a132,flags=9,maximumSize=,minimumSize=,preferredSize=]

但是我觉得我在这些组件中丢失了,似乎org.lobobrowser.ua.NavigationEvent[source=FramePanel[windowId=BrowserPanel.10481519,hashCode=23491286,parent=org.lobobrowser.gui.FillerComponent[,0,25,584x316,layout=org.lobobrowser.util.gui.WrapperLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=java.awt.Dimension[width=32767,height=32767],minimumSize=java.awt.Dimension[width=0,height=0],preferredSize=java.awt.Dimension[width=32767,height=32767]]]] FillerComponent负责渲染,即便如此,我也不知道如何向他们添加监听器。

还发现这个Link有人描述了解决问题的步骤,但对我来说似乎很复杂?是否有任何伪代码要分发?

2 个答案:

答案 0 :(得分:4)

此代码(是SSCCE&)有效。尝试一次更改一行,直到看到错误清单。

import java.awt.event.*;
import javax.swing.*;
import java.net.URL;

public  class test {

    public static void main(String[] args) throws Exception {

        String uri = "http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/MouseAdapter.html";
        final URL url = new URL(uri);
        JEditorPane htmlPanel = new JEditorPane(url);
        JFrame frame = new JFrame();
        frame.setContentPane(htmlPanel);

        htmlPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("adr is equal to" + url);

                System.out.println("Clicked!" + e);
            }
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 800);
        frame.setVisible(true);
    }
}

更新

  

..似乎bug来自htmlpanel

根据StanislavL的评论,我做了一些挖掘。似乎SimpleBrowserFrame延伸HtmlPanel&提供方法getComponent(),其中......

  

获取呈现框架的组件。 ..

也许如果使用SimpleBrowser,您可以将监听器添加到子级,它可以工作。不可否认,我只是在通过文档快速拖网后猜测。

答案 1 :(得分:3)

只是一个猜测。检查htmlPanel的子组件。可能是他们覆盖整个面板。在HtmlPanel填充内容后,尝试将侦听器附加到所有子组件。