GridBagLayout无法正常工作 [英] GridBagLayout is not working
问题描述
this.rootComponent.setLayout(new GridBagLayout());
GridBagConstraints gbc=new GridBagConstraints();
//gbc.gridwidth=2;
gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth=8;
gbc.anchor=GridBagConstraints.FIRST_LINE_START;
this.rootComponent.add(new JLabel("Test label 1"),gbc);
gbc.gridx=8;
gbc.gridy=12;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.anchor=GridBagConstraints.FIRST_LINE_START;
this.rootComponent.add(new JLabel("Test label"),gbc);
想要这样格式化.灰色部分显示了jpanel部分.最初,我想正确地布局前2个jpanel.这是行不通的.该如何解决?
Want to format it like this. grey part shows the jpanel part. Initially i want to layout the first 2 jpanel correctly . which is not working. how to fix it?
推荐答案
您没有为GridBagConstraints
指定任何weightx
和weighty
值.此外,您的gridwidth
值是错误的,因为对于最底部的JPanel
只需要将其设置为2
,对于其余的JPanel
,则需要将其设置为1
.
You are failing to specify any weightx
and weighty
values to the GridBagConstraints
. Moreover your gridwidth
values are wrong, since it only needs to be 2
for the bottom most JPanel
, for the rest it needs to be 1
.
我在做什么的说明:
考虑JPanel
s BLUE
和RED
,它们应沿着 X-AXIS 并按比例放置
相对于70:30
(因此,它们的weightx
将分别为0.7
和0.3
.由于沿 X-AXIS 的总面积为1.0
).
Explanation of what I am doing :
Consider JPanel
s BLUE
and RED
, they are to be placed along the X-AXIS, in the ratio
70:30
, with respect to each other (therefore their weightx
will be 0.7
and 0.3
respectively. Since the total area along the X-AXIS is 1.0
).
现在,这两个BLUE
和RED JPanel
都将沿 Y轴放置,相对于第三个GREEN JPanel
的比例为90:10
,因此,这两个这些BLUE
和RED
中的weighty = 0.9
和GREEN JPanel
将具有weighty = 0.1
,但是由于GREEN JPanel
被假定占据整个区域(相对于 X-AXIS BLUE
和RED JPanel
占用的gridwidth = 2
和weightx = 1.0
.
Now both of these BLUE
and RED JPanel
s are to be placed along the Y-AXIS, with respect to the third GREEN JPanel
in the ratio 90:10
, therefore, both of these BLUE
and RED
will have weighty = 0.9
, and the GREEN JPanel
will have weighty = 0.1
, but since GREEN JPanel
is suppose to occupy the whole area (with respect to X-AXIS), as occupied by BLUE
and RED JPanel
s, for that matter, its gridwidth = 2
and weightx = 1.0
.
尝试以下代码示例:
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutExample
{
private GridBagConstraints gbc;
public GridBagLayoutExample()
{
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
}
private void displayGUI()
{
JFrame frame = new JFrame("GridBagLayout Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = getPanel(Color.WHITE);
contentPane.setLayout(new GridBagLayout());
JPanel leftPanel = getPanel(Color.BLUE);
JPanel rightPanel = getPanel(Color.RED);
JPanel bottomPanel = getPanel(Color.GREEN.darker());
addComp(contentPane, leftPanel
, 0, 0, 0.7, 0.9, 1, 1, GridBagConstraints.BOTH);
addComp(contentPane, rightPanel
, 1, 0, 0.3, 0.9, 1, 1, GridBagConstraints.BOTH);
addComp(contentPane, bottomPanel
, 0, 1, 1.0, 0.1, 2, 1, GridBagConstraints.BOTH);
frame.setContentPane(contentPane);
//frame.pack();
frame.setSize(300, 300);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp
, int gridX, int gridY
, double weightX, double weightY
, int gridWidth, int gridHeight, int fill)
{
gbc.gridx = gridX;
gbc.gridy = gridY;
gbc.weightx = weightX;
gbc.weighty = weightY;
gbc.gridwidth = gridWidth;
gbc.gridheight = gridHeight;
gbc.fill = fill;
panel.add(comp, gbc);
}
private JPanel getPanel(Color backColour)
{
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(backColour);
panel.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
return panel;
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
new GridBagLayoutExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
以下是相同的输出:
这篇关于GridBagLayout无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!