所以,我有6个面板,所有的人都使用了网格布局。 我把它们放在一起使用的GridBagLayout,这里是我想要的设计
转出它成了一团糟“第二”面板变得更遥远到右边第三个小组被挤压了很多的左边,这是一场灾难。 这里是我的网格包布局代码
c.gridx = 0; c.gridy = 0;
add (first,c);
c.gridx = 2; //so that the second panel starts from the center and is divided evenly with the first panel
add(second,c);
c.gridx = 0; c.gridy = 1;
add(third,c);
c.gridx = 1;
add(fourth,c);
c.gridx = 2;
add(fifth,c);
c.gridx = 3;
add(sixth,c);
任何帮助表示赞赏。
当你写c.gridx = 0 and c.gridy = 0
,你忘了指定要多少列此JPanel
占领。 为了指定的是,你应该使用c.gridwidth = 2
,它告诉布局此JPanel
需求空间,两列。
为了得到这样的输出:
这里是一个小工作示例:
import java.awt.*;
import javax.swing.*;
// http://stackoverflow.com/questions/10968853/two-jpanels-in-jframe-one-under-other
public class GridBagLayoutExample
{
// http://stackoverflow.com/questions/10977017/grid-bag-layout-not-displaying-the-way-i-want
private void displayGUI()
{
JFrame frame = new JFrame("GridBagLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.weightx = 0.5;
gbc.weighty = 0.2;
JPanel topLeftPanel = new JPanel();
topLeftPanel.setOpaque(true);
topLeftPanel.setBackground(Color.DARK_GRAY);
contentPane.add(topLeftPanel, gbc);
gbc.gridx = 2;
JPanel topRightPanel = new JPanel();
topRightPanel.setOpaque(true);
topRightPanel.setBackground(Color.BLUE);
contentPane.add(topRightPanel, gbc);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0.25;
gbc.weighty = 0.8;
JPanel firstPanel = new JPanel();
firstPanel.setOpaque(true);
firstPanel.setBackground(Color.RED);
contentPane.add(firstPanel, gbc);
gbc.gridx = 1;
JPanel secondPanel = new JPanel();
secondPanel.setOpaque(true);
secondPanel.setBackground(Color.GREEN.darker());
contentPane.add(secondPanel, gbc);
gbc.gridx = 2;
JPanel thirdPanel = new JPanel();
thirdPanel.setOpaque(true);
thirdPanel.setBackground(Color.WHITE);
contentPane.add(thirdPanel, gbc);
gbc.gridx = 3;
JPanel fourthPanel = new JPanel();
fourthPanel.setOpaque(true);
fourthPanel.setBackground(Color.MAGENTA);
contentPane.add(fourthPanel, gbc);
frame.setContentPane(contentPane);
frame.setSize(200, 300);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new GridBagLayoutExample().displayGUI();
}
});
}
}
下面是(另一)替代嵌套布局。 面板3到6将获得额外的高度,特宽幅会均匀地在所有6层的面板。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class SixPanelNested {
SixPanelNested() {
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new TitledBorder("BorderLayout()"));
JPanel north = new JPanel(new GridLayout(1,0));
north.setBorder(new TitledBorder("GridLayout(1,0)"));
gui.add(north, BorderLayout.NORTH);
for (int ii=1; ii<3; ii++) {
JLabel l = new JLabel("Panel " + ii);
l.setBorder(new LineBorder(Color.BLACK));
north.add(l);
}
JPanel south = new JPanel(new GridLayout(1,0));
south.setBorder(new TitledBorder("GridLayout(1,0)"));
gui.add(south);
for (int ii=3; ii<7; ii++) {
JLabel l = new JLabel("Panel " + ii);
l.setBorder(new LineBorder(Color.BLACK));
south.add(l);
}
JOptionPane.showMessageDialog(null, gui);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SixPanelNested();
}
});
}
}
我不熟悉不够用GridBagLayout
快速发现可以改进的地方,以你的代码来解决这个问题。 但是,另一种方法是可能的,使用嵌套BorderLayout
秒。
板“第一”,“第三”和“第四”可置于内部的一个JPanel
A
用BorderLayout
在NORTH
, WEST
和EAST
分别。 同样可以用于完成所述面板“第二”,“第五”和在一个JPanel“第六” B
。
然后,你的基2个面板( A
和B
)在另一面板用BorderLayout
在WEST
和EAST
补充一点:
c.gridwidth = 2;
加上第一和第二板前,然后设置gridwidth回到1添加在别人面前。 或者如上述建议你可以将它们进一步分离。 你可以使用两个FlowLayouts的每一行,并添加这两个另一个面板与北/南一的BorderLayout。