关于java图形界面GridLayout的问题
package yuzhengzhong;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GridLayout {
public static void main(String[] args){
Frame frameSix=new Frame("GirdLayout Example!");
Button button1=new Button("button1");
Button button2=new Button("button2");
Button button3=new Button("button3");
Button button4=new Button("button4");
Button button5=new Button("button5");
Button button6=new Button("button6");
Button button7=new Button("button7");
frameSix.setLayout(new GridLayout(2,3));
frameSix.add(button1);
frameSix.add(button2);
frameSix.add(button3);
frameSix.add(button4);
frameSix.add(button5);
frameSix.add(button6);
frameSix.add(button7);
frameSix.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frameSix.pack();
frameSix.setVisible(true);
}
}
frameSix.setLayout(new GridLayout(2,3));请问这一行为什么会出错呀
------解决方案--------------------
你的类名和JDK类库中的java.awt.GridLayout同名,因为setLayout方法需要的参数是JDK类库中的java.awt.GridLayout,而不是你现在写的这个GridLayout类,但是你在frameSix.setLayout(new GridLayout(2,3));中没有限制包路径,所以默认new GridLayout应该是new的你现在写的这个类,所以报错
解决方案如下
1、frameSix.setLayout(new GridLayout(2,3));改为frameSix.setLayout(new java.awt.GridLayout(2, 3));
2、改一下你的类名,比如叫GridLayoutTest,然后import的时候增加一行import java.awt.GridLayout;