Re: Java Swing
- From: "Bjorn Abelli" <bjorn_abelli@xxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 28 Apr 2005 22:02:32 +0200
"Nuno Silva" ...
> Hi, I'm pretty new to Swing, and I'm at the hello
> world example code from java.sun.com.
Your problem would be no different in AWT...
> I'm trying to add next to the JLabel a JButton,
> and next to frame.getContentPane().add(label);
> I do the same for the JButton created,
> but in the GUI only shows the button.
> Can you explain to me why this happens
> and why not appending the button next to
> the label?
The default LayoutManager in a Frame/JFrame is BorderLayout, that has 5
areas, in which only one component can exist at one time.
frame.getContentPane().add(label);
....is "equal" to:
frame.getContentPane().add(label, BorderLayout.CENTER);
So when you "add" the button, it actually *replaces* the label.
> And how should I do to
> make them put together?
There's a lot of possibilities.
You could start with changing the "adding" to actually use different areas,
e.g.
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.getContentPane().add(button, BorderLayout.SOUTH);
....or you could change the LayoutManager to something else than a
BorderLayout before adding the components:
frame.getContentPane().setLayout( new FlowLayout() );
frame.getContentPane().add(label);
frame.getContentPane().add(button);
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/BorderLayout.html
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/FlowLayout.html
BTW, if you're using the latest version (1.5), there's no longer a need for
the "getContentPane()", as you can use "add" directly on the JFrame (which
forwards it to the pane).
// Bjorn A
.
- Prev by Date: Cross tabulation - someone please help if you can
- Next by Date: inner-inner class referencing outer class's inherited method
- Previous by thread: Cross tabulation - someone please help if you can
- Next by thread: inner-inner class referencing outer class's inherited method
- Index(es):