java72-GUL流式布局管理器
//面向對象思想,流式管理布局器
import java.awt.*;
import javax.swing.*;
public class test07 extends JFrame {
? ? JButton[] an={null,null,null,null,null,null,null,null};//把需要的組件定義在這里
? ? public ?static void main(String[] args){
? ? ? ? test07 lx1=new test07();//主函數調用即可
? ? }
? ? //定義一個構造器
? ? public ?test07(){
? ? ? ? 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("花生");
? ? ? ? this.setLayout(new FlowLayout());
? ? ? ?// this.setLayout(new FlowLayout(FlowLayout.LEFT));靠左
? ? ? ? //添加布局管理器,以免添加出現錯誤,由于java默認的邊界布局管理器
?
? ? ? ? this.add(an[0]);
? ? ? ? this.add(an[1]);
? ? ? ? this.add(an[2]);
? ? ? ? this.add(an[3]);
? ? ? ? this.add(an[4]);
? ? ? ? this.add(an[5]);
? ? ? ? this.add(an[6]);
? ? ? ? this.add(an[7]);
? ? ? ? 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在主函數創建對象
? ? }
}
運行結果
?
總結
以上是生活随笔為你收集整理的java72-GUL流式布局管理器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python爬虫爬取智联招聘职位信息
- 下一篇: React 高阶组件HOC详解