wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Label 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 "Label " Watch "Label " New topic
Author

Label

josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
<pre>
import java.awt.*;
import java.awt.event.*;
class btn extends Button implements ActionListener{
Frame w;
static btn bt = new btn();

void addFrame(){
w = new Frame();
w.setSize(200,200);
w.setVisible(true);
}
public static void main(String s[]){
bt.addFrame();
Button b = new Button("ok");
b.addActionListener(bt);
bt.w.add(b);
bt.w.setLayout(new FlowLayout());

}
public void actionPerformed(ActionEvent e){
Label l = new Label("asd");
l.setText("hai");

/* bt.w.setLayout(new FlowLayout());
l.setBackground(Color.blue);
bt.w.add(l); */
}
}
</pre>
I have just started experimenting on awt and help me with this one.
I want to display the label at the center whenever i click the button.I am getting a runtime error which is a very long error
A long as i comment the above lines i have no error but
when i uncomment those lines i get the error.What is my mistake ???
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I am moving this thread to the Swing/AWT section.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Shan- I get no error when I run the code after uncommenting the setLayout() and setBackground(). I tried it on jdk 1.2.2 and 1.3 beta for Windows; I see no error. If you are still getting an error, you need to find out exactly what the error message says - especially the first few lines. If it's too long to see (an annoying problem when running Java from a DOS prompt) you need to make the window as large as possible. For Windows 98, right-click on the MS-DOS logo in the upper left-hand corner, then select Properties, select the Screen tab, and set "Initial size" to the largest value you can. For other systems, I guess you'll have to experiment.
Another possibility is to trap and print the error from within your program - it should be much shorter then. Just put a try/catch block inside the actionPerformed() method:
<code><pre> public void actionPerformed(ActionEvent e) {
try {
...
}
catch (Exception e) {
System.err.println(e);
e.printStackTrace();
}
}</pre></code>
Then you should be able to see exactly what it says.
One other problem I see - after you setBackground() and add() the label, you need to tell the system to redraw your panel so we can see what's changed. I usually use validate() for this, although setVisible(true) works also.
<code><pre> bt.w.validate();</pre></code>
should probably be the last line in your actionPerformed() method (or, the last line in the try block).


"I'm not back." - Bill Harding, Twister
 
 
subject: Label
 
Similar Threads
Button
labels
Resizing my frame
Layouts
Having problem w/ Head First Java (the very first example given on page 3)