简体   繁体   English

启动时,Java GUI应用程序框架无法正确显示?

[英]Java GUI app frame doesn't display correctly when launched?

I have made this TicTacToe app using the Java Swing library. 我使用Java Swing库制作了这个TicTacToe应用程序。 Ever since I've added the menu, it wouldn't launch as expected. 自从我添加了菜单后,它就无法按预期启动。 I mean, the functionality is fine but it would sometimes display as one of the three undesirable methods I have in the image when I launch it. 我的意思是,功能很好,但它有时会显示为我启动它时图像中的三种不良方法之一。 However, once I maximize and minimize the frame, it would display in the desired manner. 但是,一旦我最大化并最小化帧,它将以所需的方式显示。

Kindly help me fix this. 请帮我解决这个问题。

在此输入图像描述

You are adding components to the frame once its already visible. 一旦已经可见,您将向框架添加组件。 Call frame.setVisible(true); 调用frame.setVisible(true); only after you have added all components or you will have to revalidate the container. 只有在添加完所有组件后,您才需要重新验证容器。 Once a container is visible and layed out, you have to call validate / revalidate if you add or remove components. 一旦容器可见并布局,如果添加或删除组件,则必须调用validate / revalidate

You are showing the frame before all the items are added 您在添加所有项目之前显示框架

public void setUpFrame() {
    ...

    frame.setVisible(true);
}

just move the frame.setVisible(true); 只需移动frame.setVisible(true); to the end of the setUpFrame() method :) setUpFrame()方法结束:)

i know that i already answer your question but i made this answer because i saw that if i press on the menu item of the exit, it "close" the window, it stays open in the background. 我知道我已经回答了你的问题,但我做了这个答案,因为我看到如果我按下退出的菜单项,它“关闭”窗口,它在后台保持打开状态。

if you want to close the program completly use System.exit(0); 如果要完全关闭程序,请使用System.exit(0); instead of frame.setVisible(false); 而不是frame.setVisible(false); in the item_exit ActionListener item_exit ActionListener

nice game and keep programming ;) 好游戏,继续编程;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM