0

Why doesn't uPanel3 appear in the center of the JFrame (aFrame) even though I am using BorderLayout?

 uPanel3.setLayout(new FlowLayout());
 uPanel3.add(background);
 aFrame.add(uPanel3,center.CENTER);
 //FlowLayout for uPanel2
 uPanel2.setLayout(new FlowLayout(FlowLayout.CENTER));
 uPanel2.add(errorLabel);
 aFrame.add(uPanel2,south.SOUTH);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • This is the end of the method there are lines of code ahead – Programmer_Novice Apr 23 '22 at 03:40
  • wouldn't borderlayout be a better choice in this case? – mindoverflow Apr 23 '22 at 03:42
  • you can also refer to this link: https://stackoverflow.com/questions/15858234/flowlayout-center-not-centering-the-components – mindoverflow Apr 23 '22 at 03:43
  • @mindoverflow I am using borderlayout on the third line, center is the object of borderlayout. My hopes are to use flowlayout to center uPanel3 on a line and borderlayout to place panel in the center of the JFrae – Programmer_Novice Apr 23 '22 at 03:56
  • then perhaps we could change ```center.CENTER``` to ```BorderLayout.CENTER``` – mindoverflow Apr 23 '22 at 03:57
  • we have to explicitly call CENTER from class, not instance object. – mindoverflow Apr 23 '22 at 03:58
  • @mindoverflow no dice. I just tried it that way, however when I put the value as WEST it appears on the same line as another FlowLayout. Which leads me to believe that it is hidden under another panel/label. Is there a way to skip a "skip a line" in a JFrame? – Programmer_Novice Apr 23 '22 at 04:04
  • we cannot skip lines, and we should definitely permanently remove or make invisible items that are on top of desired display layer. i am unable to assist further without actually running some given code. it's hard to visualize the problem as is. have you explicitly set sizes, what components are within said layouts, etc. swing layouts are quite tricky. – mindoverflow Apr 23 '22 at 04:10
  • 1
    Read the Swing tutorial on [How to Use BorderLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html). The CENTER does not mean that the panel will be centered on the frame. It means the panel will take all the space in the center of the frame. The FlowLayout always displays its components at the top of the total space available. If you actually want the panel to be centered in the frame then you can use a GridBagLayout as the layout manager for your frame. – camickr Apr 23 '22 at 14:11

0 Answers0