2016-12-20 104 views
1

我正在嘗試使用swing創建可滾動面板。但是因爲我是新手,所以我沒有這樣做。在我的代碼中,沒有任何內容顯示在jScrollablePane內。Java Swing - 可滾動面板

我創建了一個的JFrame並投入jScrollablePane裏面。我創建了一個名爲UIElement的類,該類擴展了jPanel,其中包含組件部分和子部分。兒童部分由其他UIElement s組成。

  • 所有UI元素應劃歸以前的UIElement。就像一個垂直列表。
  • 孩子應該放置在組件
  • 孩子節不能滾動,其高度固定爲兒童UIElements的總高度。
  • 組件的固定高度爲40.雖然寬度可以隨其容器一起延伸。
  • 間隔的寬度應該被固定爲40。
  • 除了從間隔,一切的寬度是動態的。
  • A UIElement可能有零個或更多的孩子。

下面是一些圖片的例子,不要忘記:寬度是動態的。

enter image description here

這裏是代碼:

public static void scrollable(String title) { 
    //Create and set up the window. 
    JFrame frame = new JFrame(title); 
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    frame.setSize(500,GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height); 

    JPanel mainPanel = new JPanel(); 
    mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS)); 
    mainPanel.setBackground(Color.WHITE); 

    mainPanel.add(new UIElement()); 

    JScrollPane scrollPane = new JScrollPane(mainPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
    frame.getContentPane().add(scrollPane); 

    //Display the window. 
    frame.setVisible(true); 
} 

這裏是的UIElement類:

public abstract class UIElement extends JPanel { 
    public static final int DESIREDHEIGHT = 40; 

    JPanel componentsPanel, childPanel; 

    public UIElement() { 
     super(); 
     this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS)); 
     this.setBackground(Color.BLACK); 

     // componentsPanel 
     componentsPanel = new JPanel() { 
      @Override 
      public Dimension getPreferredSize() { 
       Dimension d = super.getPreferredSize(); 
       d.setSize(d.getWidth(),DESIREDHEIGHT); 
       return d; 
      } 

      @Override 
      public Dimension getMaximumSize() { 
       Dimension d = super.getMaximumSize(); 
       d.setSize(d.getWidth(),DESIREDHEIGHT); 
       return d; 
      } 

      @Override 
      public Dimension getMinimumSize() { 
       Dimension d = super.getPreferredSize(); 
       d.setSize(d.getWidth(),DESIREDHEIGHT); 
       return d; 
      } 
     }; 
     componentsPanel.setLayout(new BoxLayout(componentsPanel,BoxLayout.X_AXIS)); 
     componentsPanel.setBackground(Color.BLUE); 
     this.add(componentsPanel); 

     // child panel 
     childPanel = new JPanel() { 
      @Override 
      public Dimension getPreferredSize() { 
       float height = 0; 
       for (Component c: super.getComponents()) 
        height += c.getPreferredSize().height; 
       Dimension d = super.getPreferredSize(); 
       d.setSize(d.getWidth(),height); 
       return d; 
      } 

      @Override 
      public Dimension getMaximumSize() { 
       float height = 0; 
       for (Component c: super.getComponents()) 
        height += c.getMaximumSize().height; 
       Dimension d = super.getMaximumSize(); 
       d.setSize(d.getWidth(),height); 
       return d; 
      } 

      @Override 
      public Dimension getMinimumSize() { 
       float height = 0; 
       for (Component c: super.getComponents()) 
        height += c.getMinimumSize().height; 
       Dimension d = super.getMinimumSize(); 
       d.setSize(d.getWidth(),height); 
       return d; 
      } 
     }; 
     childPanel.setLayout(new BoxLayout(childPanel,BoxLayout.Y_AXIS)); 
     childPanel.setBackground(Color.RED); 

     // parent for childPanel and spacer 
     JPanel childParent = new JPanel(); 
     childParent.setLayout(new BoxLayout(childParent,BoxLayout.X_AXIS)); 

     // spacer 
     JPanel spacer = new JPanel() { 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(DESIREDHEIGHT,DESIREDHEIGHT); 
      } 

      @Override 
      public Dimension getMaximumSize() { 
       return new Dimension(DESIREDHEIGHT,DESIREDHEIGHT); 
      } 

      @Override 
      public Dimension getMinimumSize() { 
       return new Dimension(DESIREDHEIGHT,DESIREDHEIGHT); 
      } 
     }; 
     spacer.setBackground(Color.RED); 
     childParent.add(spacer); 
     // add the childPanel later 
     childParent.add(childPanel); 
    } 


    @Override 
    public Dimension getPreferredSize() { 
     Dimension c = componentsPanel.getPreferredSize(); 
     Dimension d = childPanel.getPreferredSize(); 
     return new Dimension(c.width, c.height + d.height); 
    } 

    @Override 
    public Dimension getMaximumSize() { 
     Dimension c = componentsPanel.getMaximumSize(); 
     Dimension d = childPanel.getMaximumSize(); 
     return new Dimension(c.width, c.height + d.height); 
    } 

    @Override 
    public Dimension getMinimumSize() { 
     Dimension c = componentsPanel.getMinimumSize(); 
     Dimension d = childPanel.getMinimumSize(); 
     return new Dimension(c.width, c.height + d.height); 
    } 

} 

回答

1

不要覆蓋getPreferredSize()(或最大/微量大小)的面板。

每個面板將根據面板的佈局管理器和添加到面板的組件確定其首選大小。

這就是說,如果面板使用佈局管理器,則不需要重寫這些方法。您只需要覆蓋沒有佈局管理器的自定義組件的getPreferredSize()方法。在Swing中,這將是一個JButton,JTextField,JLabel等。

所以,如果你正在創建一個自定義組件,那麼你設置了大小。如果您只是使用組件作爲容器和佈局管理器來容納其他組件,那麼您不會計算大小。

+0

https://docs.oracle.com/javase/tutorial/uiswing/layout/problems。html - 搜索單詞「修復」 – ossobuko

+0

刪除了getPreferredSize()方法的所有覆蓋項,沒有工作 – ossobuko

+0

@ossobuko,您錯過了我關於需要定義適當大小的「自定義組件」的觀點。如果您使用的只是JPanel,那麼層次結構底部的自定義面板需要適當的大小。在我看來使用BoxLayout的層次結構中間的面板不應該重寫大小計算。 – camickr