2014-11-04 36 views
0

我有下面的類:搖擺 - JEditorPane中內部JScrollPane中 - 線不被「包裝」

import java.awt.*; 
import javax.swing.*; 
import javax.swing.text.html.HTMLEditorKit; 

public class test 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
      // now add it all to a frame 
      JFrame j = new JFrame("Test"); 

      WorkBench right = new WorkBench(); 

      j.getContentPane().add(right, BorderLayout.CENTER); 

      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 


      // center the jframe, then make it visible 
      j.setSize(800, 600); 
      j.setLocationRelativeTo(null); 
      j.setVisible(true); 
      } 
     }); 
     } 


    public static JPanel getPortal() 
    { 
     JEditorPane swingbox = new JEditorPane(); 


     swingbox.setEditorKit(new HTMLEditorKit()); 

     swingbox.setOpaque(true); 

     swingbox.setContentType("text/html"); 

     String htmlString = "<html>\n" 
     + "<body>\n" 
     + "<h1>Welcomfve! sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h1>\n" 
     + "<h2>This is an H2 header. sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h2>\n" 
     + "<p>This is some sample text sdsdsdsdsd sdsdsd sdsd sdsd sdsd </p>\n" 
     + "<p><a href=\"http://devdaily.com/blog/\">devdaily blog</a></p>\n" 
     + "</body>\n"; 
     swingbox.setText(htmlString); 


     swingbox.setBackground(Color.WHITE); 


     JPanel p = new JPanel(new BorderLayout()); 

     p.setBackground(Color.WHITE); 

     JLabel title = new JLabel("Heading "); 
     title.setBackground(Color.WHITE); 
     p.add(title, BorderLayout.NORTH); 
     p.add(swingbox, BorderLayout.CENTER); 


     return p; 
    } 

    public static class WorkBench extends JPanel 
    { 
     private Box current = null; 

     public WorkBench() 
     { 
      setLayout(new BorderLayout()); 

      current = Box.createVerticalBox(); 

      //** Scenario A - no vertical scrolling ** 
       add(current, BorderLayout.CENTER); 

      //** Scenario B - no line wrapping ** 
       //JScrollPane sp = new JScrollPane(current); 
       //sp.setViewportView(current); 
       //add(sp, BorderLayout.CENTER); 


     } 


     public void addPortal(JPanel portal) 
     { 
      current.add(portal); 

      validate(); 
     } 

    } 
} 

我要添加多個JPanels,使用addPortal方法,到垂直框,它被包圍在JScrollPane中。 JPanels使用BorderLayout並在中心顯示一個JEditorPane,它顯示可變長度的HTML。

問題是,當我把垂直框放在JScrollpane內,然後JEditorPanes的換行停止工作。如果我添加沒有Jscrollpane的垂直框,則換行可行,但是,如您所期望的那樣,不滾動。我如何獲得線包裝+滾動?

爲了讓事情更清晰,見下圖:

  • 在A,我已經直接添加垂直框JPanel的工作臺。 請注意,包線工作,但沒有垂直滾動 。
  • 在B中,我已經添加了一個JScrollPane中的垂直框,其中我添加到了JPanel WorkBench中,內容爲 。請注意垂直滾動條 存在,但換行不起作用。

http://i.stack.imgur.com/9K49t.png

+1

正從您可運行實例的任何機會呢? – 2014-11-04 08:57:37

+1

http://java-sl.com/tip_text_height_measuring.html使用該方法來確定您的內容html的高度並定義寬度 – StanislavL 2014-11-04 09:18:16

+0

謝謝大家。我剛剛搬到一個新的住所,很抱歉拖延。我會盡快回復! – SoupMonster 2014-11-07 08:24:49

回答

1

找到一個有效的解決方案:http://i.stack.imgur.com/63PHx.jpg

import javax.swing.*; 
import javax.swing.text.html.HTMLEditorKit; 

public class test 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
      // now add it all to a frame 
      JFrame j = new JFrame("Test"); 

      WorkBench right = new WorkBench(); 

      JScrollPane sp = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
      sp.setAutoscrolls(false); 

      sp.setViewportView(right); 


      j.getContentPane().add(sp, BorderLayout.CENTER); 

      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 
      right.addPortal(getPortal()); 



      SwingUtilities.invokeLater(new Runnable() { 
       public void run() { 
        sp.getViewport().setViewPosition(new java.awt.Point(0, 0)); 
       } 
      }); 


      // center the jframe, then make it visible 
      j.setSize(800, 600); 
      j.setLocationRelativeTo(null); 
      j.setVisible(true); 
      } 
     }); 
     } 


    public static JPanel getPortal() 
    { 
     JEditorPane swingbox = new JEditorPane(); 


     swingbox.setEditorKit(new HTMLEditorKit()); 

     swingbox.setOpaque(true); 

     swingbox.setContentType("text/html"); 

     String htmlString = "<html>\n" 
     + "<body>\n" 
     + "<h1>Welcomfve! sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h1>\n" 
     + "<h2>This is an H2 header. sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h2>\n" 
     + "<p>This is some sample text sdsdsdsdsd sdsdsd sdsd sdsd sdsd </p>\n" 
     + "<p><a href=\"http://devdaily.com/blog/\">devdaily blog</a></p>\n" 
     + "</body>\n"; 
     swingbox.setText(htmlString); 


     swingbox.setBackground(Color.WHITE); 


     JPanel p = new JPanel(new BorderLayout()); 

     p.setBackground(Color.WHITE); 

     JLabel title = new JLabel("Heading "); 
     title.setBackground(Color.WHITE); 
     p.add(title, BorderLayout.NORTH); 



     p.add(swingbox, BorderLayout.CENTER); 


     return p; 
    } 

    public static class WorkBench extends JPanel implements Scrollable 
    { 
     Box vertical_box = null; 

     public WorkBench() 
     { 
      setLayout(new BorderLayout()); 

      this.vertical_box = Box.createVerticalBox(); 

      //** Scenario A - no vertical scrolling ** 
       add(this.vertical_box , BorderLayout.CENTER); 

      //** Scenario B - no line wrapping ** 
       //JScrollPane sp = new JScrollPane(current); 
       //sp.setViewportView(current); 
       //add(sp, BorderLayout.CENTER); 


     } 

     public void addPortal(JPanel portal) 
     { 
      this.vertical_box.add(portal); 

      validate(); 
     } 

     @Override 
     public Dimension getPreferredScrollableViewportSize() 
     { 
      return getPreferredSize(); 
     } 


     @Override 
     public int getScrollableUnitIncrement(
       Rectangle visibleRect, 
       int orientation, 
       int direction) 
     { 
      return 20; 
     } 


     @Override 
     public int getScrollableBlockIncrement(
       Rectangle visibleRect, 
       int orientation, 
       int direction) 
     { 
      return 60; 
     } 


     @Override 
     public boolean getScrollableTracksViewportWidth() 
     { 
      return true; 
     } 


     @Override 
     public boolean getScrollableTracksViewportHeight() 
     { 
      if (getParent() instanceof JViewport) 
      { 
       return (((JViewport)getParent()).getHeight() > getPreferredSize().height); 
      } 

      return false; 
     } 
    } 
} 
+0

是不是情景A - 「沒有**水平**滾動」? – 2014-11-24 09:54:28

+0

我不這麼認爲,看着http://i.stack.imgur.com/9K49t.png - 行被包裹,但沒有垂直滾動? – SoupMonster 2014-11-25 10:58:09

+0

這很令人困惑,因爲換行也意味着「沒有水平滾動條」(但可能是垂直滾動條)。如果您的端口太多以適應屏幕會發生什麼? – 2014-11-25 12:25:27

2

JEditorPane應該總是自己調整到必要的內部顯示文本的確切大小。這就是爲什麼它與JScrollPane一起工作的原因:內部組件增長,然後滾動窗格會詢問大小並相應地配置滾動條。

您不會向我們展示如何創建您嘗試添加的JPanel門戶。我的猜測是這段代碼有問題。也許BorderLayout攪亂了事情。

我建議你換JPanelComponent,沒有任何佈局或包裝組件添加JEditorPane在面板上JScrollPane內。這應該向BoxLayout公開正確的尺寸,並因此正確調整視口的大小。

+0

謝謝亞倫 - 這讓我走上了正軌。我得到了正確調整大小的視口(請參閱示例),但是現在JEditorPane的換行被打破了。有任何想法嗎? – SoupMonster 2014-11-20 16:56:36

+0

也許如果你告訴我什麼「壞了」。 – 2014-11-20 17:00:40

+0

當然,請參閱上次編輯。圖像鏈接是否正常工作? – SoupMonster 2014-11-20 17:18:12