如何在显示(类扩展)JFrame
后立即自动执行任务(使用工作线程),如果可能的话,在扩展类{{1}的类中维护该任务的起始代码}?
我找到的所有示例,以及我到目前为止自己如何使用它,只显示GUI类扩展,其中包含在启动和显示GUI后对输入作出反应的代码,具有任何自动功能正在进行的其他操作在之外启动并显示GUI的代码;换句话说,我(做/不)看到的内容在下面的示例代码中注释:
JFrame
是否可以在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
或组件的其他初始化方法的范围内(以不会做的方式)?
如果有可能,怎么做?
奖金小问题: 假设有可能,是否有任何关于NetBeans的GUI-builder自动生成代码或类似结构代码的建议?
答案 0 :(得分:3)
是的,有可能。
有两种可能的解决方案可以解决这两种问题;如果您只需要在第一次(例如,在启动时)执行任务,或者多次(每次显示窗口或组件时)执行任务:
JFrame
中添加WindowListener
,并在windowOpened(WindowEvent e)
内启动动作/工作人员,这是&#34;在听取后立即调用-to窗口第一次显示&#34; 。JFrame
中添加ComponentListener
,并在componentShown(ComponentEvent)
内启动操作/工作人员,这是&#34;在听取后调用 - 由于setVisible方法被调用&#34; 。使用other listeners的类似解决方案也是可能的,但这些可能是最好的方法,对于任何奇怪的情况,从这两个方面来看,您应该了解如何实现其他变体。
奖励回答:
添加侦听器的最佳位置是在其他组件初始化之后。在NetBeans&#39;中,初始化代码是自动生成的,包含在方法(initComponents()
)中,并且大部分都是锁定的(可能是为了避免修改会破坏与可视化GUI构建器相关的内容)。
由于收听者与任何组件(除了框架本身;或者您正在扩展的基础组件除外)没有关系,因此分开这个组件实际上是一个好主意。侦听器从其余的初始化代码中添加(包括负责GUI子组件操作的其他侦听器),以及NetBeans&#39;锁定初始化代码可以方便地实现这种分离,这实际上有助于保持代码的清洁和可读性。
在这种情况下,我们仍然在初始化之后立即添加侦听器,但是在构造函数中而不是在initComponents()
方法中;以及&#34; addSomeListener()
&#34;方法是可以覆盖的,至少在这个例子中,我们不想意外地忘记&#34;这个代码在扩展上,我们做的与自动生成的代码类似,并包装我们的自我指向&#34; addSomeListener()
&#34;我们自己的initSomething()
方法中的方法(以及其他操作,如果需要)!
这是一个代码示例:
//Within -> public class MyFrame extends javax.swing.JFrame {
//This is a mockup worker to simulate some time-consiming loading task that would be performed at startup, with the GUI providing a loading screen...
SwingWorker<Integer, Integer> StartupLoader = new SwingWorker<Integer, Integer>() {
@Override
protected Integer doInBackground() throws Exception {
for (int i = 0; i < 100; i++) {
System.out.println("Some time consuming task is at " + i + "%...");
}
return 100;
}
};
//This method is used to avoid calling an overridable method ('addWindowListener()') from within the constructor.
private void initSelfListeners() {
WindowListener taskStarterWindowListener = new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("Performing task..."); //Perform task here. In this case, we are simulating a startup (only once) time-consuming task that would use a worker.
StartupLoader.execute();
}
@Override
public void windowClosing(WindowEvent e) {
//Do nothing...Or something...You decide!
}
@Override
public void windowClosed(WindowEvent e) {
//Do nothing...Or drink coffee...NVM; always drink coffee!
}
@Override
public void windowIconified(WindowEvent e) {
//Do nothing...Or do EVERYTHING!
}
@Override
public void windowDeiconified(WindowEvent e) {
//Do nothing...Or break the law...
}
@Override
public void windowActivated(WindowEvent e) {
//Do nothing...Procrastinate like me!
}
@Override
public void windowDeactivated(WindowEvent e) {
//Do nothing...And please don't notice I have way too much free time today...
}
};
//Here is where the magic happens! We make (a listener within) the frame start listening to the frame's own events!
this.addWindowListener(taskStarterWindowListener);
}
//The method that adds the listeners that perform the tasks is added in the constructor,
//right after initializing the components (auto-generated method in NetBeans).
public MyFrame() {
initComponents();
initSelfListeners(); //
}