gpt4 book ai didi

java - 如何在 JFrame 显示后从其中自动执行任务?

转载 作者:行者123 更新时间:2023-12-01 21:18:17 25 4
gpt4 key购买 nike

如何在显示(扩展类)JFrame 后立即自动执行任务(使用工作线程),同时,如果可能的话,在扩展类中维护该任务的起始代码 JFrame

我找到的所有示例,以及到目前为止我自己如何使用它,仅显示 GUI 类扩展,其中的代码在启动和显示 GUI 后对输入使用react,以及任何自动在启动和显示 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 构建器自动生成的代码或类似结构的代码中执行此操作的任何建议?

最佳答案

是的,这是可能的。

<小时/>

我想到了一些可能的解决方案来处理这两个问题;如果您需要仅在第一次(例如启动时)或多次(每次显示窗口或组件时)执行任务:

  • 添加 WindowListener从其中调用 JFrame,并在 windowOpened(WindowEvent e) 中启动操作/worker,这是“在监听窗口之后调用”首次展示”
  • 添加 ComponentListener从其中调用 JFrame,并在 componentShown(ComponentEvent) 中启动操作/worker,这是“在监听的组件变得可见后调用作为调用 setVisible 方法的结果”

other listeners 类似的解决方案也是可能的,但这些可能是最好的方法,对于任何奇怪的情况,从这两种方法中,您应该了解如何实现其他变体。

<小时/>

奖励答案:添加监听器的最佳位置是在其他组件初始化之后。在 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(); //
}

关于java - 如何在 JFrame 显示后从其中自动执行任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39565472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com