2
  Container c = this.getContentPane();
  JLabel lbl = new JLabel("Label");
  c.add(lbl);

what is the difference between using the above method & this one ? knowing that both give same results

  JLabel lbl = new JLabel("Label");
  add(lbl);

& if I need to put many items in the frame I can just make many Panels and add them all to one mainpanel, I don't see any need for containers, sorry for such a basic question

I know no one does GUI by code now, but it is for studying purposes so..!

n0krashy
  • 51
  • 7

3 Answers3

2

Sometimes it's nice to segment your stuff into containers for manageability. It's not required though. Other than dynamic components, very little GUI building is done in code anymore. As recommended by others, use a GUI builder for your primary work. If necessary you can drop into code, but I find that to be a pretty uncommon need.

Brian Knoblauch
  • 20,639
  • 15
  • 57
  • 92
  • i know it is just for studying purposes so I wanted to know why should one use containers as i find them completely un-necessary – n0krashy May 24 '16 at 13:24
  • @n0krashy It is all for code manageability (changeability) and readability - in short experienced programmers are often structuring their code to make their lifes easier when it will come to understund and change this code in the future. But in your case there are other and possibly better ways to accomplish this rather than using containers. – Krzysztof Cichocki May 29 '16 at 17:00
1

You don't need to use Containers. Preferably use WindowBuilder to construct some GUI in eclipse, or Matisse in Netbeans, they are pretty good WYSWIG UI design tool. Try them. It would also be good for you to read some books about UI programming, especially about Swing if you plan to use it. I can advice the book "Filthy Rich Clients: Developing Animated and Graphical Effects for Desktop Java Applications"

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
0

It is good to use JPanels, create a Jpanel and add your elements( JLabel, JText) into it.

adranale
  • 2,835
  • 1
  • 21
  • 39
PeaceIsPearl
  • 329
  • 2
  • 6
  • 19