BorderLayout
是 java.awt
包中的一个类,它是用于布局管理的。它把容器分成5个区域:NORTH、SOUTH、EAST、WEST和CENTER,每个区域只能包含一个组件。
以下是一个简单的使用 BorderLayout
的示例:
import java.awt.*;
import javax.swing.*;
public class BorderLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
JButton btn1 = new JButton("North");
JButton btn2 = new JButton("South");
JButton btn3 = new JButton("East");
JButton btn4 = new JButton("West");
JButton btn5 = new JButton("Center");
contentPane.add(btn1, BorderLayout.NORTH);
contentPane.add(btn2, BorderLayout.SOUTH);
contentPane.add(btn3, BorderLayout.EAST);
contentPane.add(btn4, BorderLayout.WEST);
contentPane.add(btn5, BorderLayout.CENTER);
frame.setVisible(true);
}
}
在这个例子中,我们创建了一个 JFrame
,然后设置了其内容面板的布局为 BorderLayout
。然后我们添加了5个按钮到不同的区域。最后,我们设置窗口可见。
The abstract class cannot create an object with the new operator, it must produce its subclass, which creates the object. If there is an abstract method in the class body of an abstract class, it can only be declared but not implemented. The subclass of the class must implement the abstract method, that is, override the abstract method of the parent class. An abstract class only cares about whether a subclass has a certain function, not about the specific implementation of the function. The specific implementation is the responsibility of the subclass.
import java.awt.*;
public class Javase_43 extends Frame
{
static Frame frm=new Frame("边界式布局管理器BorderLayout");
public static void main(String[] args)
{
BorderLayout border=new BorderLayout (5,10);
frm.setLayout(border); //设置页面布局为边界式布局方式
frm.setSize(280,200);
frm.add(new Button("上北"), BorderLayout.NORTH);
frm.add(new Button("下南"), BorderLayout. SOUTH);
frm.add(new Button("左西"), BorderLayout.WEST);
frm.add(new Button("右东"), BorderLayout.EAST);
frm.add(new Button("中央"), BorderLayout.CENTER);
frm.setVisible(true);
}
}