2013-12-18 74 views
0

我試圖與GridBagLayout一起工作JPanel,在JLayeredPane;我有兩欄,我想增加第二欄的邊界以防止裁剪。Aligning GridBagLayout單元格

這裏是我的代碼:

JLayeredPane lPane = new JLayeredPane(); 
    lPane.setBounds(0, 0, 1200, 700); 
    frame.add(lPane, BorderLayout.CENTER); 

    JPanel left = new JPanel(new GridBagLayout()); 
    left.setBounds(0, 0, 175, 700); 
    left.setBackground(Color.BLACK); 

    GridBagConstraints leftGrid = new GridBagConstraints(); 
    JLabel label; 

    label = new JLabel("Player's Goods:"); 
    leftGrid.fill = GridBagConstraints.NONE; 
    leftGrid.anchor = GridBagConstraints.NORTHWEST; 
    leftGrid.insets = new Insets(10,10,10,10); 
    leftGrid.weighty = 0.5; 
    leftGrid.gridx = 0; 
    leftGrid.gridy = 0; 
    left.add(label, leftGrid); 

    label = new JLabel(""); 
    leftGrid.weighty = 0; 
    leftGrid.weightx = 0; 
    leftGrid.gridx = 0; 
    leftGrid.gridy = 1; 
    left.add(label, leftGrid); 

    label = new JLabel("Nails:"); 
    leftGrid.ipady = 50; 
    leftGrid.gridx = 0; 
    leftGrid.gridy = 2; 
    left.add(label, leftGrid); 

    label = new JLabel("Wheat:"); 
    leftGrid.gridx = 0; 
    leftGrid.gridy = 3; 
    left.add(label, leftGrid); 

    label = new JLabel("Armor:"); 
    leftGrid.gridx = 0; 
    leftGrid.gridy = 4; 
    left.add(label, leftGrid); 

    label = new JLabel("Gold:"); 
    leftGrid.gridx = 0; 
    leftGrid.gridy = 5; 
    left.add(label, leftGrid); 

    label = new JLabel("Weapons:"); 
    leftGrid.gridx = 0; 
    leftGrid.gridy = 6; 
    left.add(label, leftGrid); 

    label = new JLabel("Spice:"); 
    leftGrid.gridx = 0; 
    leftGrid.gridy = 7; 
    left.add(label, leftGrid); 

    label = new JLabel("0 lbs."); 
    label.setHorizontalTextPosition(JLabel.LINE_START); 
    leftGrid.fill = GridBagConstraints.HORIZONTAL; 
    leftGrid.anchor = GridBagConstraints.WEST; 
    leftGrid.ipady = 0; 
    leftGrid.ipadx = 50; 
    leftGrid.weightx = 1; 
    leftGrid.gridx = 1; 
    leftGrid.gridy = 2; 
    left.add(label, leftGrid); 

    label = new JLabel("0 lbs."); 
    leftGrid.gridx = 1; 
    leftGrid.gridy = 3; 
    left.add(label, leftGrid); 

    label = new JLabel("0 lbs."); 
    leftGrid.gridx = 1; 
    leftGrid.gridy = 4; 
    left.add(label, leftGrid); 

    label = new JLabel("0 lbs."); 
    leftGrid.gridx = 1; 
    leftGrid.gridy = 5; 
    left.add(label, leftGrid); 

    label = new JLabel("9999 lbs."); 
    leftGrid.gridx = 1; 
    leftGrid.gridy = 6; 
    left.add(label, leftGrid); 

    label = new JLabel("0 lbs."); 
    leftGrid.gridx = 1; 
    leftGrid.gridy = 7; 
    left.add(label, leftGrid); 

    label = new JLabel("666 Coins"); 
    leftGrid.ipady = 50; 
    leftGrid.weighty = 1; 
    leftGrid.gridx = 1; 
    leftGrid.gridy = 8; 
    left.add(label, leftGrid); 

    lPane.add(left, new Integer(0), 0); 

指定: 「0磅」 幾乎所有的JLabels(增加了999磅,以表示可能的寬度)似乎是右對齊的。我如何使它們左對齊,然後如何調整第一列和第二列之間的空間?

回答

1

有東西在作怪組合...

label = new JLabel("Player's Goods:"); 
leftGrid.fill = GridBagConstraints.NONE; 
leftGrid.anchor = GridBagConstraints.NORTHWEST; 
leftGrid.insets = new Insets(10, 10, 10, 10); 
leftGrid.weighty = 0.5; 
leftGrid.gridx = 0; 
leftGrid.gridy = 0; 
add(label, leftGrid); 

會影響到左邊的列的整體寬度,它可能是更好的包括

leftGrid.gridwidth = GridBagConstraints.REMAINDER; 

這將使它會擴展剩餘的列,只要確保在重新使用之前將其重置爲leftGrid.gridwidth = 1;即可。

leftGrid.insets = new Insets(10, 10, 10, 10); 

將要添加的左,右立柱之間20個像素...

這...

label.setHorizontalTextPosition(JLabel.LINE_START); 

影響文本相對的對於圖標的位置,而不是它的(JLabel.LINE_START實際上並不存在)

取而代之,我認爲你想...

label.setHorizo​​ntalAlignment(JLabel.LEFT);

這...

leftGrid.ipadx = 50 

將再添25個像素(最多45包括插圖)

小心的約束,他們可以有複合效應增加柱之間的空間。

我是能夠產生......

Layout

使用...

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

public class TestGridBagLayout100 { 

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

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

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

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
//   setBackground(Color.BLACK); 

      GridBagConstraints leftGrid = new GridBagConstraints(); 
      JLabel label; 

      label = new JLabel("Player's Goods:"); 
      leftGrid.fill = GridBagConstraints.NONE; 
      leftGrid.anchor = GridBagConstraints.NORTHWEST; 
//   leftGrid.insets = new Insets(10, 10, 10, 10); 
      leftGrid.weighty = 0.5; 
      leftGrid.gridx = 0; 
      leftGrid.gridy = 0; 
      leftGrid.gridwidth = GridBagConstraints.REMAINDER; 
      add(label, leftGrid); 

      //?? 
      leftGrid.gridwidth = 1; 
      label = new JLabel(""); 
      leftGrid.weighty = 0; 
      leftGrid.weightx = 0; 
      leftGrid.gridx = 0; 
      leftGrid.gridy = 1; 
      add(label, leftGrid); 

      label = new JLabel("Nails:"); 
      leftGrid.gridy = 0; 
      leftGrid.ipady = 50; 
      leftGrid.gridx = 0; 
      leftGrid.gridy = 2; 
      add(label, leftGrid); 

      label = new JLabel("Wheat:"); 
      leftGrid.gridx = 0; 
      leftGrid.gridy = 3; 
      add(label, leftGrid); 

      label = new JLabel("Armor:"); 
      leftGrid.gridx = 0; 
      leftGrid.gridy = 4; 
      add(label, leftGrid); 

      label = new JLabel("Gold:"); 
      leftGrid.gridx = 0; 
      leftGrid.gridy = 5; 
      add(label, leftGrid); 

      label = new JLabel("Weapons:"); 
      leftGrid.gridx = 0; 
      leftGrid.gridy = 6; 
      add(label, leftGrid); 

      label = new JLabel("Spice:"); 
      leftGrid.gridx = 0; 
      leftGrid.gridy = 7; 
      add(label, leftGrid); 

      label = new JLabel("0 lbs."); 
      label.setBorder(new LineBorder(Color.RED)); 
      label.setHorizontalAlignment(JLabel.LEFT); 
      leftGrid.fill = GridBagConstraints.HORIZONTAL; 
      leftGrid.anchor = GridBagConstraints.WEST; 
      leftGrid.ipady = 0; 
//   leftGrid.ipadx = 50; 
      leftGrid.weightx = 1; 
      leftGrid.gridx = 1; 
      leftGrid.gridy = 2; 
      add(label, leftGrid); 

      label = new JLabel("0 lbs."); 
      leftGrid.gridx = 1; 
      leftGrid.gridy = 3; 
      add(label, leftGrid); 

      label = new JLabel("0 lbs."); 
      leftGrid.gridx = 1; 
      leftGrid.gridy = 4; 
      add(label, leftGrid); 

      label = new JLabel("0 lbs."); 
      leftGrid.gridx = 1; 
      leftGrid.gridy = 5; 
      add(label, leftGrid); 

      label = new JLabel("9999 lbs."); 
      leftGrid.gridx = 1; 
      leftGrid.gridy = 6; 
      add(label, leftGrid); 

      label = new JLabel("0 lbs."); 
      leftGrid.gridx = 1; 
      leftGrid.gridy = 7; 
      add(label, leftGrid); 

      label = new JLabel("666 Coins"); 
//   leftGrid.ipady = 50; 
      leftGrid.weighty = 1; 
      leftGrid.gridx = 1; 
      leftGrid.gridy = 8; 
      add(label, leftGrid); 
     } 
    } 

} 
+0

完美。非常感謝你! – user3113659