Java圖形界面GUI布局方式(小結(jié))
采用流式布局會將元素按從左到右的順序排列,如果一個元素在一行中放不下,那這個元素會另起一行依然按照從左到右的順序排列
示例:
代碼
public class Test {public static void main(String[] args) {//創(chuàng)建窗口JFrame jFrame = new JFrame();//設(shè)置窗口名稱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();//設(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);}}邊界布局
采用邊界布局會將元素分別劃分到東,西,中,南,北五個方位,分別使用EAST,WEST,CENTER,SOUTH,NORTH標(biāo)識,每個方位只能放一個元素
示例
代碼
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);}}卡片布局
顧名思義,若一個容器使用卡片布局,其里面的所有組件就像是一副牌一樣重疊在一起,容器只能顯示一個組件,默認(rèn)顯示第一個組件,可以通過CardLayout中的show方法改變顯示的組件
示例
顯示第一個按鈕
顯示第二個按鈕
代碼
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);//將按鈕添加到面板中并對按鈕進行命名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)和我們平時的坐標(biāo)有些區(qū)別,如下:
組件是以左上角頂點為原點來定位坐標(biāo),使用自定義布局,要將容器使用的布局管理器設(shè)置為null
那有的小伙伴會問了,既然布局管理器設(shè)置為null,那可不可以直接不設(shè)置啊,當(dāng)然不行,如果不設(shè)置的話,組件會不顯示
示例
代碼
public class Test {public static void main(String[] args) {//創(chuàng)建窗口JFrame jFrame = new JFrame();//設(shè)置窗口名稱jFrame.setTitle('hello');//關(guān)閉窗口同時結(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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. vue實現(xiàn)web在線聊天功能2. IntelliJ IDEA設(shè)置自動提示功能快捷鍵的方法3. Java Bean與Map之間相互轉(zhuǎn)化的實現(xiàn)方法4. SpringBoot+TestNG單元測試的實現(xiàn)5. Springboot 全局日期格式化處理的實現(xiàn)6. Java使用Tesseract-Ocr識別數(shù)字7. Python使用urlretrieve實現(xiàn)直接遠程下載圖片的示例代碼8. Django使用HTTP協(xié)議向服務(wù)器傳參方式小結(jié)9. JAMon(Java Application Monitor)備忘記10. 完美解決vue 中多個echarts圖表自適應(yīng)的問題
