久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Java圖形界面GUI布局方式(小結(jié))

瀏覽:2日期:2022-08-14 08:43:37
流式布局

采用流式布局會(huì)將元素按從左到右的順序排列,如果一個(gè)元素在一行中放不下,那這個(gè)元素會(huì)另起一行依然按照從左到右的順序排列

示例:

Java圖形界面GUI布局方式(小結(jié))

代碼

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è)元素

示例

Java圖形界面GUI布局方式(小結(jié))

代碼

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è)按鈕

Java圖形界面GUI布局方式(小結(jié))

顯示第二個(gè)按鈕

Java圖形界面GUI布局方式(小結(jié))

代碼

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ū)別,如下:

Java圖形界面GUI布局方式(小結(jié))

組件是以左上角頂點(diǎn)為原點(diǎn)來定位坐標(biāo),使用自定義布局,要將容器使用的布局管理器設(shè)置為null

那有的小伙伴會(huì)問了,既然布局管理器設(shè)置為null,那可不可以直接不設(shè)置啊,當(dāng)然不行,如果不設(shè)置的話,組件會(huì)不顯示

示例

Java圖形界面GUI布局方式(小結(jié))

代碼

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)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 巴塘县| 呼伦贝尔市| 玉环县| 克什克腾旗| 米易县| 清原| 濮阳县| 定日县| 色达县| 永安市| 舟曲县| 平泉县| 肇州县| 巴东县| 当阳市| 右玉县| 兴城市| 方城县| 黄龙县| 崇信县| 湟中县| 共和县| 永顺县| 宜黄县| 南靖县| 宁河县| 个旧市| 陵川县| 临沂市| 东丰县| 庆阳市| 临江市| 大同市| 英吉沙县| 马龙县| 临邑县| 垫江县| 洛南县| 扶余县| 临清市| 宁陕县|