我试图覆盖mouseClicked
的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()
和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]]]]
但是我觉得我在这些组件中迷失了,似乎
FillerComponent
或FramePanel
负责渲染,即使如此,我也不知道如何向它们添加侦听器。还发现有人描述解决问题的步骤Link,但对我来说似乎如此复杂?是否有任何伪代码要分发?
最佳答案
此代码(是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);
}
}
更新资料
..seems错误来自htmlpanel
根据StanislavL的评论,我做了一些挖掘工作。似乎
SimpleBrowserFrame
扩展了HtmlPanel
并提供了方法getComponent()
。获取渲染框架的组件。 ..
也许如果使用
SimpleBrowser
,则可以将侦听器添加到子级中,这样就可以了。诚然,我只是在快速搜寻文档之后猜测。关于java - addMouseListener到程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9870527/