Java GUI application frame does not display correctly on startup?

I made this TicTacToe application using Java Swing library. Since I added the menu, it doesn't start as expected. I mean the functionality is fine, but it showed up sometimes as one of three unwanted methods I have in the image when I run it. However, as soon as I maximize and minimize the frame, it will display as desired.

Please help me fix this.

enter image description here


source to share

3 answers

You add components to the frame as soon as it is visible. Call frame.setVisible(true);

only after you have added all the components, or you will have to retest the container. Once the container is visible and laid out, you must call validate

/ revalidate

if you add or remove components.



A frame is shown before all elements are added.

public void setUpFrame() {





just move frame.setVisible(true);

to the end of the method setUpFrame()


I know I'm already answering your question, but I made this answer because I saw that if I click on an exit menu item, it "closes" the window, it stays open in the background.

if you want to close the program completely, use System.exit(0);

instead frame.setVisible(false);

initem_exit ActionListener

good game and keep programming;)



All Articles