• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't close window

 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code compiles, but when run buttons don't show up until window is maximized, then I can't close window, Help!!!
import java.awt.*;
class MyButton {
MyButton() {
Frame f = new Frame("Top Teams in the NFL");
f.setLayout(new FlowLayout());
f.setVisible(true);
f.setSize(500,300);
f.setBackground(Color.darkGray);
Button a = new Button("Raiders");
Button b = new Button("Ravens");
Button c = new Button("Rams");
Button d = new Button("Steelers");
Button e = new Button("Niners");
Button g = new Button("Bears");
f.add(a);
f.add(b);
f.add(c);
f.add(d);
f.add(e);
f.add(g);
}

public static void main(String args[])
{ MyButton mf = new MyButton(); }
}
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"This code compiles, but when run buttons don't show up until window is maximized, then I can't close window, Help!!!"
You can't close the window becuase you didn't add the windowlistener. Try adding one.
doesn't show the buttons, try adding the buttons to the frame first then show the Frame.
hope this help.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fei is correct, f.setVisible(true) should be the last statement after adding all your GUI components. You also have to add a window listener that will either minimize/close the window or exit the application. There is probably an example somewhere in your textbook.

------------------
Bosun
SCJP for the Java� 2 Platform
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fei and Bosun, I very much appreciate your help. I understand now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic