按照BorderLayout布局方式,在JPanel的五个区中各加载一个按钮 。
时间: 2024-02-09 20:09:50 浏览: 42
好的,以下是实现这个需求的Java代码:
```java
import javax.swing.*;
import java.awt.*;
public class BorderLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout Example");
JPanel panel = new JPanel(new BorderLayout());
JButton northButton = new JButton("North");
JButton southButton = new JButton("South");
JButton eastButton = new JButton("East");
JButton westButton = new JButton("West");
JButton centerButton = new JButton("Center");
panel.add(northButton, BorderLayout.NORTH);
panel.add(southButton, BorderLayout.SOUTH);
panel.add(eastButton, BorderLayout.EAST);
panel.add(westButton, BorderLayout.WEST);
panel.add(centerButton, BorderLayout.CENTER);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
```
运行这个程序,你会看到一个窗口,其中包含了一个JPanel,这个JPanel中有五个按钮,分别位于North、South、East、West和Center区域。这就是按照BorderLayout布局方式在JPanel的五个区中加载一个按钮的实现。
阅读全文