Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Label " Watch "Label " New topic


josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
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();
public static void main(String s[]){
Button b = new Button("ok");
bt.w.setLayout(new FlowLayout());

public void actionPerformed(ActionEvent e){
Label l = new Label("asd");

/* bt.w.setLayout(new FlowLayout());
bt.w.add(l); */
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

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

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

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) {
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
I agree. Here's the link:
subject: Label
It's not a secret anymore!