JEdi​​torPane中的滚动条位于矩形中

时间:2014-01-28 05:41:57

标签: java swing jscrollpane jeditorpane

我有一个JEditorPane,它位于窗口的矩形内,因此它不会填满整个窗口。然而,当HTML页面变得太大时,它会切断它而不是放入滚动条,我知道我需要一个JScrollpane或者其他东西,但是我不希望整个窗口上的滚动条,只在矩形内部。

以下是我的代码片段(rweb已定义为Rectangle):

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

1 个答案:

答案 0 :(得分:5)

您永远不会将JEditorPane添加到JScrollPane。文本组件本身没有滚动支持。

尝试使用window.add(new JScrollPane(web));代替

请查看How to use scroll panes了解详情

ps- web.setBounds(rweb);只有在您使用null布局时才有效,如果您使用,您最好使用EmptyBorder或提供此功能的布局管理器指定插入内容,例如GridBagLayout