使用borderlayout布局完成含有多个按钮的界面
时间: 2023-10-15 10:05:54 浏览: 108
下面是一个使用BorderLayout布局管理器的示例程序,其中包含多个按钮:
```
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BorderLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");
JButton btn3 = new JButton("Button 3");
JButton btn4 = new JButton("Button 4");
JButton btn5 = new JButton("Button 5");
panel.add(btn1, BorderLayout.NORTH);
panel.add(btn2, BorderLayout.WEST);
panel.add(btn3, BorderLayout.CENTER);
panel.add(btn4, BorderLayout.EAST);
panel.add(btn5, BorderLayout.SOUTH);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
```
在这个示例中,我们创建了一个JPanel,并使用BorderLayout布局管理器将多个按钮添加到该面板上。每个按钮都添加到面板的不同位置(NORTH、WEST、CENTER、EAST和SOUTH),以便它们在屏幕上呈现为一个网格。最后,我们将面板添加到JFrame的内容面板上,并显示该窗口。
阅读全文
相关推荐


















