문제

How to add a JButton into the center of a JFrame with BorderLayout()? I tried using BorderLayout.CENTER, but instead of the center of the screen, it gave the top-center of the screen. Or do I have to use another layout manager?

도움이 되었습니까?

해결책

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.

Animated image of nested layout example showing size change

다른 팁

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.

try this

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

JButton btnNewButton = new JButton("New button");
frame.getContentPane().add(btnNewButton, BorderLayout.CENTER);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top