1

I've a few questions:

First, and most importantly, why can you run a GUI program with a frame, a couple panels, buttons, etc. several times - without changing anything - and half the time components are displayed in the frame, and the other half of the time nothing is displayed... literally, I change nothing in the code and it's the most frustrating problem to have. Sometimes stuff appears, sometimes nothing. I'm using Eclipse and just have the below code in a main method.

Secondly, can someone please explain clearly how Frames, Panels, and Layouts work?

JFrame f = new JFrame();
f.setSize(400, 400);
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);

JPanel p = new JPanel();
JButton alpha = new JButton();
JButton bravo = new JButton();
alpha.setLabel("Alpha");
bravo.setLabel("Bravo");
p.add(alpha, BorderLayout.WEST);
p.add(bravo, BorderLayout.EAST);
p.add(new JLabel("Charlie"), BorderLayout.SOUTH);
f.add(p);

The above sometimes works, and when it does it's not laying out how I understand from Oracle pages and tutorials. I create a frame (with no default layout), create a panel with two buttons that should be next to each other (WEST, EAST), then add a label to the bottom of the panel - and that whole panel is added to the frame. However they are all added next to each other at the top of the frame, centered.

Can you only have one component per panel? Can you add multiple panels to a frame, and if so do they overlap?

FYI I have watched YouTube videos and read a few other posts here, nothing seems to explain the basics simply and thoroughly, so I thought a forum post would get better explanations.

Thanks

1 Answers1

5

Sometimes stuff appears, sometimes nothing

Simply call f.setVisible(true); in the end after adding all the components.


Some Points:

  1. Use SwingUtilities.invokeLater() to make sure that EDT is initialized properly.

    Read more

  2. Call f.pack() instead of using f.setSize(400, 400).

  3. JFrame by default use BorderLayout so there is no need to set it again.


I have watched YouTube videos and read a few other posts here, nothing seems to explain the basics simply and thoroughly

Please have a look at the below sections of The Swing Tutorial for better understanding along of JFrame and Layout with detailed example:

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Wow that fixes the most annoying problem - thank you! Thought you could do it just at frame setup. Can you please explain a bit about layouts? Why can't I add the label to the bottom of the panel (when I add panel to frame and run, they're all lined up in NORTH) – user3397788 Jun 20 '14 at 21:56
  • Please have a look at [How to Use Various Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html) for better understanding of the layout and your answer is hidden somewhere in this tutorial. :) – Braj Jun 20 '14 at 21:59
  • Alright will do, I'll make a separate post if I have more questions (since first half is resolved). Thanks – user3397788 Jun 20 '14 at 22:00
  • Practice makes a man perfect. So keep coding... meet you soon. – Braj Jun 20 '14 at 22:00