使用BorderLayout将JButton放在屏幕的中心

时间:2013-05-24 04:13:22

标签: java swing layout-manager border-layout

如何使用BorderLayout() JButton 添加到JFrame的中心?我尝试使用BorderLayout.CENTER,但不是屏幕的中心,它给出了屏幕的顶部中心。或者我是否必须使用其他布局管理器?

3 个答案:

答案 0 :(得分:5)

JPanel中添加CENTER,并将布局设置为GridBagLayoutBoxLayout,如this answerSet component at center of the page所示。

GridBagLayout用于对包含Nested Layout Example中显示的黄色/红色渐变图像的标签进行居中。

Animated image of nested layout example showing size change

答案 1 :(得分:1)

学习可能需要一些时间,但SpringLayout值得研究。它可以让您在GUI上定位您想要的元素。您可以查看here以获取不同布局的示例。

答案 2 :(得分:0)

试试这个

frame.getContentPane().setLayout(new BorderLayout(0, 0));

JButton btnNewButton = new JButton("New button");
frame.getContentPane().add(btnNewButton, BorderLayout.CENTER);
相关问题