【发布时间】:2016-09-19 04:39:30
【问题描述】:
如何在(类扩展)JFrame 显示后立即自动执行任务(使用工作线程),同时尽可能在扩展 JFrame 的类中维护该任务的起始代码?
我找到的所有示例,以及到目前为止我自己是如何使用它的,仅显示 GUI 类扩展,其代码在启动和显示 GUI 后对输入作出反应,任何自动在启动和显示 GUI 的代码之外进行的进一步操作;换句话说,我(做/不做)看到的是下面示例代码中的注释:
public static void main(String args[]) {
//Set the Look and Feel
//...
//Create and display the form
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MyFrame().setVisible(true); //<--- I never see automatic stuff happening
//...within here (class). I realize it can be done overriding setVisible(), but
//...that would be cheating in regards to the intent of this question.
}
});
//In the examples I found, and the way I have done it myself, automatic
//..."after GUI is displayed" actions, such as a starting a worker that will load
//...something heavy on startup, while the GUI displays a progressar, are started here.
}
是否可以在MyFrame(JFrame 的任何扩展)内执行任务,例如触发事件、启动工作程序或以其他方式执行一些不相关的代码,而无需在setVisible() 或组件的其他初始化方法(以其他方式无法完成的方式)?
如果可以的话,怎么做?
额外的小问题: 假设有可能,对于 NetBeans 的 GUI-builder 自动生成的代码或类似结构的代码,是否有任何建议?
【问题讨论】:
标签: java swing user-interface netbeans task