//test.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class test{
public static void main(String args[]){
JFrame frame;
JPanel panel;
JButton[][]button;
//JButton close;
GridLayout gl;

panel=new JPanel();
frame=new JFrame("aaa");
frame.setVisible(true);
frame.getContentPane().add(panel); 
gl=new GridLayout(2,5);
panel.setLayout(gl);
button=new JButton[2][5];
for(int x=0;x<2;x++) 
for(int y=0;y<5;y++){
button[x][y]=new JButton("o");
panel.add(button[x][y]);
}
//close=new JButton("close");
//panel.add(close);
//close.addActionListener(this);
frame.pack();
     }
}

解决方案 »

  1.   

    这里错了:gl=new GridLayout(3,5);
    应该是:  gl=new GridLayout(2,5);
      

  2.   

    没写错啊?我就是想问在GridLayout(3,5)中怎样加JButton[2][5]^^
      

  3.   

    是这样的,我想在GridLayout(3,5)的前2行加10个JButton[2][5],第3行加1个JButton("close"),难道不行吗?一定要用2个JPanel吗?
      

  4.   

    那好象就只能用两个jpanel了。
      

  5.   

    那就用两个panel吧,这样第3行的Button的位置还能好看一些。:)
      

  6.   

    搞定这个:
    http://www.csdn.net/expert/topic/1004/1004416.xml?temp=.9450647
      

  7.   

    这算不算是GridLayout的一个bug?
      

  8.   

    不能算是bug吧,看看下面这段,你就明白了。
    When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number or rows and the total number of components in the layout. So, for example, if three rows and two columns have been specified and nine components are added to the layout, then they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero. 
    也就是说行是优先考虑的因素。
      

  9.   

    搞定这个:
    http://www.csdn.net/expert/topic/1004/1004416.xml?temp=.9450647
      

  10.   

    当然不是Bug, 楼上namowen兄不是写着的忙:Specifying the number of columns affects the layout only when the number of rows is set to zero. 给你改好了
    //test.java
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class test{
    public static void main(String args[]){
    JFrame frame;
    JPanel panel;
    JButton[][]button;
    //JButton close;
    GridLayout gl;

    panel=new JPanel();
    frame=new JFrame();
    frame.setVisible(true);
    frame.getContentPane().add(panel); 
    gl=new GridLayout(0,5);
    panel.setLayout(gl);
    button=new JButton[2][5];
    for(int x=0;x<2;x++) 
    for(int y=0;y<5;y++){
    button[x][y]=new JButton("o");
    panel.add(button[x][y]);
    }
    JButton close=new JButton("close");
    panel.add(close);
    //close.addActionListener(this);
    frame.pack();
         }
    }