gpt4 book ai didi

java - JEdi​​torPane 的奇怪行为

转载 作者:行者123 更新时间:2023-11-30 07:54:41 24 4
gpt4 key购买 nike

我创建了一个自定义类,它扩展了 JEditorPane 并使用了它的 setPage() 方法。但是我在使用它时遇到了一个非常奇怪的问题。这就是我实现它的方式;

class WebReader extends JEditorPane {

WebReader(String addressIn) {
setEditable(false);
showPage(addressIn)
}

void showPage(String address) {
try {
setPage(address);
} catch (Exception e) {
e.printStackTrace();
}
}

调用可能看起来像这样;

WebReader fooReader = new WebReader("https://www.google.com");
fooReader.showPage("https://www.google.comxxxx");

这本不应该起作用但神秘地起作用了。

非常奇怪的是,如果我已经输入了正确的 URL,它不会捕获错误的 URL。例如,如果我输入“https://www.google.com”,效果很好(应该如此),然后输入 https://www.google.comxxxxx ,它仍然在我的 JEditorPane 上显示 google.com 并且没有引发异常(我希望它这样做)。

值得注意的是,如果我输入 https://www.google.comxxxxx作为我的“起始网址”,它确实会引发异常。

编辑:添加了更多代码。

最佳答案

如果页面是异步加载的(在后台),你不会得到 IOException。是否异步加载 URL 取决于 EditorKit 为您正在加载的内容类型安装的文档。来自documentation for JEditorPane.setPage :

This may load either synchronously or asynchronously depending upon the document returned by the EditorKit. If the Document is of type AbstractDocument and has a value returned by AbstractDocument.getAsynchronousLoadPriority that is greater than or equal to zero, the page will be loaded on a separate thread using that priority.

If the document is loaded synchronously, it will be filled in with the stream prior to being installed into the editor with a call to setDocument, which is bound and will fire a property change event. If an IOException is thrown the partially loaded document will be discarded and neither the document or page property change events will be fired. If the document is successfully loaded and installed, a view will be built for it by the UI which will then be scrolled if necessary, and then the page property change event will be fired.

If the document is loaded asynchronously, the document will be installed into the editor immediately using a call to setDocument which will fire a document property change event, then a thread will be created which will begin doing the actual loading. In this case, the page property change event will not be fired by the call to this method directly, but rather will be fired when the thread doing the loading has finished. It will also be fired on the event-dispatch thread. Since the calling thread can not throw an IOException in the event of failure on the other thread, the page property change event will be fired when the other thread is done whether the load was successful or not.

显然在这种情况下,网页的 EditorKit 是 HTMLEditorKit。来自documentation of HTMLEditorKit :

Larger documents involve a lot of parsing and take some time to load. By default, this kit produces documents that will be loaded asynchronously if loaded using JEditorPane.setPage.

解决方法是自己同步加载文档,而不是使用 JEditorPane.setPage:

Document doc;

URLConnection connection = new URL(url).openConnection();
try (InputStream stream = connection.getInputStream()) {
String contentType = connection.getContentType();
EditorKit editorKit =
JEditorPane.createEditorKitForContentType(contentType);
doc = editorKit.createDefaultDocument();
editorKit.read(stream, doc, 0);
}

pane.setDocument(doc);

关于java - JEdi​​torPane 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43870900/

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