BorderLayout.EAST 与“East"之间的关系

clw_zxq 2016-11-17 08:23:56
大家好,参考了一篇学习教程,部分程序如下:
public class BoaderDemo {
public static void main(String args[]){
JLabel label1,label2,label3,label4,label5;
JFrame mw=new JFrame("My first window");
mw.setSize(250,200);
Container con=mw.getContentPane();
con.setLayout(new BorderLayout());
label1=new JLabel("east label");
label2=new JLabel("south label");
label3=new JLabel("west label");
label4=new JLabel("north label",JLabel.CENTER);
label5=new JLabel("middle",JLabel.CENTER);
con.add(label1,"East");
con.add(label2,"South");
con.add(label3,"West");
con.add(label4,"North");
con.add(label5,"Center");
mw.setVisible(true);

}

}
可以编译并运行,但是我把红字标出来的”East"改为“EAST",编译出错,改为小写也出错
改为BorderLayout.EAST,编译正常。
查看API,BorderLayout.EAST的EAST是 public static final所定义的,静态常量,这是最正确的写法
但是找不出为什么用”East"也能编译通过的依据。
...全文
211 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
自由自在_Yu 2016-11-18
  • 打赏
  • 举报
回复
java.awt. BorderLayout中是这么定义的 public static final String EAST = "East"; BorderLayout.EAST 和 "East" 就是一样的呀
clw_zxq 2016-11-18
  • 打赏
  • 举报
回复
谢谢,我仔细查看了JDK1.7 API离线手册, EAST public static final String EAST The east layout constraint (right side of container). See Also: Constant Field Values 点红色的链接出来的是 Constant Field Values Contents org.w3c.* 没有其它类 使用JDK1.6 API手册,可以查到 java.awt.BorderLayout public static final String AFTER_LAST_LINE "Last" public static final String AFTER_LINE_ENDS "After" public static final String BEFORE_FIRST_LINE "First" public static final String BEFORE_LINE_BEGINS "Before" public static final String CENTER "Center" public static final String EAST "East" public static final String LINE_END "After" public static final String LINE_START "Before" public static final String NORTH "North" public static final String PAGE_END "Last" public static final String PAGE_START "First" public static final String SOUTH "South" public static final String WEST "West" 我的API手册数据不全造成的。
Java语言编写的汉诺塔程序,形象直观; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Tower extends Frame implements ActionListener,Runnable { HannoiTower tower=null; Button renew,auto=null; char towerName[]={'A','B','C'}; int 盘子数目,盘宽,盘高; Thread thread; TextArea 信息条=null; public Tower() { thread=new Thread(this); 盘子数目=5; 盘宽=80; 盘高=18; 信息条=new TextArea(12,12); 信息条.setText(null); tower=new HannoiTower(盘子数目,盘宽,盘高,towerName,信息条); renew=new Button("重新开始"); auto=new Button("自动演示搬盘子"); renew.addActionListener(this); auto.addActionListener(this); add(tower,BorderLayout.CENTER); add(renew,BorderLayout.SOUTH); add(auto,BorderLayout.NORTH); add(信息条,BorderLayout.EAST); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); setBounds(60,20,670,540); validate(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==renew) { if(!(thread.isAlive())) { this.remove(tower); 信息条.setText(null); tower=new HannoiTower(盘子数目,盘宽,盘高,towerName,信息条); add(tower,BorderLayout.CENTER); validate(); } else { } } if(e.getSource()==auto) { if(!(thread.isAlive())) { thread=new Thread(this); } try { thread.start(); } catch(Exception eee) { } } } public void run() { this.remove(tower); 信息条.setText(null); tower=new HannoiTower(盘子数目,盘宽,盘高,towerName,信息条); add(tower,BorderLayout.CENTER); validate(); tower.自动演示搬运盘子(盘子数目,towerName[0] ,towerName[1],towerName[2]); } public static void main(String args[]) { new Tower(); } }

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧