Java如何禁用JPanel组件自动垂直调整大小
Java how to disable JPanel components automatic vertical resizing
我 运行 遇到了一个非常烦人的问题。 JPanel 在组件之间添加了垂直间隙,我需要摆脱它。
我正在尝试得到这个(蓝线是我想要摆脱的 space):
看起来像这样:
这是我现在的 class:
public class SummaryPanel extends JPanel
{
private JLabel bagelLabel;
private JLabel toppingLabel;
private JLabel coffeeLabel;
private JLabel shotsLabel;
private JLabel subtotal;
private JLabel tax;
private JLabel total;
private JPanel selectionsPanel;
private JPanel totalPanel;
public SummaryPanel()
{
bagelLabel = new JLabel("No bagel [=11=].00");
toppingLabel = new JLabel("No topping [=11=].00");
coffeeLabel = new JLabel("No coffee [=11=].00");
shotsLabel = new JLabel("(Includes 0 shots) [=11=].00");
subtotal = new JLabel("");
tax = new JLabel("");
total = new JLabel("");
setLayout(new GridLayout(2,1));
selectionsPanel = new JPanel();
selectionsPanel.setLayout(new GridLayout(4,1));
selectionsPanel.add(bagelLabel);
selectionsPanel.add(toppingLabel);
selectionsPanel.add(coffeeLabel );
selectionsPanel.add(shotsLabel );
totalPanel = new JPanel();
totalPanel.setLayout(new GridLayout(3,1));
totalPanel.add(subtotal);
totalPanel.add(tax);
totalPanel.add(total);
totalPanel.setVisible(false);
add(selectionsPanel);
add(totalPanel);
}
}
GridLayout
会将面板划分为指定的行数和列数,每个组件将完整填充其中一个单元格。
您不妨考虑改用 BoxLayout
。这将允许您堆叠您的组件而不会令人不快地膨胀。
它由布局管理器控制。
setLayout(new GridLayout(2,1));
您使用的是 GridLayout,因此两个组件中的每一个都相同 space。
selectionsPanel.setLayout(new GridLayout(4,1));
每个 JLabel 依次获得每个面板可用的总数 space 的四分之一。
您可以使用 BorderLayout:
//setLayout(new GridLayout(2,1));
setLayout(new BorderLayout);
然后当您将组件添加到您使用的面板时:
add(selectionsPanel, BorderLayout.PAGE_START);
add(totalsPanel, BorderLayout.PAGE_END);
现在将考虑首选尺寸。
我 运行 遇到了一个非常烦人的问题。 JPanel 在组件之间添加了垂直间隙,我需要摆脱它。 我正在尝试得到这个(蓝线是我想要摆脱的 space):
看起来像这样:
这是我现在的 class:
public class SummaryPanel extends JPanel
{
private JLabel bagelLabel;
private JLabel toppingLabel;
private JLabel coffeeLabel;
private JLabel shotsLabel;
private JLabel subtotal;
private JLabel tax;
private JLabel total;
private JPanel selectionsPanel;
private JPanel totalPanel;
public SummaryPanel()
{
bagelLabel = new JLabel("No bagel [=11=].00");
toppingLabel = new JLabel("No topping [=11=].00");
coffeeLabel = new JLabel("No coffee [=11=].00");
shotsLabel = new JLabel("(Includes 0 shots) [=11=].00");
subtotal = new JLabel("");
tax = new JLabel("");
total = new JLabel("");
setLayout(new GridLayout(2,1));
selectionsPanel = new JPanel();
selectionsPanel.setLayout(new GridLayout(4,1));
selectionsPanel.add(bagelLabel);
selectionsPanel.add(toppingLabel);
selectionsPanel.add(coffeeLabel );
selectionsPanel.add(shotsLabel );
totalPanel = new JPanel();
totalPanel.setLayout(new GridLayout(3,1));
totalPanel.add(subtotal);
totalPanel.add(tax);
totalPanel.add(total);
totalPanel.setVisible(false);
add(selectionsPanel);
add(totalPanel);
}
}
GridLayout
会将面板划分为指定的行数和列数,每个组件将完整填充其中一个单元格。
您不妨考虑改用 BoxLayout
。这将允许您堆叠您的组件而不会令人不快地膨胀。
它由布局管理器控制。
setLayout(new GridLayout(2,1));
您使用的是 GridLayout,因此两个组件中的每一个都相同 space。
selectionsPanel.setLayout(new GridLayout(4,1));
每个 JLabel 依次获得每个面板可用的总数 space 的四分之一。
您可以使用 BorderLayout:
//setLayout(new GridLayout(2,1));
setLayout(new BorderLayout);
然后当您将组件添加到您使用的面板时:
add(selectionsPanel, BorderLayout.PAGE_START);
add(totalsPanel, BorderLayout.PAGE_END);
现在将考虑首选尺寸。