我想将填充了按钮的网格布局分成两部分,其中前5个按钮可以进入BorderLayout
的北部,最后5个按钮可以进入南部。
myPanel = new JPanel(new GridLayout(10, 1, 5, 10));
final JPanel mainPanel = new JPanel(new BorderLayout());
答案 0 :(得分:2)
有许多选项,只使用一个面板,您可以更改GridLayout
属性以允许5列和2行...
JPanel panel = new JPanel(new GridLayout(2, 10));
for (int index = 0; index < 10; index++) {
panel.add(new JButton(Integer.toString(index)));
}
或者,你可以使用三个面板,一个用作外部容器,定义行,其他面板定义行
JPanel outer = new JPanel(new GridLayout(2, 1));
JPanel topRow = new JPanel(new GridLayout(1, 5));
for (int index = 0; index < 5; index++) {
topRow.add(new JButton(Integer.toString(index)));
}
JPanel bottomRow = new JPanel(new GridLayout(1, 5));
for (int index = 5; index < 10; index++) {
bottomRow.add(new JButton(Integer.toString(index)));
}
outer.add(topRow);
outer.add(bottomRow);
答案 1 :(得分:0)
以下代码可以帮助您;)您需要将动作侦听器添加到按钮中。
final JPanel mainPanel = new JPanel(new BorderLayout());
JPanel northPanel = new JPanel(new GridLayout(1, 5));
JButton b1 = new Button("Button 1");
northPanel.add(b1);
(...) (Add the other 4 buttons)
mainPanel.add(southPanel, BorderLayout.NORTH);
JPanel southPanel = new JPanel(new GridLayout(1, 5));
JButton b6 = new Button("Button 6");
southPanel .add(b6);
(...) (Add the other 4 buttons)
mainPanel.add(southPanel, BorderLayout.SOUTH);
答案 2 :(得分:0)
试试这个:
myPanelNorth = new JPanel(new GridLayout(5, 1, 5, 10));
myPanelSouth = new JPanel(new GridLayout(5, 1, 5, 10));
BorderLayout b = new BorderLayout()
final JPanel mainPanel = new JPanel();
mainPanel.add(myPanelNorth , b.NORTH);
mainPanel.add(myPanelSouth , b.SOUTH);
然后分别将你的按钮添加到北方和南方