我有一个 JEditorPane,位于窗口的矩形内,因此它不会填充整个窗口。然而,当 HTML 页面变得太大时,它会切断它而不是放入滚动条,我知道我需要一个 JScrollpane 或其他东西,但我不希望滚动条在整个窗口上,只在矩形内。
这是我的代码片段(rweb 已定义为矩形):
JEditorPane web = new JEditorPane();
web.setEditable(false);
try {
web.setPage("http://www.google.com");
}catch (IOException e) {
web.setContentType("text/html");
web.setText("<html>Could not load webpage</html>");
}
web.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if(Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
rweb = new Rectangle(0, 0, width, 600);
web.setBorder(null);
web.setBounds(rweb);
window.add(web);
最佳答案
您永远不会将JEditorPane
添加到JScrollPane
。文本组件本身不支持滚动。
尝试使用window.add(new JScrollPane(web));
代替
看看How to use scroll panes了解更多详情
ps- web.setBounds(rweb);
仅在您使用 null
布局时才有效,如果是,您最好使用 EmptyBorder
或提供指定插入功能的布局管理器,例如 GridBagLayout
关于java - 矩形中 JEditorPane 中的滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21397745/