gpt4 book ai didi

java - 将MouseListener添加到程序中

转载 作者:行者123 更新时间:2023-12-01 07:37:32 26 4
gpt4 key购买 nike

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

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()frame.addMouseListener(new MouseAdapter()) 但没有帮助。

编辑:

我尝试了你的建议,我选择了 BrowsertPanel 而不是 HtmlPanel,然后我尝试获取其组件,如下所示:

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=]

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

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]]]]

但我觉得我迷失在这些组件中,似乎 FillerComponentFramePanel 负责渲染,即便如此,我不知道如何向它们添加监听器.

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

最佳答案

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

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

更新

..seems bug is from htmlpanel

根据 StanislavL 的评论,我做了一些挖掘。看来是SimpleBrowserFrame扩展 HtmlPanel & 提供方法 getComponent()其中..

Gets the component that renders the frame. ..

如果使用了SimpleBrowser,您可以将监听器添加到子级中,这样就可以工作了。诚然,我只是在快速浏览文档后猜测。

关于java - 将MouseListener添加到程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9870527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com