2010-09-28 80 views
0

自從我開始與Swing合作以來,我還沒有理解這一點。爲什麼BoxLayout使用Y軸排序(從示例中)將按鈕放置在另一個空間中,一直到面板的末尾(這就是我認爲它的工作原理),但將JPanel放置在相同的位置,但使它們伸展。有人會如此善意向我解釋這種現象嗎? (是的示例代碼生成,但是這所以我不寫它即使是現在:-))java boxlayout組合


public class MyFrame extends JFrame { 

    private JButton jButton1; 
    private JButton jButton2; 
    private JLabel jLabel1; 
    private JLabel jLabel10; 
    private JLabel jLabel11; 
    private JLabel jLabel12; 
    private JLabel jLabel13; 
    private JLabel jLabel14; 
    private JLabel jLabel15; 
    private JLabel jLabel16; 
    private JLabel jLabel17; 
    private JLabel jLabel18; 
    private JLabel jLabel19; 
    private JLabel jLabel2; 
    private JLabel jLabel20; 
    private JLabel jLabel21; 
    private JLabel jLabel22; 
    private JLabel jLabel23; 
    private JLabel jLabel24; 
    private JLabel jLabel25; 
    private JLabel jLabel26; 
    private JLabel jLabel27; 
    private JLabel jLabel3; 
    private JLabel jLabel4; 
    private JLabel jLabel5; 
    private JLabel jLabel6; 
    private JLabel jLabel7; 
    private JLabel jLabel8; 
    private JLabel jLabel9; 
    private JPanel jPanel1; 
    private JPanel jPanel2; 
    private JPanel jPanel3; 
    private JPanel jPanel4; 
    private JScrollPane jScrollPane1; 
    private JScrollPane jScrollPane2; 

    public MyFrame() { 
     buildGUI(); 
     setVisible(true); 
    } 

    private void buildGUI() { 

     jScrollPane1 = new JScrollPane(); 
     jPanel4 = new JPanel(); 
     jPanel2 = new JPanel(); 
     jLabel1 = new JLabel(); 
     jLabel2 = new JLabel(); 
     jLabel3 = new JLabel(); 
     jLabel4 = new JLabel(); 
     jLabel5 = new JLabel(); 
     jLabel10 = new JLabel(); 
     jLabel9 = new JLabel(); 
     jLabel8 = new JLabel(); 
     jLabel7 = new JLabel(); 
     jLabel6 = new JLabel(); 
     jLabel27 = new JLabel(); 
     jPanel3 = new JPanel(); 
     jLabel11 = new JLabel(); 
     jLabel12 = new JLabel(); 
     jLabel13 = new JLabel(); 
     jLabel14 = new JLabel(); 
     jLabel15 = new JLabel(); 
     jLabel16 = new JLabel(); 
     jLabel17 = new JLabel(); 
     jLabel18 = new JLabel(); 
     jLabel19 = new JLabel(); 
     jLabel20 = new JLabel(); 
     jLabel21 = new JLabel(); 
     jLabel22 = new JLabel(); 
     jLabel26 = new JLabel(); 
     jLabel25 = new JLabel(); 
     jLabel24 = new JLabel(); 
     jLabel23 = new JLabel(); 
     jScrollPane2 = new JScrollPane(); 
     jPanel1 = new JPanel(); 
     jButton1 = new JButton(); 
     jButton2 = new JButton(); 

     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

     jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 

     jPanel4.setLayout(new BoxLayout(jPanel4, BoxLayout.Y_AXIS)); 

     jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT)); 

     jLabel1.setText("jLabel1"); 
     jPanel2.add(jLabel1); 

     jLabel2.setText("jLabel2"); 
     jPanel2.add(jLabel2); 

     jLabel3.setText("jLabel3"); 
     jPanel2.add(jLabel3); 

     jLabel4.setText("jLabel4"); 
     jPanel2.add(jLabel4); 

     jLabel5.setText("jLabel5"); 
     jPanel2.add(jLabel5); 

     jLabel10.setText("jLabel10"); 
     jPanel2.add(jLabel10); 

     jLabel9.setText("jLabel9"); 
     jPanel2.add(jLabel9); 

     jLabel8.setText("jLabel8"); 
     jPanel2.add(jLabel8); 

     jLabel7.setText("jLabel7"); 
     jPanel2.add(jLabel7); 

     jLabel6.setText("jLabel6"); 
     jPanel2.add(jLabel6); 

     jLabel27.setText("jLabel27"); 
     jPanel2.add(jLabel27); 

     jPanel4.add(jPanel2); 

     jLabel11.setText("jLabel11"); 
     jPanel3.add(jLabel11); 

     jLabel12.setText("jLabel12"); 
     jPanel3.add(jLabel12); 

     jLabel13.setText("jLabel13"); 
     jPanel3.add(jLabel13); 

     jLabel14.setText("jLabel14"); 
     jPanel3.add(jLabel14); 

     jLabel15.setText("jLabel15"); 
     jPanel3.add(jLabel15); 

     jLabel16.setText("jLabel16"); 
     jPanel3.add(jLabel16); 

     jLabel17.setText("jLabel17"); 
     jPanel3.add(jLabel17); 

     jLabel18.setText("jLabel18"); 
     jPanel3.add(jLabel18); 

     jLabel19.setText("jLabel19"); 
     jPanel3.add(jLabel19); 

     jLabel20.setText("jLabel20"); 
     jPanel3.add(jLabel20); 

     jLabel21.setText("jLabel21"); 
     jPanel3.add(jLabel21); 

     jLabel22.setText("jLabel22"); 
     jPanel3.add(jLabel22); 

     jLabel26.setText("jLabel26"); 
     jPanel3.add(jLabel26); 

     jLabel25.setText("jLabel25"); 
     jPanel3.add(jLabel25); 

     jLabel24.setText("jLabel24"); 
     jPanel3.add(jLabel24); 

     jLabel23.setText("jLabel23"); 
     jPanel3.add(jLabel23); 

     jPanel4.add(jPanel3); 

     jScrollPane1.setViewportView(jPanel4); 

     jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.Y_AXIS)); 

     jButton1.setText("jButton1"); 
     jPanel1.add(jButton1); 

     jButton2.setText("jButton2"); 
     jPanel1.add(jButton2); 

     jScrollPane2.setViewportView(jPanel1); 

     GroupLayout layout = GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(GroupLayout.LEADING) 
      .add(layout.createSequentialGroup() 
       .add(24, 24, 24) 
       .add(jScrollPane1, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE) 
       .add(37, 37, 37) 
       .add(jScrollPane2, GroupLayout.PREFERRED_SIZE, 244, GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(31, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(GroupLayout.LEADING) 
      .add(GroupLayout.TRAILING, layout.createSequentialGroup() 
       .add(21, 21, 21) 
       .add(layout.createParallelGroup(GroupLayout.TRAILING) 
        .add(GroupLayout.LEADING, jScrollPane2, GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE) 
        .add(GroupLayout.LEADING, jScrollPane1, GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)) 
       .add(40, 40, 40)) 
     ); 

     pack(); 
    } 

    public static void main(String args[]) { 
     new MyFrame(); 
    } 
} 
+0

對不起,我被引導瞭解到,你有一些輕微的修改,使問題類似,但略有改變。相反,你完全消除了你原來的問題,並用另一個問題取而代之。這使得難以回答問題和答案,因爲現在你有一個與當前問題無關的答案。我會回滾你的問題,我建議你將編輯作爲一個新問題發佈(你可以從編輯歷史中獲取內容;點擊上面我的名字上面的鏈接,去編輯並點擊「查看源代碼」 )。 – 2010-09-29 13:44:04

回答

3

一個JButton,具有相當於首選大小默認的最大尺寸(基於文本/圖標在按鈕中),這意味着它指示LayoutManager不要將其尺寸調整爲比正常尺寸更大。

JPanel的默認最大尺寸爲32767/32767,這意味着它幾乎可以佔用您提供的任何空間。

你可以採取與本SNIPPIT一看:

JPanel panel = new JPanel(); 
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 

    for (int i = 0; i < 5; i++) { 
     JComponent comp = new JPanel(); 
//  JComponent comp = new JButton("Button " + i); 
     System.out.printf("Pref: %s Max: %s%n", comp.getPreferredSize(), comp.getMaximumSize()); 
     comp.setBackground(new Color(100 + i * 30, 0, 0)); 
     panel.add(comp); 
    } 

的最大大小都可以明確使用setMaximumSize()進行設置。

+0

謝謝,現在我明白了,我不知道這裏是否合適,因爲這個boxlayout是我更復雜的問題的一部分。可以包括在這裏嗎? – Martin 2010-09-28 19:31:24

+0

@Martin:如果您的問題比我給出的解決方案更復雜,請隨時更新您的問題。但一定要將代碼放到能夠演示更復雜問題的最小代碼中,並且要具體說明問題。你發佈的代碼太長了,我不想看看它。 – 2010-09-28 19:41:34

+0

我遇到了另一個問題,我可以設置JPanel的最大尺寸,但事情是它總是將面板放大到它的最大尺寸,當它放在JPanel和BoxLayout裏面時,我不知道實際尺寸是多少因爲它取決於JPanel包含的縮放比例 – Martin 2010-09-30 16:49:55