• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Label

 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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 ???
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am moving this thread to the Swing/AWT section.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic