Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

labels

 
josephine chen
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic