Java swing gridbaglayout未填充整个框架
我正在尝试构建一个类似于JProgressBar的组件,它将显示三种状态,而不是如图所示的两种状态 我通过将三个JLabel组件(具有三种不同的背景色)添加到布局设置为GridBagLayout的面板中来实现这一点。我为这些标签指定了相应的权重,以便根据父面板的大小调整它们的大小。下面是相应的示例代码:Java swing gridbaglayout未填充整个框架,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我正在尝试构建一个类似于JProgressBar的组件,它将显示三种状态,而不是如图所示的两种状态 我通过将三个JLabel组件(具有三种不同的背景色)添加到布局设置为GridBagLayout的面板中来实现这一点。我为这些标签指定了相应的权重,以便根据父面板的大小调整它们的大小。下面是相应的示例代码: public class CustomProgressBar { public static void main(String[] args) { JFrame frame =
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
以提供负插入
,则不会显示底层面板
。变化如下
GridBagConstraints gbc=新的GridBagConstraints(-1,-1,1,0,0,10,0,新插入(-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);
}
}
输出:
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);
}
}
这似乎是一个取整问题。将初始宽度设置为160时,左侧或右侧没有黑色条。它们在缓慢调整大小时出现/消失。黑色条纹是由于舍入错误造成的。父面板的宽度始终为整数像素。当将它们分发到特定组件时,布局管理器会得到一些额外的像素,它不知道放在哪里。这就是GridBagLayout的工作原理。如果这对您来说是一个严重的问题,您将不得不覆盖GridBagLayout。看看它处理的是您遇到的类似问题。@MatheM相关链接确实解决了我的问题。更简单的方法是:创建默认约束“gbc=new GridBagConstraints()”,然后只设置水平插入“gbc.Insets=new Insets(0,-1,0,-1)”。虽然这种方法很简单,可以填充整个面板,但当其中一个标签必须填充整个面板时,会出现问题。例如,在上面的示例中,将第一个和第二个标签的重量设为零,应使用红色的第三个标签填充整个面板,但我们在开始处看到一条绿色。