日期:2014-05-20  浏览次数:20648 次

java上的JButton 的定义为什么不能这样?

JButton mybutton=new JButton("OK");
mycontainer.add(mybutton);
 
 


JButton不也是一个类吗?为什么上面的编译不能通过;比如同样的还有这个就可以通过编译:
	Container mycontainer=getContentPane();
    FlowLayout layout =new FlowLayout(FlowLayout.LEFT,10,20);
    mycontainer.setLayout(layout);
Java 布局

------解决方案--------------------
JButton mybutton=new JButton("OK");
mycontainer.add(mybutton);

container是容器,里面只能放面板组建;JButton不不属于面板组建;


 Container mycontainer=getContentPane();    FlowLayout layout =new FlowLayout(FlowLayout.LEFT,10,20);    mycontainer.setLayout(layout);

这个可以。。属于面板组建;
------解决方案--------------------
你再仔细看看代码,是不是写到类外面了。编译通过没任何问题。