Java圖形界面GUI布局方式(小結(jié))
流式布局
采用流式布局會將元素按從左到右的順序排列,如果一個元素在一行中放不下,那這個元素會另起一行依然按照從左到右的順序排列
示例:

代碼
public class Test {
public static void main(String[] args) {
// 創(chuàng)建窗口
JFrame jFrame = new JFrame();
// 設置窗口名稱
jFrame.setTitle("hello");
// 創(chuàng)建流式布局管理器 對齊方式為左對齊
LayoutManager layout = new FlowLayout(FlowLayout.LEFT);
// 關(guān)閉窗口結(jié)束程序
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 創(chuàng)建內(nèi)容面板
Container contentpage = jFrame.getContentPane();
// 設置內(nèi)容面板布局方式為流布局
contentpage.setLayout(layout);
// 創(chuàng)建按鈕
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
// 設置按鈕大小
button1.setPreferredSize(new Dimension(100,100));
button2.setPreferredSize(new Dimension(100,100));
button3.setPreferredSize(new Dimension(100,100));
button4.setPreferredSize(new Dimension(100,100));
button5.setPreferredSize(new Dimension(100,100));
// 設置按鈕背景顏色
button1.setBackground(Color.red);
button2.setBackground(Color.blue);
button3.setBackground(Color.pink);
button4.setBackground(Color.orange);
button5.setBackground(Color.yellow);
// 將按鈕添加到內(nèi)容面板中
contentpage.add(button1);
contentpage.add(button2);
contentpage.add(button3);
contentpage.add(button4);
contentpage.add(button5);
// 設置窗口大小
jFrame.setSize(500, 300);
// 設置窗口可見
jFrame.setVisible(true);
}
}
邊界布局
采用邊界布局會將元素分別劃分到東,西,中,南,北五個方位,分別使用EAST,WEST,CENTER,SOUTH,NORTH標識,每個方位只能放一個元素
示例

代碼
public class Test {
public static void main(String[] args) {
// 創(chuàng)建窗口
JFrame jFrame = new JFrame();
// 設置窗口名稱
jFrame.setTitle("hello");
// 創(chuàng)建邊界布局管理器
BorderLayout layout = new BorderLayout();
// 關(guān)閉窗口結(jié)束程序
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 創(chuàng)建內(nèi)容面板
Container contentpage = jFrame.getContentPane();
// 設置內(nèi)容面板布局方式為流布局
contentpage.setLayout(layout);
// 創(chuàng)建按鈕
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
// 設置按鈕背景顏色
button1.setBackground(Color.red);
button2.setBackground(Color.blue);
button3.setBackground(Color.pink);
button4.setBackground(Color.orange);
button5.setBackground(Color.yellow);
// 將按鈕添加到內(nèi)容面板中
// 將按鈕放置到北部
contentpage.add(button1,BorderLayout.NORTH);
// 將按鈕放置到南部
contentpage.add(button2,BorderLayout.SOUTH);
// 將按鈕放置到西部
contentpage.add(button3,BorderLayout.WEST);
// 將按鈕放置到東部
contentpage.add(button4,BorderLayout.EAST);
// 將按鈕放置到中心
contentpage.add(button5,BorderLayout.CENTER);
// 設置窗口大小
jFrame.setSize(500, 300);
// 設置窗口可見
jFrame.setVisible(true);
}
}
卡片布局
顧名思義,若一個容器使用卡片布局,其里面的所有組件就像是一副牌一樣重疊在一起,容器只能顯示一個組件,默認顯示第一個組件,可以通過CardLayout中的show方法改變顯示的組件
示例
顯示第一個按鈕

顯示第二個按鈕

代碼
public class Test {
public static void main(String[] args) {
// 創(chuàng)建窗口
JFrame jFrame = new JFrame();
// 設置窗口名稱
jFrame.setTitle("hello");
// 創(chuàng)建卡片布局管理器
CardLayout layout = new CardLayout();
// 關(guān)閉窗口結(jié)束程序
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 創(chuàng)建面板
JPanel jPanel = new JPanel();
// 設置面板布局方式為卡片布局
jPanel.setLayout(layout);
// 添加 按鈕 設置背景顏色
JButton jButton1 = new JButton();
jButton1.setBackground(Color.pink);
JButton jButton2 = new JButton();
jButton2.setBackground(Color.yellow);
// 將按鈕添加到面板中并對按鈕進行命名
jPanel.add(jButton1,"bt1");
jPanel.add(jButton2,"bt2");
// 指定在面板上顯示的按鈕
layout.show(jPanel, "bt2");
// 將面板添加到窗口中
jFrame.add(jPanel);
// 設置窗口大小
jFrame.setSize(500,300);
// 設置窗口可見
jFrame.setVisible(true);
}
}
自定義布局
所謂自定義布局就是不使用任何布局管理器,而是我們自己通過指定組件的X坐標,Y坐標,寬度,高度來指定組件的位置
這里的坐標和我們平時的坐標有些區(qū)別,如下:

組件是以左上角頂點為原點來定位坐標,使用自定義布局,要將容器使用的布局管理器設置為null
那有的小伙伴會問了,既然布局管理器設置為null,那可不可以直接不設置啊,當然不行,如果不設置的話,組件會不顯示
示例

代碼
public class Test {
public static void main(String[] args) {
// 創(chuàng)建窗口
JFrame jFrame = new JFrame();
// 設置窗口名稱
jFrame.setTitle("hello");
// 關(guān)閉窗口同時結(jié)束程序
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 創(chuàng)建面板
JPanel jPanel = new JPanel();
// 使用自定義布局,將容器使用的布局管理器設置為null
jPanel.setLayout(null);
// 添加 按鈕 設置背景顏色
JButton jButton1 = new JButton();
jButton1.setBackground(Color.pink);
JButton jButton2 = new JButton();
jButton2.setBackground(Color.yellow);
// 設置按鈕的坐標為(100,100) ,寬度為100,高度為100
jButton1.setBounds(new Rectangle(100,100,100,100));
// 設置按鈕的坐標為(220,70) ,寬度為100,高度為100
jButton2.setBounds(new Rectangle(220,70,100,100));
// 將按鈕添加到面板中
jPanel.add(jButton1);
jPanel.add(jButton2);
// 將面板添加到窗口中
jFrame.add(jPanel);
// 設置窗口大小
jFrame.setSize(500,300);
// 設置窗口可見
jFrame.setVisible(true);
}
}
到此這篇關(guān)于Java圖形界面GUI布局方式(小結(jié))的文章就介紹到這了,更多相關(guān)Java GUI布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot統(tǒng)一接口返回數(shù)據(jù)的實現(xiàn)
這篇文章主要介紹了springboot統(tǒng)一接口返回數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
Java實現(xiàn)定時讀取json文件里內(nèi)容的示例代碼
有時候我們會需要定時來讀取JSON配置文件里的內(nèi)容,來執(zhí)行一些業(yè)務邏輯上的操作,本文就介紹了Java實現(xiàn)定時讀取json文件里內(nèi)容的示例代碼,感興趣的可以了解一下2023-08-08
java中ArrayList和LinkedList的區(qū)別詳解
這篇文章主要介紹了java中ArrayList和LinkedList的區(qū)別詳解,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2021-01-01
SpringBoot參數(shù)校驗之@Valid的使用詳解
這篇文章主要通過示例為大家詳細介紹一下介紹了SpringBoot參數(shù)校驗中@Valid的使用方法,文中的示例代碼講解詳細,需要的可以參考一下2022-06-06
基于Java的MathML轉(zhuǎn)圖片的方法(示例代碼)
最近接到一個新需求mathML轉(zhuǎn)圖片怎么實現(xiàn)呢?剛開始還真是蒙圈了,不知道怎么實現(xiàn),今天小編記錄一種基于Java的MathML轉(zhuǎn)圖片的方法,感興趣的朋友一起看看吧2021-06-06
Spring的連接數(shù)據(jù)庫以及JDBC模板(實例講解)
下面小編就為大家?guī)硪黄猄pring的連接數(shù)據(jù)庫以及JDBC模板(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Spring多定時任務@Scheduled執(zhí)行阻塞問題解決
這篇文章主要介紹了Spring多定時任務@Scheduled執(zhí)行阻塞問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05

