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磅,以表示可能的寬度)似乎是右對齊的。我如何使它們左對齊,然後如何調整第一列和第二列之間的空間?
完美。非常感謝你! – user3113659