?? chap11-5.txt
字號:
// 程序11-5
import java.awt.*;
import javax.swing.*;
public class testGridLayout{
JFrame frame;
Container contentPane;
GridLayout grid1,grid2; // 定義兩個GridLayout引用
JButton buttons[ ];
String names[ ]={"one","two","three","four","five","six"};
boolean change; // 改變布局的標記
public testGridLayout( ){ //構造函數
frame=new subJFrame("testGridLayout");
contentPane=frame.getContentPane( );
grid1=new GridLayout(2,3,5,5); // 定義一個2行3列的布局管理器
grid2=new GridLayout(3,2); // 定義一個3行2列的布局管理器
contentPane.setLayout(grid1); // 設置內容格的布局
fillComponent(contentPane); // 向內容格中添加組件
change=true; // 設置改變標記為true
frame.setSize(220,200);
frame.show( );
}
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[i]); // 將按鈕加入容器
}
}
public void testGrid( ){
while(true){
try{
Thread.sleep(1000); // 睡眠1秒
}catch(Exception e){
e.printStackTrace( );
}
if(change)
contentPane.setLayout(grid1); // 采用grid1設置內容格的布局
else
contentPane.setLayout(grid2); // 采用grid2設置內容格的布局
change=!change; // 反轉標記
contentPane.validate( ); // 使改變后的布局有效
}
}
public static void main(String args[ ]){
testGridLayout obj=new testGridLayout( );
obj.testGrid( );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -