2014-01-28 15 views
0

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

这里是我的代码片段(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); 

回答

5

你永远不添加JEditorPaneJScrollPane。文本组件本身没有滚动支持。

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

看看How to use scroll panes更多细节

PS- web.setBounds(rweb);只会如果您使用的是null布局和工作,如果你是,你是用的更好EmptyBorder或提供指定插入的能力的布局管理器,如GridBagLayout