我正在尝试使用GridBagLayout
内的JPanel
,JLayeredPane
;我有两列,我想增加第二列的界限,以防止剪辑。
这是我的代码:
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磅,以便了解可能的宽度)似乎是正确对齐的。如何使它们左对齐,然后如何调整第一列和第二列之间的空间?
答案 0 :(得分: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.setHorizontalAlignment(JLabel.LEFT);
此...
leftGrid.ipadx = 50
将列之间的空间增加另外25个像素(最多45个像素,包括插图)
注意约束,它们会产生复合效应。
我能够制作......
使用...
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);
}
}
}