Xen*_*oRo 1 java user-interface swing netbeans task
如何在显示(类扩展)后立即自动执行任务(使用工作线程)JFrame,同时如果可能的话,在类扩展中维护该任务的起始代码JFrame?
我找到的所有示例,以及到目前为止我自己如何使用它,仅显示 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.
}
Run Code Online (Sandbox Code Playgroud)
MyFrame是否可以在(任何扩展)内执行任务,例如触发事件、启动工作程序或以其他方式执行一些不相关的代码段,而无需在组件的JFrame重写或其他初始化方法中执行此操作setVisible())(以其他方式不会这样做的方式)?
如果可能的话,该怎么做?
额外的小问题:假设有可能,是否有关于在 NetBeans 的 GUI 构建器自动生成的代码或类似结构的代码中执行此操作的任何建议?
对的,这是可能的。
我想到了一些可能的解决方案来处理这两个问题;如果您需要仅在第一次(例如启动时)或多次(每次显示窗口或组件时)执行任务:
WindowListenerfrom JFrame,并启动其中的操作/工作人员windowOpened(WindowEvent e),即“在第一次显示侦听窗口后立即调用”。ComponentListenerfrom JFrame,并在其中启动操作/工作人员componentShown(ComponentEvent),这是“在侦听组件因调用 setVisible 方法而变得可见后调用”。与其他侦听器的类似解决方案也是可能的,但这些可能是最好的方法,对于任何奇怪的情况,从这两种方法中,您应该了解如何实现其他变体。
额外的答案:
添加侦听器的最佳位置是在其他组件初始化之后。在 NetBeans 中,初始化代码是自动生成的,包含在方法 ( initComponents()) 中,并且大部分是锁定的(可能是为了避免进行会破坏与可视 GUI 生成器相关的内容的修补)。
由于侦听器与任何组件都不相关(除了框架本身;或您正在扩展的“基本”组件),因此将此侦听器添加与其余初始化代码(包括负责的其他侦听器)分开实际上是一个好主意GUI 子组件的操作),因此 NetBeans 锁定初始化代码是这种分离的便捷实施,实际上有助于保持代码的整洁和可读性。
在这种情况下,我们仍然在初始化后立即添加监听器,但在构造函数中而不是在方法中initComponents();并且由于“ addSomeListener()”方法是可重写的,并且至少在这个示例中,我们不想意外地“忘记”扩展上的此代码,因此我们与自动生成的代码类似,并包装我们的自指向“”addSomeListener()方法(以及其他行动(如果需要)用我们自己的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(); //
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4577 次 |
| 最近记录: |