由于固定大小的 JPanel,GridBagLayout 忽略了重量分布
GridBagLayout ignoring weight distribution due to fixed-size JPanel
我的 UI 分为两个面板,我们称它们为 NormalPanel 和 SpecialPanel。 NormalPanel 应该占水平 space 的 25%,SpecialPanel 占其他 75%。为此,我使用了
gridBagLayout.columnWeights = new double[] { 1, 3 };
结果:
我遇到的问题 运行 是由 specialPanel 的属性引起的。它是一个包含固定大小面板的 JScrollPane。这意味着只要 JFrame 变得足够大以显示整个固定大小的面板,它就会忽略权重分布,只显示整个面板。
问题:
我怎样才能阻止这种情况发生?
我用来创建示例的代码:
public MyWindow() {
GridBagLayout gbLayout = new GridBagLayout();
// This makes the first row take 100% of the space (We only have one row)
gbLayout.rowWeights = new double[] { 1 };
// This makes the second column take up 3 times as much space as the first
gbLayout.columnWeights = new double[] { 1, 3 };
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = gbc.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
// Initialize the panels
normalPanel = new JPanel();
normalPanel.setBackground(Color.red);
specialPanel = new SpecialPanel();
contentPanel = new JPanel(gbLayout);
gbc.gridx = 0;
gbc.gridy = 0;
contentPanel.add(normalPanel, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
contentPanel.add(specialPanel, gbc);
setContentPane(contentPanel);
setSize(1280, 720);
setMinimumSize(new Dimension(1280, 720));
setVisible(true);
}
public static void main(String[] args) {
MyWindow window = new MyWindow();
}
/**
*
* This panel has a ScrollPan which is displaying a fixed sized panel.
*
*/
public class SpecialPanel extends JPanel {
private JScrollPane scrollPane;
private JPanel scrollPaneView;
private JPanel fixedSizePanel;
public SpecialPanel() {
fixedSizePanel = new JPanel();
fixedSizePanel.setPreferredSize(new Dimension(1280,720));
fixedSizePanel.setMaximumSize(new Dimension(1280,720));
fixedSizePanel.setMinimumSize(new Dimension(1280,720));
fixedSizePanel.setBackground(Color.blue);
scrollPaneView = new JPanel(new FlowLayout());
scrollPaneView.add(fixedSizePanel);
scrollPane = new JScrollPane(scrollPaneView);
scrollPane.setHorizontalScrollBarPolicy(scrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(scrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
this.setLayout(new GridLayout(1,1));
this.add(scrollPane);
}
}
}
GridBagLayout 是...违反直觉的 它的分布方式space。它首先 "asks" 组件它们想要的大小,然后给它们 space,然后才根据权重分配剩余的 space。
要解决此问题,请扩展您要添加到 GridBagLayout 的面板并像这样覆盖它们的 getPrefferedSize()
方法。
@Override
public Dimension getPreferredSize() {
return new Dimension();
}
现在面板将 "tell" 他们不想要任何 space 的布局,因此布局将根据权重分配所有 space。
我的 UI 分为两个面板,我们称它们为 NormalPanel 和 SpecialPanel。 NormalPanel 应该占水平 space 的 25%,SpecialPanel 占其他 75%。为此,我使用了
gridBagLayout.columnWeights = new double[] { 1, 3 };
结果:
我遇到的问题 运行 是由 specialPanel 的属性引起的。它是一个包含固定大小面板的 JScrollPane。这意味着只要 JFrame 变得足够大以显示整个固定大小的面板,它就会忽略权重分布,只显示整个面板。
问题:
我怎样才能阻止这种情况发生?
我用来创建示例的代码:
public MyWindow() {
GridBagLayout gbLayout = new GridBagLayout();
// This makes the first row take 100% of the space (We only have one row)
gbLayout.rowWeights = new double[] { 1 };
// This makes the second column take up 3 times as much space as the first
gbLayout.columnWeights = new double[] { 1, 3 };
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = gbc.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
// Initialize the panels
normalPanel = new JPanel();
normalPanel.setBackground(Color.red);
specialPanel = new SpecialPanel();
contentPanel = new JPanel(gbLayout);
gbc.gridx = 0;
gbc.gridy = 0;
contentPanel.add(normalPanel, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
contentPanel.add(specialPanel, gbc);
setContentPane(contentPanel);
setSize(1280, 720);
setMinimumSize(new Dimension(1280, 720));
setVisible(true);
}
public static void main(String[] args) {
MyWindow window = new MyWindow();
}
/**
*
* This panel has a ScrollPan which is displaying a fixed sized panel.
*
*/
public class SpecialPanel extends JPanel {
private JScrollPane scrollPane;
private JPanel scrollPaneView;
private JPanel fixedSizePanel;
public SpecialPanel() {
fixedSizePanel = new JPanel();
fixedSizePanel.setPreferredSize(new Dimension(1280,720));
fixedSizePanel.setMaximumSize(new Dimension(1280,720));
fixedSizePanel.setMinimumSize(new Dimension(1280,720));
fixedSizePanel.setBackground(Color.blue);
scrollPaneView = new JPanel(new FlowLayout());
scrollPaneView.add(fixedSizePanel);
scrollPane = new JScrollPane(scrollPaneView);
scrollPane.setHorizontalScrollBarPolicy(scrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(scrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
this.setLayout(new GridLayout(1,1));
this.add(scrollPane);
}
}
}
GridBagLayout 是...违反直觉的 它的分布方式space。它首先 "asks" 组件它们想要的大小,然后给它们 space,然后才根据权重分配剩余的 space。
要解决此问题,请扩展您要添加到 GridBagLayout 的面板并像这样覆盖它们的 getPrefferedSize()
方法。
@Override
public Dimension getPreferredSize() {
return new Dimension();
}
现在面板将 "tell" 他们不想要任何 space 的布局,因此布局将根据权重分配所有 space。