我正在尝试绘制一个类似于下图的布局。我从昨天晚上开始尝试,但我还没能做到。任何想法如何在Java swing中绘制这样的布局
我尝试使用GridLayout
和GridBagLayout
进行绘制,但实际上我还没有得到它。
答案 0 :(得分:1)
这是一个纯粹的GridBagLayout
实现,但可以使用BorderLayout
并将三个顶部面板放入他们自己的JPanel
(使用GridBagLayout
)a将其放入CENTER
,底部面板位于SOUTH
位置......
setLayout(new GridBagLayout());
JPanel main = createPanel(400, 400, Color.RED);
JPanel menu = createPanel(600, 100, Color.BLUE);
JPanel upper = createPanel(200, 300, Color.GREEN);
JPanel lower = createPanel(200, 100, Color.MAGENTA);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 2;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(main, gbc);
gbc.gridx++;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.BOTH;
add(upper, gbc);
gbc.gridy++;
add(lower, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = 2;
gbc.weightx = 1;
add(menu, gbc);