2013-05-28 101 views
1

這是此問題的擴展:Java gridbaglayout problems。由於panel1的寬度溢出,Panel1與panel2重疊。但是,如果我刪除了gc.gridwidth = 2,它將正確對齊panel2,但它也會將組合框移到其右側的原始位置。我一直在玩弄不同的gridbaglayout屬性,但不能像我想要的那樣對齊它。Java gridbaglayout調整

繼承人我的代碼:

public void createGUI() 
{ 
    main_panel = new JPanel(); 
    main_panel.setLayout(new GridBagLayout()); 
    GridBagConstraints gc = new GridBagConstraints(); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 0; 
    gc.gridy = 0; 
    gc.insets = new Insets(5, 0, 10, 0); 
    main_panel.add(label, gc); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 1; 
    gc.gridy = 0; 
    gc.insets = new Insets(5, 0, 10, 0); 
    main_panel.add(band_combobox, gc); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 0; 
    gc.gridy = 1; 
    gc.gridwidth = 2; 
    gc.insets = new Insets(0, 0, 10, 0); 
    main_panel.add(panel1, gc); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 1; 
    gc.gridy = 1; 
    gc.gridwidth = 2; 
    gc.insets = new Insets(0, 0, 10, 0); 
    main_panel.add(panel2, gc); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 0; 
    gc.gridy = 2; 
    gc.gridwidth = 2; 
    gc.insets = new Insets(0, 0, 10, 0); 
    main_panel.add(panel3, gc); 
} 

enter image description here

我試圖做這樣的事情:

enter image description here

+0

GBC是基於第1行創建 – mKorbel

+0

把標籤和組合框與流佈局一個單獨的面板和cooordinates放置在頂部。 – Marco

+0

@mKorbel謝謝。我將panel2的gridx設置爲3,並將panel2正確對齊,但標籤和combox沒有居中。 – user1352609

回答

2

不要害怕使用複合佈局來實現您的需求。您的標籤和組合框想要「浮動」在最上面一排。這將是一個難以實現它自己的一個GridBagLayout的事情,但通過將它們添加到另一個容器中,用不同的佈局管理器,它是可以實現的......

enter image description here

當您更改組件gridWidthgridHeight這將讓他們「流動」到其他行和列,這意味着他們可以坐在上面或下面是佔用空間,這取決於加入時,他們對組件...

此外,檢查出的weight性質。

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.TitledBorder; 

public class TestPane { 
    private JPanel main_panel; 

    public static void main(String[] args) { 
     new TestPane(); 
    } 

    public TestPane() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       createGUI();; 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(main_panel); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public void createGUI() { 

     main_panel = new JPanel(); 
     main_panel.setLayout(new GridBagLayout()); 
     GridBagConstraints gc = new GridBagConstraints(); 

     JLabel label = new JLabel("Band Directory"); 
     JComboBox band_combobox = new JComboBox(); 

     JPanel panel1 = new JPanel(); 
     panel1.add(new JLabel("1")); 
     panel1.setBorder(new TitledBorder("Panel1")); 
     JPanel panel2 = new JPanel(); 
     panel2.setBorder(new TitledBorder("Panel2")); 
     panel2.add(new JLabel("2")); 
     JPanel panel3 = new JPanel(); 
     panel3.setBorder(new TitledBorder("Panel3")); 
     panel3.add(new JLabel("3")); 

     JPanel top = new JPanel(); 
     top.add(label); 
     top.add(band_combobox); 

     gc.fill = GridBagConstraints.HORIZONTAL; 
     gc.weightx = 1; 
     gc.gridx = 0; 
     gc.gridy = 0; 
     gc.gridwidth = GridBagConstraints.REMAINDER; 
     gc.insets = new Insets(5, 0, 10, 0); 
     main_panel.add(top, gc); 

     gc.gridwidth = 1; 
     gc.weightx = 0.5; 
     gc.gridx = 0; 
     gc.gridy = 1; 
     gc.insets = new Insets(0, 0, 10, 0); 
     main_panel.add(panel1, gc); 

     gc.gridx = 1; 
     gc.gridy = 1; 
     gc.insets = new Insets(0, 0, 10, 0); 
     main_panel.add(panel2, gc); 

     gc.fill = GridBagConstraints.HORIZONTAL; 
     gc.weightx = 1; 
     gc.gridx = 0; 
     gc.gridy = 2; 
     gc.gridwidth = GridBagConstraints.REMAINDER; 
     gc.insets = new Insets(0, 0, 10, 0); 
     main_panel.add(panel3, gc); 
    } 
} 

你可能會發現它有用通過How to use GridBagLayout

2

試試這個:

public void createGUI() 
{ 
    main_panel = new JPanel(); 
    main_panel.setLayout(new GridBagLayout()); 
    GridBagConstraints gc = new GridBagConstraints(); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 0; 
    gc.gridy = 0; 
    gc.insets = new Insets(5, 0, 10, 0); 
    main_panel.add(label, gc); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 1; 
    gc.gridy = 0; 
    gc.insets = new Insets(5, 0, 10, 0); 
    main_panel.add(band_combobox, gc); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 0; 
    gc.gridy = 1; 
    gc.gridwidth = 1; 
gc.weightx = 1; 
gc.weighty = 1; 
    gc.insets = new Insets(0, 0, 10, 0); 
    main_panel.add(panel1, gc); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 1; 
    gc.gridy = 1; 
    gc.gridwidth = 1; 
gc.weightx = 1; 
gc.weighty = 1; 
    gc.insets = new Insets(0, 0, 10, 0); 
    main_panel.add(panel2, gc); 

    gc.fill = GridBagConstraints.BOTH; 
    gc.gridx = 0; 
    gc.gridy = 2; 
gc.weightx = 1; 
gc.weighty = 1; 
    gc.gridwidth = 2; 
    gc.insets = new Insets(0, 0, 10, 0); 
    main_panel.add(panel3, gc); 
} 

你沒有設定重量,而你的寬度設置有點不妥。

也把樂隊的標籤和組合在一個JPanel,並將其寬度設置爲兩個,並將其添加到頂部。這看起來好多了。

另請參閱我的GridBagLayout教程(在我的博客中,在我的配置文件中)獲取更多幫助。

2

樂隊目錄標籤,組合框,會員小組和CD列表面板應分配gridwidth = 1具有讀取和添加CD面板gridwith = 2 。

public JPanel getComponentPanel() 
{ 
    main_panel = new JPanel(); 
    main_panel.setLayout(new GridBagLayout()); 
    GridBagConstraints gc = new GridBagConstraints(); 

    gc.gridx = 0; 
    gc.gridy = 0; 
    gc.insets = new Insets(5, 0, 10, 0); 
    main_panel.add(label, gc); 

    gc.gridx = 1; 
    gc.gridy = 0; 
    gc.insets = new Insets(5, 10, 10, 0); 
    main_panel.add(band_combobox, gc); 

    gc.gridx = 0; 
    gc.gridy = 1; 
    gc.insets = new Insets(0, 10, 10, 0); 
    panel1.setBorder(
      BorderFactory.createLineBorder(Color.BLACK)); 
    panel1.setMinimumSize(new Dimension(100, 50)); 
    panel1.setPreferredSize(new Dimension(100, 50)); 
    panel1.setMaximumSize(new Dimension(100, 50)); 
    main_panel.add(panel1, gc); 

    gc.gridx = 1; 
    gc.gridy = 1; 
    gc.insets = new Insets(0, 10, 10, 0); 
    panel2.setBorder(
      BorderFactory.createLineBorder(Color.BLACK)); 
    panel2.setMinimumSize(new Dimension(100, 50)); 
    panel2.setPreferredSize(new Dimension(100, 50)); 
    panel2.setMaximumSize(new Dimension(100, 50)); 
    main_panel.add(panel2, gc); 

    gc.gridx = 0; 
    gc.gridy = 2; 
    gc.gridwidth = 2; 
    gc.insets = new Insets(0, 10, 10, 0); 
    panel3.setBorder(
      BorderFactory.createLineBorder(Color.BLACK)); 
    panel3.setMinimumSize(new Dimension(100, 50)); 
    panel3.setPreferredSize(new Dimension(100, 50)); 
    panel3.setMaximumSize(new Dimension(100, 50)); 
    main_panel.add(panel3, gc); 

    return main_panel; 
} 
1
  • GBC來創建列需要座標在第一行,則創建矩陣爲整個容器,緩和的坡度是必要的調整大小無閃爍

  • 肯定的是可能幾乎創造這個GBC矩陣而不是將無形的JComponents(JLabel在這種情況下添加邊框)添加到合成器中,也許爲什麼要打擾,

  • 這是關於使用GBC的AbsoluteLayout,另一種方法是使用隱形JC omponents

enter image description here

import java.awt.Color; 
import java.awt.ComponentOrientation; 
import java.awt.Font; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingConstants; 
import javax.swing.SwingUtilities; 

public class FrameAndGBC { 

    private JLabel hidelLabel; 
    private JLabel firstLabel; 
    private JTextField firstText; 
    private JFrame frame = new JFrame("Frame And GBC"); 

    public FrameAndGBC() { 
     JPanel panel = copyTextNorthPanel(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(panel); 
     frame.pack(); 
     frame.setLocation(150, 150); 
     frame.setVisible(true); 
    } 

    private JPanel copyTextNorthPanel() { 
     JPanel panel = new JPanel(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
     for (int k = 0; k < 50; k++) { 
      hidelLabel = new JLabel("  "); 
      hidelLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.weightx = 0.5; 
      gbc.weighty = 0.5; 
      gbc.gridx = k; 
      gbc.gridy = 0; 
      panel.add(hidelLabel, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT); 
      firstLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 0; 
      gbc.gridwidth = 8; 
      gbc.gridy = k + 1; 
      panel.add(firstLabel, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstText = new JTextField("Testing TextField"); 
      firstText.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 9; 
      gbc.gridwidth = k + 8; 
      gbc.gridy = k + 1; 
      panel.add(firstText, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT); 
      firstLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 20 + k; 
      gbc.gridwidth = 8; 
      gbc.gridy = k + 1; 
      panel.add(firstLabel, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstText = new JTextField("Testing TextField"); 
      firstText.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 29 + k; 
      gbc.gridwidth = 21 - k; 
      gbc.gridy = k + 1; 
      panel.add(firstText, gbc); 
     } 
     return panel; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       FrameAndGBC fs = new FrameAndGBC(); 
      } 
     }); 
    } 
}