简体   繁体   中英

Why should I use java containers?

  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..!

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.

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"

最好使用JPanel ,创建一个Jpanel并将元素( JLabelJText )添加到其中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM