繁体   English   中英

Java swing gridbaglayout没有填充完整的框架

[英]Java swing gridbaglayout does not fill complete frame

我试图建立类似的JProgressBar这将显示出三种状态,而不是两个如图所示的组件在这里

我通过将三个JLabel组件(具有三种不同的背景颜色)添加到面板并将布局设置为GridBagLayout来构建它。 我已为这些标签指定了相应的权重,以便根据父面板的大小调整大小。 以下是相应的示例代码:

public class CustomProgressBar {

  public static void main(String[] args) {

    JFrame frame = new JFrame("test");

    JPanel panel = new JPanel();

    double firstLabelWeight = 0.10d;
    double secondLabelWeight = 0.20d;
    double thirdLabelWeight = 1d - firstLabelWeight - secondLabelWeight;

    JLabel firstLabel = new JLabel();
    firstLabel.setOpaque(true);
    firstLabel.setBackground(Color.GREEN);

    JLabel secondLabel = new JLabel();
    secondLabel.setOpaque(true);
    secondLabel.setBackground(Color.YELLOW);

    JLabel thirdLabel = new JLabel();
    thirdLabel.setOpaque(true);
    thirdLabel.setBackground(Color.RED);

    panel.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;

    gbc.weightx = firstLabelWeight;
    gbc.weighty = 1d;
    panel.add(firstLabel, gbc);

    gbc.weightx = secondLabelWeight;
    gbc.weighty = 1d;
    panel.add(secondLabel, gbc);

    gbc.weightx = thirdLabelWeight;
    gbc.weighty = 1d;
    panel.add(thirdLabel, gbc);

    panel.setBackground(Color.BLACK);
    panel.setPreferredSize(new Dimension(157, 50));

    frame.add(panel);
    frame.setVisible(true);
    frame.pack();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

现在,这种方法的问题是,当我尝试调整JFrame中,上述三个标签不灌装父面板完整的横向空间,我们看到黑色条到面板的两端如图所示这里

我也试过设置gbc.fill和gbc.anchor参数,但它仍然不起作用。

非常感谢任何有关此问题的帮助! 谢谢 :)

为了克服舍入问题,您可以通过以下方式覆盖面板的布局:

class MyLayout extends GridBagLayout
{
  private Component owner;

  public MyLayout(Component owner)
  {
    this.owner = owner;
  }

  @Override
  protected void adjustForGravity(GridBagConstraints constraints,
                                  Rectangle          rect)
  {
    // Adjust position and width of first (GREEN) label if necessary
    if ((rect.x > 0) && (rect.x <= 2))
    {
      rect.width += rect.x;
      rect.x = 0;
    }

    // Adjust width of last (RED) label if necessary
    int gap = owner.getWidth() - rect.x - rect.width;
    if ((gap > 0) && (gap <= 2))
      rect.width += gap;
  }

} // class MyLayout

然后,当然,以这种方式设置面板的布局:

panel.selLayout(new MyLayout(panel));

如果您构建GridBagConstraints提供负Insets ,这并不会显示出下面的Panel 更改将如下

GridBagConstraints gbc = new GridBagConstraints(-1,-1,1,1,0,0,10,0,new Insets(-1,-1,-1,-1),0,0);

更新示例:

public class CustomProgressBar {

public static void main(String[] args) {

    JFrame frame = new JFrame("test");

    JPanel panel = new JPanel();

    double firstLabelWeight = 0.10d;
    double secondLabelWeight = 0.20d;
    double thirdLabelWeight = 1d - firstLabelWeight - secondLabelWeight;

    JLabel firstLabel = new JLabel();
    firstLabel.setOpaque(true);
    firstLabel.setBackground(Color.GREEN);

    JLabel secondLabel = new JLabel();
    secondLabel.setOpaque(true);
    secondLabel.setBackground(Color.YELLOW);

    JLabel thirdLabel = new JLabel();
    thirdLabel.setOpaque(true);
    thirdLabel.setBackground(Color.RED);

    panel.setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints(-1, -1, 1, 1, 0, 0, 10, 0, new Insets(-1, -1, -1, -1), 0, 0);

    gbc.fill = GridBagConstraints.BOTH;

    gbc.weightx = firstLabelWeight;
    gbc.weighty = 1d;
    panel.add(firstLabel, gbc);

    gbc.weightx = secondLabelWeight;
    gbc.weighty = 1d;
    panel.add(secondLabel, gbc);

    gbc.weightx = thirdLabelWeight;
    gbc.weighty = 1d;
    panel.add(thirdLabel, gbc);

    panel.setBackground(Color.BLACK);
    panel.setPreferredSize(new Dimension(157, 50));

    frame.add(panel);
    frame.setVisible(true);
    frame.pack();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}

输出:

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM