Swing - BorderLayout 類



簡介

BorderLayout 類將元件排列到五個區域:東、西、北、南和中。每個區域只能包含一個元件,每個區域中的每個元件都由相應的常量 NORTH、SOUTH、EAST、WEST 和 CENTER 標識。

類宣告

以下是 java.awt.BorderLayout 類的宣告:

public class BorderLayout
   extends Object
      implements LayoutManager2, Serializable

欄位

以下是 java.awt.BorderLayout 類的欄位:

  • static String AFTER_LAST_LINE - PAGE_END 的同義詞。

  • static String AFTER_LINE_ENDS - LINE_END 的同義詞。

  • static String BEFORE_FIRST_LINE - PAGE_START 的同義詞。

  • static String BEFORE_LINE_BEGINS - LINE_START 的同義詞。

  • static String CENTER - 中心佈局約束(容器的中間)。

  • static String EAST - 東部佈局約束(容器的右側)。

  • static String LINE_END - 元件位於佈局的行方向末尾。

  • static String LINE_START - 元件位於佈局的行方向開頭。

  • static String NORTH - 北部佈局約束(容器的頂部)。

  • static String PAGE_END - 元件位於佈局內容的最後一行之後。

  • static String PAGE_START - 元件位於佈局內容的第一行之前。

  • static String SOUTH - 南部佈局約束(容器的底部)。

  • static String WEST - 西部佈局約束(容器的左側)。

類建構函式

序號 建構函式及描述
1

BorderLayout()

構造一個新的邊界佈局,元件之間沒有間隙。

2

BorderLayout(int hgap, int vgap)

構造一個具有指定元件間隙的邊界佈局。

類方法

序號 方法及描述
1

void addLayoutComponent(Component comp, Object constraints)

使用指定的約束物件將指定的元件新增到佈局。

2

void addLayoutComponent(String name, Component comp)

如果佈局管理器使用每個元件的字串,則將元件 comp 新增到佈局,並將其與 name 指定的字串關聯。

3

int getHgap()

返回元件之間的水平間隙。

4

float getLayoutAlignmentX(Container parent)

返回沿 x 軸的對齊方式。

5

float getLayoutAlignmentY(Container parent)

返回沿 y 軸的對齊方式。

6

int getVgap()

返回元件之間的垂直間隙。

7

void invalidateLayout(Container target)

使佈局無效,表明如果佈局管理器快取了資訊,則應將其丟棄。

8

void layoutContainer(Container target)

使用此邊界佈局佈置容器引數。

9

Dimension maximumLayoutSize(Container target)

給定指定目標容器中的元件,返回此佈局的最大尺寸。

10

Dimension minimumLayoutSize(Container target)

使用此佈局管理器確定目標容器的最小大小。

11

Dimension preferredLayoutSize(Container target)

根據容器中的元件,使用此佈局管理器確定目標容器的首選大小。

12

void removeLayoutComponent(Component comp)

從此邊界佈局中移除指定的元件。

13

void setHgap(int hgap)

設定元件之間的水平間隙。

14

void setVgap(int vgap)

設定元件之間的垂直間隙。

15

String toString()

返回此邊界佈局狀態的字串表示形式。

繼承的方法

此類繼承自以下類:

  • java.lang.Object

BorderLayout 示例

使用您選擇的任何編輯器建立以下 Java 程式,例如在 D:/ > SWING > com > tutorialspoint > gui >

SwingLayoutDemo.java

package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingLayoutDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   private JLabel msglabel;

   public SwingLayoutDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();  
      swingLayoutDemo.showBorderLayoutDemo();       
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java SWING Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      headerLabel = new JLabel("",JLabel.CENTER );
      statusLabel = new JLabel("",JLabel.CENTER);        
      statusLabel.setSize(350,100);
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showBorderLayoutDemo(){
      headerLabel.setText("Layout in action: BorderLayout");      

      JPanel panel = new JPanel();
      panel.setBackground(Color.darkGray);
      panel.setSize(300,300);
      BorderLayout layout = new BorderLayout();
      layout.setHgap(10);
      layout.setVgap(10);
      
      panel.setLayout(layout);        
      panel.add(new JButton("Center"),BorderLayout.CENTER);
      panel.add(new JButton("Line Start"),BorderLayout.LINE_START); 
      panel.add(new JButton("Line End"),BorderLayout.LINE_END);
      panel.add(new JButton("East"),BorderLayout.EAST);   
      panel.add(new JButton("West"),BorderLayout.WEST); 
      panel.add(new JButton("North"),BorderLayout.NORTH); 
      panel.add(new JButton("South"),BorderLayout.SOUTH); 

      controlPanel.add(panel);
      mainFrame.setVisible(true);  
   }
}

使用命令提示符編譯程式。轉到 D:/ > SWING 並鍵入以下命令。

D:\SWING>javac com\tutorialspoint\gui\SwingLayoutDemo.java

如果未出現錯誤,則表示編譯成功。使用以下命令執行程式。

D:\SWING>java com.tutorialspoint.gui.SwingLayoutDemo

驗證以下輸出。

SWING BorderLayout
swing_layouts.htm
廣告