BorderLayout是窗口对象(如JFrame、JWindow、JDialog、JInternalFrame等)的默认布局。BorderLayout在五个区域中排列组件。四个边被称为北、南、东和西。中间部分称为中心。每个区域只能包含一个组件,并由相应的常数标识为 北 , 南方 , 东 , 西 和 居中 .
null
施工人员:
- BorderLayout(): 它将构建一个新的边界布局,组件之间没有间隙。
- 边框布局(int,int): 它将根据组件之间的指定间隙构建边框布局。
常用方法:
- toString() :返回表示边框布局状态的字符串。
- getLayoutAlignmentX(容器父级) :返回沿X轴的布局对齐方式。
- getLayoutAlignmentY(容器父级) :它将返回沿Y轴的布局对齐。
- 拆卸布局部件(部件组件) :此方法用于从borderlayout中删除指定的组件。
- getVgap() :返回组件之间的垂直间隙。
- getHgap() :返回组件之间的水平间隙。
- setHgap(int hgap) :用于设置组件之间的水平间隙。
- setVgap(int vgap) :用于设置组件之间的垂直间隙。
下面的程序将演示BorderLayout类:
- 项目1: 下面的程序在 窗口 ,其实例类为“ BorderLayoutDemo ”. 我们创建5个JButton,然后将它们添加到 窗口 通过使用 添加() 方法我们将使用 设置大小() 和 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(); } } |
输出:
- 项目2: 这个程序将演示如何在BorderLayout中传递参数。使用设置背景色 挫折 方法我们创建了5个JButton组件,名为“ btn1 “, “ btn2 “, “ btn3 “, “ btn4 “, “ btn5 ,然后使用 添加() 方法我们使用 setTitle() , 设置大小() 和 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://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END