GridBagLayout和最小宽度

时间:2013-07-25 11:53:39

标签: java swing gridbaglayout

我想制作具有约束最大宽度的面板,但是当容器变短时会减小其宽度。

我使用GridBagLayout但是当尺寸变得足够短时它表现得很奇怪。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @author Michael Nesterenko
 *
 */
public class SSCE extends JFrame {

    /**
     * 
     */
    public SSCE() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GridBagLayout gbl = new GridBagLayout();
        gbl.columnWidths = new int[] {200, 1};
        gbl.columnWeights = new double[] {0, 1};
        gbl.rowHeights = new int[] {10};
        gbl.rowWeights = new double[] {0};
        setLayout(gbl);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JPanel sizeRestrictedPanel = new JPanel();
        sizeRestrictedPanel.setBackground(Color.BLUE);
        sizeRestrictedPanel.setMinimumSize(new Dimension(50, 50));
        sizeRestrictedPanel.setMaximumSize(new Dimension(300, 50));
        sizeRestrictedPanel.setPreferredSize(new Dimension(300, 50));
        add(sizeRestrictedPanel, gbc);

        JPanel dummy = new JPanel();
        dummy.setBackground(Color.RED);
        add(dummy, gbc);

        setPreferredSize(new Dimension(600, 200));
        pack();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new SSCE().setVisible(true);
    }

}

当框架宽度变得足够短时,蓝色面板会立即调整大小,但是我希望它可以通过调整窗口大小来平滑调整大小。

2 个答案:

答案 0 :(得分:2)

您可以定义GridBagConstraints.ipadxGridBagConstraints.ipady约束来设置最小宽度/高度

/**
 * This field specifies the internal padding of the component, how much
 * space to add to the minimum width of the component. The width of
 * the component is at least its minimum width plus
 * <code>ipadx</code> pixels.
 * <p>
 * The default value is <code>0</code>.
 * @serial
 * @see #clone()
 * @see java.awt.GridBagConstraints#ipady
 */
public int ipadx;

/**
 * This field specifies the internal padding, that is, how much
 * space to add to the minimum height of the component. The height of
 * the component is at least its minimum height plus
 * <code>ipady</code> pixels.
 * <p>
 * The default value is 0.
 * @serial
 * @see #clone()
 * @see java.awt.GridBagConstraints#ipadx
 */
public int ipady;

答案 1 :(得分:2)

当用户减小窗口宽度时,只有红色面板首先收缩。

问题的代码示例实际上是从扩展窗口开始,超出了打包组件所给出的大小。如果我们删除行setPreferredSize(new Dimension(600, 200));,则会显示下面显示的窗口,而不是带有展开红色面板的窗口。如果我们将宽度减小到小于这个尺寸,那么我们强制蓝色面板小于其首选尺寸并且“它表现得很奇怪”。这是因为GridBagLayout不再能够尊重蓝色面板的首选尺寸,现在它为每个面板提供了相等的空间,并且只是尝试遵守它们的最小尺寸。

enter image description here

我建议不要将setPreferredSize与GridBagLayout一起使用。 您可能希望确定窗口仍然有用所需的最小尺寸,并且不允许用户将尺寸减小到此最小值以下。当用户展开窗口时,某些组件不应占用比最初分配的空间更多的空间。我们可以使用GridBagConstraints weightx和weighty而不是setMaximumSize来完成此任务。

下面的代码显示了两个面板平滑调整大小,我添加了一些文本来显示调整大小的尺寸。

import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.*;

/**
 * @author Michael Nesterenko, Leon LaSpina
 *
 */
public class SSCE extends JFrame {
   JLabel blueDimension, redDimension;
   JPanel sizeRestrictedPanel, dummyPanel, bottomPanel;

   public SSCE() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JPanel testPanel = new JPanel();
      bottomPanel = new JPanel();
      setLayout(new BorderLayout());
      GridBagLayout gbl = new GridBagLayout();
      testPanel.setLayout(gbl);
      add(testPanel, BorderLayout.CENTER);
      add(bottomPanel, BorderLayout.SOUTH);
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.weightx = 0.75;

      sizeRestrictedPanel = new JPanel();
      sizeRestrictedPanel.setBackground(Color.BLUE);
      sizeRestrictedPanel.setMinimumSize(new Dimension(50, 50));
      sizeRestrictedPanel.setMaximumSize(new Dimension(300, 50));
      //sizeRestrictedPanel.setPreferredSize(new Dimension(300, 50));
      testPanel.add(sizeRestrictedPanel, gbc);
      dummyPanel = new JPanel();
      dummyPanel.setBackground(Color.RED);
      dummyPanel.setMinimumSize(new Dimension(50, 50));
      //dummyPanel.setPreferredSize(new Dimension(50, 50));
      gbc.weightx = 0.25;
      testPanel.add(dummyPanel, gbc);

      setSize(new Dimension(600, 200));
      blueDimension = new JLabel("------");
      blueDimension.setForeground(Color.BLUE);
      redDimension = new JLabel("------");
      redDimension.setForeground(Color.RED);
      bottomPanel.add(blueDimension);
      bottomPanel.add(new JLabel("   "));
      bottomPanel.add(blueDimension);
      bottomPanel.add(redDimension);
      this.addComponentListener(new ComponentAdapter() {
         public void componentResized(ComponentEvent e) {
            updateDimensionText();
         }
      });
   }

   private void updateDimensionText() {
      Dimension d1 = sizeRestrictedPanel.getSize();
      String s1 = d1.width + "," + d1.height;
      Dimension d2 = dummyPanel.getSize();
      String s2 = d2.width + "," + d2.height;
      blueDimension.setText(s1);
      redDimension.setText(s2);
   }

   /**
    * @param args
    */
   public static void main(String[] args) {
      SSCE win = new SSCE();
      win.setVisible(true);
      win.updateDimensionText();
   }
}