简体   繁体   English

显示 JFrame 后如何执行某些操作

[英]How to execute some action after the JFrame are displayed

I have this GUI application, and I want to execute a custom action inmediatelly when the user open the application, but after the GUI are shown.我有这个 GUI 应用程序,我想在用户打开应用程序时立即执行自定义操作,但在显示 GUI 之后。

So, I put the call to the action into the public Main() of the JFrame like this:所以,我把对动作的调用放到 JFrame 的public Main()中,如下所示:

public Main() {
    initComponents();
    ExecuteAfter();
}

Where ExecuteAfter() is the method that contain the acction, or actions to execute.其中ExecuteAfter()是包含要执行的操作或操作的方法。

This works fine, but not in the way I want.这工作正常,但不是我想要的方式。 This way, the action executes allways before the JFrame are displayed, that is before the aplication windows appear in the screen.这样,动作总是在JFrame显示之前执行,也就是在应用程序窗口出现在屏幕上之前。 What I want is that execute the action only after the JFrame are displayed, that is after the aplication windows appear in the screen.我想要的是仅在显示JFrame之后执行操作,即在应用程序窗口出现在屏幕上之后。

I tried put the call into the public static void main(String args[]) because there's where the JFrame is created and displayed.我尝试将调用放入public static void main(String args[])因为在那里创建和显示JFrame But doesn't work because the method isn't static, and I can't put static that method because it use some components of the JFrame that are already initialized non-static by the IDE.但不起作用,因为该方法不是静态的,而且我不能将该方法JFrame静态,因为它使用了JFrame一些组件,这些组件已经由 IDE 初始化为非静态的。

So, the question is: Where I need to put the call for the action can be executed after the JFrame are displayed on the screen?那么,问题来了: JFrame显示在屏幕上后,我需要把调用的动作放在哪里才能执行? Or there's other way of doing that?或者有其他方法可以做到这一点?

Thanks in advance!提前致谢!

I used a WindowListener and solved the problem.我使用了WindowListener并解决了这个问题。

Instead of put the call in the constructor public Main() or in the main public static void main(String args[]) which cannot be done, I configured a WindowsListener for do the call.我没有将调用放在无法完成的构造函数public Main()或 main public static void main(String args[])中,而是配置了一个WindowsListener来进行调用。 Like that:像那样:

private void formWindowOpened(java.awt.event.WindowEvent evt) {
    ExecuteAfter();
}

And works perfectly in the way I want.并且以我想要的方式完美运行。

Thanks @MadProgrammer for the tip.感谢@MadProgrammer 的提示。

If i understand you question, I use a similar case for my project.如果我理解您的问题,我会在我的项目中使用类似的案例。 I was needed to start timer when JFrame show, so this is how i do that.当 JFrame 显示时,我需要启动计时器,所以我就是这样做的。

So i use 2 methods and 1 constructor.所以我使用了 2 个方法和 1 个构造函数。 First method (exp. Name: prepare GUI), here you can add all thinks what you need to create JFrame, JPanel....and that method I call in constructor.第一种方法(exp。名称:prepare GUI),在这里你可以添加所有你需要的东西来创建JFrame,JPanel......以及我在构造函数中调用的那个方法。 In second method (exp. Name: start GUI) you will add all components to JPanel/s, JPanel/s to JFrame, and set JFrame visible = (true) and then add your method ExecuteAfter().在第二种方法(exp。名称:启动 GUI)中,您将所有组件添加到 JPanel/s,将 JPanel/s 添加到 JFrame,并设置 JFrame visible = (true),然后添加您的方法 ExecuteAfter()。 That second method(prepare GUI) you need to call in main method.您需要在主方法中调用的第二种方法(准备 GUI)。 I hope that's will help you.我希望这会帮助你。

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

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