📜  Java AWT |边框布局类

📅  最后修改于: 2022-05-13 01:54:35.183000             🧑  作者: Mango

Java AWT |边框布局类

BorderLayout 是 JFrame、JWindow、JDialog、JInternalFrame 等窗口对象的默认布局。BorderLayout 将组件排列在五个区域中。四个边被称为北,南,东,西。中间部分称为中心。每个区域只能包含一个组件,并由相应的常数标识为NORTHSOUTHEASTWESTCENTER

构造函数:

  1. BorderLayout():它将构造一个新的边框布局,组件之间没有间隙。
  2. BorderLayout(int, int):它将构造一个边框布局,组件之间具有指定的间隙。

常用方法:

  1. toString() :返回一个表示边框布局状态的字符串。
  2. getLayoutAlignmentX(Container parent) :返回沿 X 轴的布局对齐方式。
  3. getLayoutAlignmentY(Container parent) :它将返回沿 Y 轴的布局对齐方式。
  4. removeLayoutComponent(Component comp) :该方法用于从边框布局中移除指定的组件。
  5. getVgap() :返回组件之间的垂直间隙。
  6. getHgap() :返回组件之间的水平间隙。
  7. setHgap(int hgap) :用于设置组件之间的水平间隙。
  8. setVgap(int vgap) :用于设置组件之间的垂直间隙。

下面的程序将说明 BorderLayout 类:

  • 程序1:下面的程序在一个JFrame中创建JButton组件,它的实例类是“ BorderLayoutDemo ”。我们创建 5 个 JButton,然后使用add()方法将它们添加到JFrame 。我们将分别使用setSize()setVisible()方法设置框架的大小和可见性。使用setLayout()方法设置布局。
Java
// Java program to illustrate the BorderLayout
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
// class extends JFrame
class BoderLayoutDemo extends JFrame {
 
    BoderLayoutDemo()
    {
         
        // Creating Object of Jpanel class
        JPanel pa = new JPanel();
 
        // set the layout
        pa.setLayout(new BorderLayout());
 
        // add a new JButton with name "wel" and it is
        // lie top of the container
        pa.add(new JButton("WelCome"), BorderLayout.NORTH);
 
        // add a new JButton with name "come" and it is
        // lie button of the container
        pa.add(new JButton("Geeks"), BorderLayout.SOUTH);
 
        // add a new JButton with name "Layout" and it is
        // lie left of the container
        pa.add(new JButton("Layout"), BorderLayout.EAST);
 
        // add a new JButton with name "Border" and it is
        // lie right of the container
        pa.add(new JButton("Border"), BorderLayout.WEST);
 
        // add a new JButton with name "hello everybody" and it is
        // lie center of the container
        pa.add(new JButton("GeeksforGeeks"), BorderLayout.CENTER);
 
        // add the pa object which refer to the Jpanel
        add(pa);
 
        // Function to close the operation of JFrame.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // Function to set size of JFrame.
        setSize(300, 300);
 
        // Function to set visible status of JFrame.
        setVisible(true);
    }
}
 
class MainFrame {
 
    // Driver code
    public static void main(String[] args)
    {
         
        // calling the constructor
        new BoderLayoutDemo();
    }
}


Java
// Java program to illustrate the BorderLayout
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Button;
import java.awt.Color;
  
// class extends JFrame
public class BorderDemo extends JFrame {
  
    // Constructor of BorderDemo class.
    public BorderDemo()
    {
  
        // set the layout
        setLayout(new BorderLayout());
  
        // set the background
        setBackground(Color.red);
  
        // creates Button (btn1)
        Button btn1 = new Button("Geeks");
  
        // creates Button (btn2)
        Button btn2 = new Button("GFG");
  
        // creates Button (btn3)
        Button btn3 = new Button("Sudo Placement");
  
        // creates Button (btn4)
        Button btn4 = new Button("GeeksforGeeks");
  
        // creates Button (btn5)
        Button btn5 = new Button("Java");
  
        // Adding JButton "btn1" on JFrame.
        add(btn1, "North");
  
        // Adding JButton "btn2" on JFrame.
        add(btn2, "South");
  
        // Adding JButton "btn3" on JFrame.
        add(btn3, "East");
  
        // Adding JButton "btn4" on JFrame.
        add(btn4, "West");
  
        // Adding JButton "btn5" on JFrame.
        add(btn5, "Center");
  
        // function to set the title
        setTitle("Learning a Border Layout");
  
        // Function to set size of JFrame.
        setSize(350, 300);
  
        // Function to set visible status of JFrame
        setVisible(true);
    }
  
    // Main Method
    public static void main(String args[])
    {
  
        // calling the constructor
        new BorderDemo();
    }
}


输出:

  • 程序 2:该程序将展示如何在 BorderLayout 中传递参数。使用setBackground()方法设置背景颜色。我们创建了 5 个 JButton 组件,分别命名为“ btn1 ”、“ btn2 ”、“ btn3 ”、“ btn4 ”、“ btn5 ”,然后使用add()方法将它们添加到 JFrame 中。我们分别使用setTitle()setSize()setVisible()方法设置框架的标题、大小和可见性。布局由方法setLayout()设置。

Java

// Java program to illustrate the BorderLayout
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Button;
import java.awt.Color;
  
// class extends JFrame
public class BorderDemo extends JFrame {
  
    // Constructor of BorderDemo class.
    public BorderDemo()
    {
  
        // set the layout
        setLayout(new BorderLayout());
  
        // set the background
        setBackground(Color.red);
  
        // creates Button (btn1)
        Button btn1 = new Button("Geeks");
  
        // creates Button (btn2)
        Button btn2 = new Button("GFG");
  
        // creates Button (btn3)
        Button btn3 = new Button("Sudo Placement");
  
        // creates Button (btn4)
        Button btn4 = new Button("GeeksforGeeks");
  
        // creates Button (btn5)
        Button btn5 = new Button("Java");
  
        // Adding JButton "btn1" on JFrame.
        add(btn1, "North");
  
        // Adding JButton "btn2" on JFrame.
        add(btn2, "South");
  
        // Adding JButton "btn3" on JFrame.
        add(btn3, "East");
  
        // Adding JButton "btn4" on JFrame.
        add(btn4, "West");
  
        // Adding JButton "btn5" on JFrame.
        add(btn5, "Center");
  
        // function to set the title
        setTitle("Learning a Border Layout");
  
        // Function to set size of JFrame.
        setSize(350, 300);
  
        // Function to set visible status of JFrame
        setVisible(true);
    }
  
    // Main Method
    public static void main(String args[])
    {
  
        // calling the constructor
        new BorderDemo();
    }
}

输出:

注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。

参考: https: Java/awt/BorderLayout.html