Put a JButton in the center of the screen with BorderLayout

孤街浪徒 提交于 2019-11-27 16:25:13
Andrew Thompson

Put a JPanel in the CENTER and set the layout to GridBagLayout or BoxLayout as seen in this answer to Set component at center of the page.

The GridBagLayout is used to center a label containing the yellow/red gradient image seen in the Nested Layout Example.

It may take some time to learn, but SpringLayout is worth looking into. It will let you position elements on the GUI where you wish. You can look here for examples of different layouts.

Sanjaya Liyanage

try this

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

JButton btnNewButton = new JButton("New button");
frame.getContentPane().add(btnNewButton, BorderLayout.CENTER);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!