日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java AWT布局

發(fā)布時間:2023/12/29 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java AWT布局 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

java.awt庫提供5種基本布局。 每種布局都有其自身的意義,并且它們是完全不同的。 在本文的此處,我們將討論如何將任何布局應(yīng)用于框架或面板,并簡要討論每種布局。

java.awt庫中可用的5種布局是:

  • 邊框布局
  • 網(wǎng)格布局
  • GridBag布局
  • 卡布局
  • 流程布局
  • 1.邊框布局

    BorderLayout是一種按照方向組織組件的布局。 邊框布局將框架或面板分為5個部分-北,南,東,西和居中。 通過傳遞附加參數(shù),可以將每個組件按特定方向排列。

    BorderLayoutExample.java package com.mkyong;import java.awt.BorderLayout; import java.awt.Button; import java.awt.Label; import javax.swing.JFrame;public class BorderLayoutExample extends JFrame {public static void main(String[] args) {BorderLayoutExample a = new BorderLayoutExample();}public BorderLayoutExample() {setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);BorderLayout b = new BorderLayout();setTitle("Border Layout");setSize(300, 300);add(new Button("North"), BorderLayout.NORTH);add(new Button("South"), BorderLayout.SOUTH);add(new Button("East"), BorderLayout.EAST);add(new Button("West"), BorderLayout.WEST);add(new Button("Center"), BorderLayout.CENTER);} }

    輸出量

    2.網(wǎng)格布局

    GridLayout是排列組件的一種更有組織的方式。 它以包含均勻分布的單元格的網(wǎng)格形式劃分框架或面板。 每個組件都添加到特定的單元格中。 組件的放置順序直接取決于將它們添加到框架或面板的順序。 下圖顯示了基于2列3行GridLayout的Frame。

    構(gòu)造函數(shù)GridLayout(int row,int cols)確定網(wǎng)格大小。

    GridLayoutExample.java package com.mkyong;import java.awt.Button; import java.awt.GridLayout; import javax.swing.JFrame;public class GridLayoutExample extends JFrame {public static void main(String[] args) {GridLayoutExample a = new GridLayoutExample();}public GridLayoutExample() {setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);GridLayout g = new GridLayout(3, 2);setLayout(g);setTitle("Grid Layout");setSize(300, 300);add(new Button("Button 1"));add(new Button("Button 2"));add(new Button("Button 3"));add(new Button("Button 4"));add(new Button("Button 5"));add(new Button("Button 6"));}}

    輸出量

    3. GridBag布局

    GridBagLayout是最靈活的布局,它提供了一種有組織但靈活的方式來排列組件。 它使開發(fā)人員可以靈活地選擇組件的確切位置(在網(wǎng)格中),即行跨度和列跨度以及水平和垂直間距。 下圖顯示了GridBagLayout 。 它包含一次跨2行的Button 5。

    GridBagLayoutExample.java package com.mkyong;import java.awt.Button; import java.awt.CardLayout; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame;public class GridBagLayoutExample extends JFrame {public static void main(String[] args) {GridBagLayoutExample a = new GridBagLayoutExample();}public GridBagLayoutExample() {setSize(300, 300);setPreferredSize(getSize());setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);GridBagLayout g = new GridBagLayout();GridBagConstraints gbc = new GridBagConstraints();setLayout(g);setTitle("GridBag Layout");GridBagLayout layout = new GridBagLayout();this.setLayout(layout);gbc.fill = GridBagConstraints.HORIZONTAL;gbc.gridx = 0;gbc.gridy = 0;this.add(new Button("Button 1"), gbc);gbc.gridx = 1;gbc.gridy = 0;this.add(new Button("Button 2"), gbc);gbc.fill = GridBagConstraints.HORIZONTAL;gbc.ipady = 20;gbc.gridx = 0;gbc.gridy = 1;this.add(new Button("Button 3"), gbc);gbc.gridx = 1;gbc.gridy = 1;this.add(new Button("Button 4"), gbc);gbc.gridx = 0;gbc.gridy = 2;gbc.fill = GridBagConstraints.HORIZONTAL;gbc.gridwidth = 2;this.add(new Button("Button 5"), gbc);} }

    輸出量

    4.卡布局

    這是很少使用的布局,用于將組件彼此堆疊。 CardLayout允許組件保持彼此CardLayout ,并根據(jù)需要將任何組件切換到最前面。 用圖片描述卡片的布局是沒有意義的。 因此,讓我們通過一個小例子來理解它。 考慮下面的代碼:

    mychoice.addItemListener(new ItemListener() {public void itemStateChanged(ItemEvent e) {CardLayout cardLayout = (CardLayout)(e.getTarget().getParent().getLayout());cardLayout.show(panel, (String)e.getItem());}});

    上面的代碼與帶有組合框的事件偵聽器相關(guān)聯(lián)。 根據(jù)組合值的變化,將顯示該組件。 為了創(chuàng)建卡片布局,您可以使用以下代碼

    CardLayoutExample.java package com.mkyong;import java.awt.Button; import java.awt.CardLayout; import javax.swing.JFrame;public class CardLayoutExample extends JFrame {public static void main(String[] args) {CardLayoutExample a = new CardLayoutExample();}public CardLayoutExample() {setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);CardLayout g = new CardLayout();setLayout(g);setTitle("Card Layout");setSize(300, 300);add(new Button("Button 1"));add(new Button("Button 2"));add(new Button("Button 3"));add(new Button("Button 4"));add(new Button("Button 5"));add(new Button("Button 6"));} }

    輸出量

    5.流程布局

    顧名思義, FlowLayout是一種布局,它允許組件流到可見部分的末端。 FlowLayout基本上有助于開發(fā)響應(yīng)速度更快的UI,并使組件保持自由流動的方式。 下圖顯示了具有6個組件的實(shí)際流布局。

    由于這是框架或面板的默認(rèn)布局,因此也可以在不顯式應(yīng)用布局的情況下工作。

    FlowLayoutExample.java package com.mkyong;import java.awt.Button; import java.awt.FlowLayout; import javax.swing.JFrame;public class FlowLayoutExample extends JFrame {public static void main(String[] args) {FlowLayoutExample a = new FlowLayoutExample();}public FlowLayoutExample() {setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);FlowLayout g = new FlowLayout();setLayout(g);setTitle("Flow Layout");setSize(300, 300);add(new Button("Button 1"));add(new Button("Button 2"));add(new Button("Button 3"));add(new Button("Button 4"));add(new Button("Button 5"));add(new Button("Button 6"));} }

    輸出量

    結(jié)論

    本文討論了可用的AWT布局。 討論已經(jīng)非常簡短地進(jìn)行了。 因此,參考文獻(xiàn)中提供了一些鏈接,它們可能會幫助您了解更多信息。

    參考文獻(xiàn)

  • 布局細(xì)節(jié)
  • 使用邊框布局
  • 使用卡布局
  • 使用流程布局
  • 使用GridBag布局
  • 使用GridBag布局
  • 標(biāo)簽: awt 布局

    翻譯自: https://mkyong.com/awt/java-awt-layouts/

    總結(jié)

    以上是生活随笔為你收集整理的Java AWT布局的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。