java73-GUL网格布局管理器
//面向對象思想,網格管理布局器
import java.awt.*;
import javax.swing.*;
public class test08 extends JFrame {
? ? JButton[] an={null,null,null,null,null,null,null,null,null};//把需要的組件定義在這里
? ? int s=9;
? ? public ?static void main(String[] args){
? ? ? ? test08 lx1=new test08();//主函數調用即可
? ? }
? ? //定義一個構造器
? ? public ?test08(){
? ? ? ? an[0]=new JButton("蘋果");
? ? ? ? an[1]=new JButton("香蕉");
? ? ? ? an[2]=new JButton("李子");
? ? ? ? an[3]=new JButton("梨子");
? ? ? ? an[4]=new JButton("栗子");
? ? ? ? an[5]=new JButton("哈密瓜");
? ? ? ? an[6]=new JButton("西瓜");
? ? ? ? an[7]=new JButton("花生");
? ? ? ? an[8]=new JButton("花兒");
? ? ? ? this.setLayout(new GridLayout(3,3,3,3));
? ? ? ? // this.setLayout(new FlowLayout(FlowLayout.LEFT));靠左
? ? ? ? //添加布局管理器,以免添加出現錯誤,由于java默認的邊界布局管理器
? ? ? ? for(int i=0;i<s;i++){
? ? ? ? ? ? this.add(an[i]);
? ? ? ? }
?
? ? ? ? this.setTitle("歌謠");
? ? ? ? //設置初始位置
? ? ? ? this.setLocation(100,100);
? ? ? ? //設置大小
? ? ? ? this.setSize(180,180);
? ? ? ? //釋放窗口關閉的資源,這個要寫對
? ? ? ? this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
? ? ? ? //顯示界面
? ? ? ? this.setVisible(true);
? ? ? ? //不可動
? ? ? ? this.setResizable(false);
? ? ? ? //1繼承jframe類
? ? ? ? //2在最上方定義組件
? ? ? ? //3在構造方法中創建組件
? ? ? ? //4在構造方法添加組件
? ? ? ? //5設置窗體屬性
? ? ? ? //6顯示窗體
? ? ? ? //7在主函數創建對象
? ? }
}
運行結果
?
總結
以上是生活随笔為你收集整理的java73-GUL网格布局管理器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 詹姆斯高斯林_詹姆斯·高斯林(James
- 下一篇: 基于LM358的几种外围电路