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è)置窗口可見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è)置窗口可見jFrame.setVisible(true);}}卡片布局
顧名思義,若一個(gè)容器使用卡片布局,其里面的所有組件就像是一副牌一樣重疊在一起,容器只能顯示一個(gè)組件,默認(rèn)顯示第一個(gè)組件,可以通過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è)置窗口可見jFrame.setVisible(true);}}自定義布局
所謂自定義布局就是不使用任何布局管理器,而是我們自己通過指定組件的X坐標(biāo),Y坐標(biāo),寬度,高度來指定組件的位置
這里的坐標(biāo)和我們平時(shí)的坐標(biāo)有些區(qū)別,如下:
組件是以左上角頂點(diǎn)為原點(diǎn)來定位坐標(biāo),使用自定義布局,要將容器使用的布局管理器設(shè)置為null
那有的小伙伴會(huì)問了,既然布局管理器設(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è)置窗口可見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. WML教程之文本框控件Input2. 不要在HTML中濫用div3. 詳解CSS偽元素的妙用單標(biāo)簽之美4. 利用CSS3新特性創(chuàng)建透明邊框三角5. Vue3獲取DOM節(jié)點(diǎn)的3種方式實(shí)例6. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)7. XML入門的常見問題(三)8. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)9. vue實(shí)現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細(xì)教程10. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂代碼
