Java圖形界面GUI布局方式(小結(jié))
采用流式布局會(huì)將元素按從左到右的順序排列,如果一個(gè)元素在一行中放不下,那這個(gè)元素會(huì)另起一行依然按照從左到右的順序排列
示例:
代碼
public class Test {public static void main(String[] args) {//創(chuàng)建窗口JFrame jFrame = new JFrame();//設(shè)置窗口名稱jFrame.setTitle('hello');//創(chuàng)建流式布局管理器 對(duì)齊方式為左對(duì)齊LayoutManager layout = new FlowLayout(FlowLayout.LEFT);//關(guān)閉窗口結(jié)束程序jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//創(chuàng)建內(nèi)容面板Container contentpage = jFrame.getContentPane();//設(shè)置內(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');//設(shè)置按鈕大小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));//設(shè)置按鈕背景顏色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);//設(shè)置窗口大小jFrame.setSize(500, 300);//設(shè)置窗口可見(jiàn)jFrame.setVisible(true);}}邊界布局
采用邊界布局會(huì)將元素分別劃分到東,西,中,南,北五個(gè)方位,分別使用EAST,WEST,CENTER,SOUTH,NORTH標(biāo)識(shí),每個(gè)方位只能放一個(gè)元素
示例
代碼
public class Test {public static void main(String[] args) {//創(chuàng)建窗口JFrame jFrame = new JFrame();//設(shè)置窗口名稱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();//設(shè)置內(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');//設(shè)置按鈕背景顏色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);//設(shè)置窗口大小jFrame.setSize(500, 300);//設(shè)置窗口可見(jiàn)jFrame.setVisible(true);}}卡片布局
顧名思義,若一個(gè)容器使用卡片布局,其里面的所有組件就像是一副牌一樣重疊在一起,容器只能顯示一個(gè)組件,默認(rèn)顯示第一個(gè)組件,可以通過(guò)CardLayout中的show方法改變顯示的組件
示例
顯示第一個(gè)按鈕
顯示第二個(gè)按鈕
代碼
public class Test {public static void main(String[] args) {//創(chuàng)建窗口JFrame jFrame = new JFrame();//設(shè)置窗口名稱jFrame.setTitle('hello');//創(chuàng)建卡片布局管理器CardLayout layout = new CardLayout();//關(guān)閉窗口結(jié)束程序jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//創(chuàng)建面板JPanel jPanel = new JPanel();//設(shè)置面板布局方式為卡片布局jPanel.setLayout(layout);//添加 按鈕 設(shè)置背景顏色JButton jButton1 = new JButton();jButton1.setBackground(Color.pink);JButton jButton2 = new JButton();jButton2.setBackground(Color.yellow);//將按鈕添加到面板中并對(duì)按鈕進(jìn)行命名jPanel.add(jButton1,'bt1');jPanel.add(jButton2,'bt2');//指定在面板上顯示的按鈕layout.show(jPanel, 'bt2');//將面板添加到窗口中jFrame.add(jPanel);//設(shè)置窗口大小jFrame.setSize(500,300);//設(shè)置窗口可見(jiàn)jFrame.setVisible(true);}}自定義布局
所謂自定義布局就是不使用任何布局管理器,而是我們自己通過(guò)指定組件的X坐標(biāo),Y坐標(biāo),寬度,高度來(lái)指定組件的位置
這里的坐標(biāo)和我們平時(shí)的坐標(biāo)有些區(qū)別,如下:
組件是以左上角頂點(diǎn)為原點(diǎn)來(lái)定位坐標(biāo),使用自定義布局,要將容器使用的布局管理器設(shè)置為null
那有的小伙伴會(huì)問(wèn)了,既然布局管理器設(shè)置為null,那可不可以直接不設(shè)置啊,當(dāng)然不行,如果不設(shè)置的話,組件會(huì)不顯示
示例
代碼
public class Test {public static void main(String[] args) {//創(chuàng)建窗口JFrame jFrame = new JFrame();//設(shè)置窗口名稱jFrame.setTitle('hello');//關(guān)閉窗口同時(shí)結(jié)束程序jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//創(chuàng)建面板JPanel jPanel = new JPanel();//使用自定義布局,將容器使用的布局管理器設(shè)置為nulljPanel.setLayout(null);//添加 按鈕 設(shè)置背景顏色JButton jButton1 = new JButton();jButton1.setBackground(Color.pink);JButton jButton2 = new JButton();jButton2.setBackground(Color.yellow);//設(shè)置按鈕的坐標(biāo)為(100,100) ,寬度為100,高度為100jButton1.setBounds(new Rectangle(100,100,100,100));//設(shè)置按鈕的坐標(biāo)為(220,70) ,寬度為100,高度為100jButton2.setBounds(new Rectangle(220,70,100,100));//將按鈕添加到面板中jPanel.add(jButton1);jPanel.add(jButton2);//將面板添加到窗口中jFrame.add(jPanel);//設(shè)置窗口大小jFrame.setSize(500,300);//設(shè)置窗口可見(jiàn)jFrame.setVisible(true);}}
到此這篇關(guān)于Java圖形界面GUI布局方式(小結(jié))的文章就介紹到這了,更多相關(guān)Java GUI布局內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析2. jsp實(shí)現(xiàn)登錄界面3. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法4. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫(huà)特效5. 刪除docker里建立容器的操作方法6. 概述IE和SQL2k開(kāi)發(fā)一個(gè)XML聊天程序7. XML入門的常見(jiàn)問(wèn)題(二)8. asp批量添加修改刪除操作示例代碼9. jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲10. jsp實(shí)現(xiàn)簡(jiǎn)單用戶7天內(nèi)免登錄
