This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Can't close window 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 » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't close window

Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
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

Joined: Aug 26, 2000
Posts: 1242
"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

Joined: Nov 06, 2000
Posts: 1510
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


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
Thanks Fei and Bosun, I very much appreciate your help. I understand now.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can't close window
 
Similar Threads
Can't close program
NullPointerException in my First AWT program
Compiler Problems with WindowListeners
open new window
Register Event Listener to Button?