帮看个JAVA语法问题

来源:百度知道 编辑:UC知道 时间:2024/06/03 16:43:50
import java.awt.*;
public class TestBorderLayout {
public static void main(String args[]) {
Frame f;
f = new Frame("Border Layout");
Button bn = new Button("BN");
Button bs = new Button("BS");
Button bw = new Button("BW");
Button be = new Button("BE");
Button bc = new Button("BC");

f.add(bn, "North");
f.add(bs, "South");
f.add(bw, "West");
f.add(be, "East");
f.add(bc, "Center");

// 也可使用下述语句
/*
f.add(bn, BorderLayout.NORTH);
f.add(bs, BorderLayout.SOUTH);
f.add(bw, BorderLayout.WEST);
f.add(be, BorderLayout.EAST);
f.add(bc, BorderLayout.CENTER);
*/

f.setSize(200,200);
f.setVisible(true);
}
}
帮忙解释下里面分别用的是add的哪个方法
f.add(bc, "Center");"Center"怎么可以这样直接调用啊》

//简单明了的说:
BorderLayout.CENTER 是一个常量值 这个常量是BorderLayout的一个常量 表示的是放在这个BorderLayout 的位置 是一个int 类型的 这个没错
jdk 里的返回的字符串类型的写法 是如下的写法
f.add(bc, "Center"); 这里的center 是一个字符串类型
两种写法都可以.........不要花费时间在这些问题上 掌握一种写法 了解另一种写法 是初学者最适当的做法~!谢谢~!

BorderLayout.CENTER确实是String类型的

但是frame本身有add(Component comp, Object constraints)这个方法:
Adds the specified component to the end of this container.
Also notifies the layout manager to add the component to
this container's layout using the specified constraints object.

BorderLayout.CENTER是String类型当然属于Object类,于是f.add(bc, BorderLayout.CENTER)就是调用了上面这个方法

f.add(bc, BorderLayout.CENTER);我听视频里讲的BorderLayout.CENTER的返回值是int类型 可我查的时候看到明明是string类型

返回以前int类型可能是以前java类库但是现在都是返回String。

public static final String AFTER_LAST_LINE "Last"
public static final String AFTER_LINE_ENDS "After"
public static final String BEFORE_FIRST_LINE "First&