This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic


josephine chen
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this thread to the Swing/AWT section.
Jim Yingst
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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) {
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).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!