wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes labels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "labels" Watch "labels" New topic
Author

labels

josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
<pre>
import java.awt.*;
import java.awt.event.*;
class btnev extends Button implements ActionListener{
static Frame w = new Frame();;
static btnev bt = new btnev();
static Label l = new Label();
void addframe(){
w.setSize(200,200);
w.setVisible(true);
}
public static void main(String s[]){
Button b = new Button("ok");
b.addActionListener(bt);
bt.w.add(b);
bt.w.setLayout(new FlowLayout());
w.setLayout(new FlowLayout(FlowLayout.CENTER));
w.add(l);
bt.addframe();
}
public void actionPerformed(ActionEvent e){
if ((l.getText())== "hello")
l.setText("hai");
else
l.setText("hello");

}
}
</pre>

I am able to display the label but i want the label to be in the center of the frame but it always aligns near the button.How to
get it to the center of the frame???
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You will need to use some layout manager other than FlowLayout, which will always try to put components next to each other unless they simply won't fit on a line. The simplest alternative is probably BorderLayout. Add the OK button to the North region, and add the label to the Center region. If that's not what you want, you may also want to look at BoxLayout or GridBagLayout, which are more powerful and more complicated (especially GridBagLayout - it can do mast anything you want, but it's hardest to learn.) Spend some time learning about each layout - they each have their own special tricks.


"I'm not back." - Bill Harding, Twister
 
 
subject: labels
 
Similar Threads
Button
Resizing my frame
Label
Weird problem!!!!!!!!
swing help