2013-01-21 49 views
0

我在獲取頂級JPanel(實際上是最頂級父級JPanel)的高度時遇到一些問題,無法調整其子級JPanel的高度。因此,當我將頂層JPanel放置到網格佈局中時,在父JPanel佔用的子JPanel下面存在額外間隙。我想要做的是消除這種差距,使得頂級JPanel的大小與子級JPanel的大小相同。強制頂部JPanel調整到其子級高度JPanel

基本上,父JPanel是一個擴展JPanel的類。我嘗試使用子JPanel的大小將其調整爲子JPanel的大小,但結果仍然相同。

this.setSize(childJPanel.getSize()); 

爲了澄清,我已經使用了馬蒂斯可視化編輯器編輯這上面的JPanel(設置爲GroupLayout的),而我把這個頂部的JPanel與一個GridLayout的另一個JPanel的。

如何強制頂部JPanel調整到其子JPanel的高度?

CODE:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package helloworld; 

/** 
* 
* @author Justin 
*/ 
public class FooPanel extends javax.swing.JPanel { 

    /** 
    * Creates new form NewJPanel 
    */ 
    public FooPanel() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jPanel1 = new javax.swing.JPanel(); 
     jLabel2 = new javax.swing.JLabel(); 

     jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); 

     jLabel2.setText("HELLOWORLD"); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jLabel2) 
       .addContainerGap(122, Short.MAX_VALUE)) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jLabel2) 
       .addContainerGap(45, Short.MAX_VALUE)) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
     ); 
    }// </editor-fold> 
    // Variables declaration - do not modify 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JPanel jPanel1; 
    // End of variables declaration 
} 

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package helloworld; 

import java.awt.GridLayout; 
import javax.swing.border.EmptyBorder; 

/** 
* 
* @author Justin 
*/ 
public class NewJApplet extends javax.swing.JApplet { 

    /** 
    * Initializes the applet NewJApplet 
    */ 
    @Override 
    public void init() { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the applet */ 
     try { 
      java.awt.EventQueue.invokeAndWait(new Runnable() { 
       public void run() { 
        initComponents(); 
        FooPanel panel1 = new FooPanel(); 
        FooPanel panel2 = new FooPanel(); 
        FooPanel panel3 = new FooPanel(); 
        myPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 
        myPanel.setLayout(new GridLayout(0,1)); 
        myPanel.add(panel1); 
        myPanel.add(panel2); 
        myPanel.add(panel3); 

       } 
      }); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    /** 
    * This method is called from within the init() method to initialize the 
    * form. WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     myPanel = new javax.swing.JPanel(); 

     javax.swing.GroupLayout myPanelLayout = new javax.swing.GroupLayout(myPanel); 
     myPanel.setLayout(myPanelLayout); 
     myPanelLayout.setHorizontalGroup(
      myPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 200, Short.MAX_VALUE) 
     ); 
     myPanelLayout.setVerticalGroup(
      myPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(myPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(0, 200, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(myPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 
    }// </editor-fold> 
    // Variables declaration - do not modify 
    private javax.swing.JPanel myPanel; 
    // End of variables declaration 
} 

結果:

enter image description here

+0

嘗試設置首選大小而不是大小。 –

+1

@Vlad no:永遠不會調用'setPreferredSize()'。 Justin,你不應該調用'setSize()/ setPreferredSize()/ setMinimumSize()/ setMaximumSize()'。讓所有這些都由LayoutManager處理,這正是他們的意圖。 'GridLayout'是那裏最蹩腳的'LayoutManager'之一。 'BorderLayout'和'GridBagLayout'更多地被使用。在一些罕見的情況下,你可能會考慮'FlowLayout'。 'GroupLayout'是GUI構建器的佈局,並且產生難以讀取且難以調試的代碼。 –

+0

@Vlad:我嘗試設置首選大小。父JPanel和子JPanel之間的差距仍然存在。這似乎是父JPanel想要將其高度調整爲由gridlayout分配的大小的結果,儘管我不希望發生這種情況。 – Justin

回答

2

用如上述紀堯姆波萊提到的,事實證明,GridLayout「將在其所有的孩子平等的大小(根據可用空間以及所有孩子的最大規模)「。使用BoxLayout管理器(特別是PAGE_LAYOUT)將緩解上述問題。具體而言,BoxLayout不會將其所有子級設置爲相同大小。我發現這個頁面對於計算出How to Use BoxLayout非常有用。

使用BoxLayout時,可以使用Glue和RigidArea來間隔組件。