JAVA學習筆記(四十五) - 布局管理器

來源:互聯網
上載者:User

標籤:流式布局管理器   邊框布局管理器   網格布局管理器   卡片布局管理器   

FlowLayout流式布局管理器
/* * FlowLayout流式布局管理器 */public class Test03 {    public static void main(String[] args) {        Frame frame=new Frame("My Frame");        Button btn1=new Button("按鈕1");        Button btn2=new Button("按鈕2");        Button btn3=new Button("按鈕3");        Button btn4=new Button("按鈕4");        frame.add(btn1);        frame.add(btn2);        frame.add(btn3);        frame.add(btn4);        frame.setSize(200, 200);        frame.setLocationRelativeTo(null);        //frame.setLayout(new FlowLayout());//設定表單布局為流式布局,預設為邊框布局        frame.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));//設定表單布局為流式布局,預設為邊框布局        frame.setVisible(true);    }}
BorderLayout邊框布局管理器
import java.awt.BorderLayout;import java.awt.Button;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Frame;/* *BorderLayout邊框布局管理器 */public class Test04 {    public static void main(String[] args) {        Frame frame=new Frame("My Frame");        Button btn1=new Button("按鈕1");        Button btn2=new Button("按鈕2");        Button btn3=new Button("按鈕3");        Button btn4=new Button("按鈕4");        Button btn5=new Button("按鈕5");        frame.setLayout(new BorderLayout(10,5));        frame.add("North", btn1);        frame.add("South",btn2);        frame.add("West",btn3);        frame.add("East",btn4);        frame.add("Center",btn5);//預設將組件添加到Center中間地區        frame.add("North",new Button("按鈕6"));        frame.setSize(200, 200);        frame.setLocationRelativeTo(null);        frame.setBackground(Color.gray);        frame.setVisible(true);    }}
GridLayout網格布局管理器
import java.awt.BorderLayout;import java.awt.Button;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.GridLayout;/* *GridLayout網格布局管理器 */public class Test05 {    public static void main(String[] args) {        Frame frame=new Frame("My Frame");        Button btn1=new Button("按鈕1");        Button btn2=new Button("按鈕2");        Button btn3=new Button("按鈕3");        Button btn4=new Button("按鈕4");        Button btn5=new Button("按鈕5");        Button btn6=new Button("按鈕6");        Button btn7=new Button("按鈕7");        frame.setLayout(new GridLayout(0, 2));//設定表單為網格布局        frame.add(btn1);        frame.add(btn2);        frame.add(btn3);        frame.add(btn4);        frame.add(btn5);        frame.add(btn6);        frame.add(btn7,2);//將組件添加到指定的網格中,索引從0開始,組件開始移動        frame.setSize(200, 200);        frame.setLocationRelativeTo(null);        frame.setBackground(Color.gray);        frame.setVisible(true);    }}
CardLayout卡片布局管理器
import java.awt.CardLayout;import java.awt.Color;import java.awt.Frame;import java.awt.Panel;/* * CardLayout卡片布局管理器 */public class Test06 {    public static void main(String[] args) {        Frame frame=new Frame("我的表單");        Panel pnl1=new Panel();        Panel pnl2=new Panel();        Panel pnl3=new Panel();        CardLayout c=new CardLayout();//建立卡片布局管理器        frame.setLayout(c);//設定表單為卡片布局        pnl1.setBackground(Color.red);        pnl2.setBackground(Color.yellow);        pnl3.setBackground(Color.blue);        //向表單中添加面板組件,並指定名稱        frame.add(pnl1,"first");        frame.add(pnl2,"second");        frame.add(pnl3,"third");        c.last(frame);//顯示最後一個卡片,即最後加入的組件        c.previous(frame);//顯示上一個卡片        c.show(frame, "first");//顯示名稱為first的卡片        frame.setSize(300, 300);        frame.setVisible(true);    }}

JAVA學習筆記(四十五) - 布局管理器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.