Java – use the layout to set the panel in the center of the screen
•
Java
I try to set the position of the child panel in the center of the parent panel by using
parent_panel.setLayout(new BorderLayout()); parent_panel.add(child_panel,BorderLayout.CENTER);
But it will be added in the middle of the horizontal screen, but vertically at the top
How do I add it vertically and horizontally to the center of the screen? What do I need to do?
Solution
If I understand correctly, you want an interface like this:
+-------- Parent panel --------+ | | | | | +--- Child panel ----+ | | | | | | | | | | | | | | | | | | +--------------------+ | | | | | +------------------------------+
... and you have not added other components to the parent panel
If this is the case, you have two choices I know (based on this question, I have obviously answered):
>Use gridbaglayout with an empty GridBagConstraints object as follows:
parent_panel.setLayout(new GridBagLayout()); parent_panel.add(child_panel,new GridBagConstraints());
>Use @ r_ 866_ 2419@Layout , as follows:
parent_panel.setLayout(new @R_866_2419@Layout(parent_panel,@R_866_2419@Layout.PAGE_AXIS)); @R_866_2419@ horizontal@R_866_2419@ = @R_866_2419@.createHorizontal@R_866_2419@(); horizontal@R_866_2419@.add(@R_866_2419@.createHorizontalGlue()); horizontal@R_866_2419@.add(child_panel); horizontal@R_866_2419@.add(@R_866_2419@.createHorizontalGlue()); @R_866_2419@ vertical@R_866_2419@ = @R_866_2419@.createVertical@R_866_2419@(); vertical@R_866_2419@.add(@R_866_2419@.createVerticalGlue()); vertical@R_866_2419@.add(horizontal@R_866_2419@); // one inside the other vertical@R_866_2419@.add(@R_866_2419@.createVerticalGlue());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码