?? chap11-4.txt
字號:
// 程序11-4
import java.awt.*;
import javax.swing.*;
public class testBorderLayout{
JFrame frame;
Container contentPane;
BorderLayout layout;
JButton buttons[ ];
String names[ ]={"North","South","East","West","Center"};
private void fillComponent(Container c){
buttons=new JButton[names.length];
for(int i=0;i<names.length;i++)
buttons[i]=new JButton(names[i]);
// 將各個按鈕加入到指定的位置
c.add(buttons[0],BorderLayout.NORTH);
c.add(buttons[1],BorderLayout.SOUTH);
c.add(buttons[2],BorderLayout.EAST);
c.add(buttons[3],BorderLayout.WEST);
c.add(buttons[4],BorderLayout.CENTER);
}
public void testBorder( ){
frame=new subJFrame("testBorderLayout");
contentPane=frame.getContentPane( );
layout=new BorderLayout(5,5); // 定義一個布局管理器
contentPane.setLayout(layout); // 設置內容格的布局
fillComponent(contentPane); // 向內容格中添加組件
frame.setSize(300,200);
frame.show( );
}
public void hideButton( ){ // 隱藏按鈕
for(int i=0;i<names.length;i++){
try{
Thread.sleep(1000); // 睡眠1秒
}catch(Exception e){
e.printStackTrace( );
}
buttons[i].setVisible(false);
layout.layoutContainer(contentPane);
}
}
public void showButton( ){ // 重新顯示按鈕
for(int i=names.length-1;i>=0;i--){
try{
Thread.sleep(1000); // 睡眠1秒
}catch(Exception e){
e.printStackTrace( );
}
buttons[i].setVisible(true);
layout.layoutContainer(contentPane);
}
}
public static void main(String args[ ]){
testBorderLayout obj=new testBorderLayout( );
obj.testBorder( );
obj.hideButton( ); // 隱藏按鈕
obj.showButton( ); // 顯示各個按鈕
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -