[英]Scrolling to the other part of the webpage
我正在渲染網頁,並嘗試滾動到其中的某個位置。 但是,滾動不起作用。
這是我的代碼...
import org.lobobrowser.html.*;
import org.lobobrowser.html.gui.HtmlPanel;
import org.lobobrowser.html.parser.*;
import org.lobobrowser.html.test.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class finall {
Node goTo;
public void show(URL url,Node theFinalNode) throws MalformedURLException, IOException, SAXException {
goTo = theFinalNode;
String uri=url.toString();
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();
HtmlRendererContext rendererContext = (HtmlRendererContext) new LocalHtmlRendererContext(htmlPanel, uAgent);
DocumentBuilderImpl builder = new DocumentBuilderImpl(uAgent, rendererContext);
Document document = builder.parse(is);
JFrame frame = new JFrame();
frame.getContentPane().add(htmlPanel);
htmlPanel.setDocument(document, rendererContext);
frame.setSize(300, 450);
frame.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
htmlPanel.scrollTo(goTo);
}
});
}
有人可以幫我理解為什么滾動不起作用。
我認為可能不是在滾動,因為您的HtmlPanel
沒有添加到JScrollPane
的GUI中。 嘗試更改以下代碼...
JFrame frame = new JFrame();
frame.add(new JScrollPane(htmlPanel)); // CHANGED LINE HERE
htmlPanel.setDocument(document, rendererContext);
// Set the size of the JFrame when the root
// component does not have a preferred size.
frame.setSize(300, 450);
frame.setVisible(true);
現在當您的htmlPanel.scrollTo(goTo);
在稍后執行時,它應該能夠滾動到該位置。
在我看來,好像您要傳遞給show方法的Node不在HtmlPanel正在查看的文檔中。 在您的代碼中,使用以下命令生成文檔:
Document document = builder.parse(is);
這將創建一個新文檔以及與之關聯的許多新節點。 參數theFinalNode將不屬於該文檔,因為它是在創建文檔之前創建的。 您將需要通過調用文檔對象上的方法或使用類似XPath的方法從新文檔中提取所需的Node:
http://www.roseindia.net/tutorials/xPath/java-xpath.shtml
一旦有一個Node實際上是所查看文檔的一部分,那么scrollTo方法就應該起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.