java中关于gui组件

来源:百度知道 编辑:UC知道 时间:2024/06/04 01:36:46
Frame f=new Frame();
f.add(a=new Button("1"),"a");
这里的后边的"a"是啥意思?我查了java.awt.*包中的容器类中的方法,没有以字符串作第2参数的。。只有一个以object为参数的,不知道做什么的
不是那个用方位的那个方法。。这个就和前边那个生成的实例名字一样

有这个方法啊,第一参数组件,第二参数组件在容器中的位置(如果容器的布局是:BorderLayout,这里的"a"作为参数可能会抛出异常)
常用参数有 Center,North,East,West...
---------------------------------------
如果你非得这样搞,那你就去重写方法add(Button b,String s);
public void add(Button b,String s){
b.setName(s);
this.add(b);
}
否则,add(a=new Button("1"),"a"); 肯定会抛出异常!!~~

该方法没有这样的参数类型,会抛出异常的,public Component add(Component comp, int index)
comp - the component to be added
index - the position at which to insert the component, or -1 to append the component to the end

有这样的类型,给你参考参考

他实际是在Frame里面加了一个BUTTON
也可以改成Button a=new Button();
f.add(a,"a");