可以使用什么Java Swing事件来了解应用程序何时启动? [英] What Java Swing event can be used to know when application has finished starting?

查看:75
本文介绍了可以使用什么Java Swing事件来了解应用程序何时启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个地方来挂钩一些代码,以便在应用程序加载完成后以编程方式创建,调整JPanel的大小和位置。

I'm looking for a place to hook some code to programmatically create, size and position a JPanel after the application has finished loading.

我刚刚开始用Java。我正在使用带有jdk1.6.0_13的NetBeans 6.5.1。我已经使用新项目向导来创建基本的Java / Swing桌面应用程序。这是一个SingleFrameApplication,它使用带有中央主要JPanel的FrameView,其中放置了所有UI元素。

I'm just starting with Java. I'm using NetBeans 6.5.1 with jdk1.6.0_13. I've used the new project wizard to create a basic Java/Swing desktop application. This is a SingleFrameApplication that uses a FrameView with a central main JPanel where all the UI elements are placed.

我首先在FrameView构造函数中尝试了我的代码,但是当我尝试根据我添加到UI中的一个设计时间控件的边界矩形来安排我的JPanel,该控件尚未完成定位和调整大小,所以我得到坐标的全零。

I first tried my code in the FrameView constructor but when I try to arrange my JPanel based on the bounding rectangle of one of the design time controls I added to the UI, that control has not yet finished being positioned and sized so I'm getting all zeros for the coordinates.

我已经验证了我的代码在应用程序加载后通过单击事件调用它可以按预期工作,所以我的问题是找到一种方法来了解何时完成所有内容的大小和排列。

I've verified my code works as expected by calling it from a click event after the application has loaded so my problem is finding a way to know when everything is finished being sized and arranged.

我还尝试了来自主JPanel的componentShown事件,但我后来读到只有在显式调用setVisible时才触发,这显然在正常的应用程序启动过程中没有发生。

I also tried the componentShown event from the main JPanel but I later read that is only fired if setVisible is explicitly called which apparently doesn't happen during normal application startup.

任何人都可以提供一些指示吗?谢谢。

Can anyone provide some pointers? Thanks.

更新:

除了我在回答中提到的内容下面,我还阅读了Application.ready()方法。这也是知道应用程序的UI部分何时完成它需要做的所有事情的兴趣点。从应用程序传达我的观点似乎有点乱。

In addition to what I mention in my answer below, I also read about the Application.ready() method. This would also be a point in time of interest for knowing when the UI part of an application is finished doing everything it needs to do. Communicating to my view from the application seemed a bit messy though.

推荐答案

我使用的解决方案实际上是答案的组合来自Charles Marin和JRL(我赞成你的两个信用答案,谢谢)。

The solution I went with was actually a combination of the answers from Charles Marin and JRL (I upvoted both of your answers for credit, thanks).

我的FrameView类实现了WindowListener。

I had my FrameView class implement WindowListener.

...
public class MyView extends FrameView implements WindowListener
...

在我的FrameView构造函数中,我为应用程序的主框架添加了一个监听器。

and in my FrameView constructor I added a listener to the application's main frame.

...
getFrame().addWindowListener((WindowListener) this);
...

然后在我的windowActivated实现中我可以调用我必须的代码根据其他控件的位置和大小排列和控制主JPanel上的控件。

Then in my implementation of windowActivated I could call the code I had to arrange and size a control on the main JPanel based on the location and size of other controls.

public void windowActivated(WindowEvent e)
{
    // The application should now be finished doing its startup stuff.
    // Position and size a control based on other UI controls here
}

这篇关于可以使用什么Java Swing事件来了解应用程序何时启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆