gpt4 book ai didi

java - 在按钮单击内显示加载 GIF,并在执行操作后再次隐藏它

转载 作者:行者123 更新时间:2023-12-01 18:48:31 24 4
gpt4 key购买 nike

我有以下 JFrame,里面有一个足够简单的退出应用程序方法。我正在尝试添加某种加载程序(最好是旋转 GIF)。然而,目前我刚刚添加了一个名为“loading”的 JLabel,以便在单击按钮时显示文本“loading...”。

问题是消息框(即操作)在我的函数中根本不显示,并且加载 JLabel 继续显示。谁能解释一下为什么这不起作用,并给我关于如何用 GIF 替换加载文本的建议。任何帮助都会很棒。

 public void Exit() {
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// event start
loading.setVisible(true);

new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() throws Exception {
Thread.sleep(100);
return null;
}

@Override
protected void done() {
// event start
loading.setVisible(false);

// prompt user input message box //
int close = JOptionPane.showConfirmDialog(panel,"Are you sure?",
"Information", JOptionPane.YES_NO_OPTION);

// await user selection.
if(close == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
};
}
});
}

最佳答案

创建 SwingWorker 本身是不够的。您必须调用其 execute()创建后的方法。

Schedules this SwingWorker for execution on a worker thread. There are a number of worker threads available. In the event all worker threads are busy handling other SwingWorkers this SwingWorker is placed in a waiting queue.

Note: SwingWorker is only designed to be executed once. Executing a SwingWorker more than once will not result in invoking the doInBackground method twice.

就您而言:

SwingWorker<?,?> worker = new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() throws Exception {
Thread.sleep(100);
return null;
}

@Override
protected void done() {
// event start
loading.setVisible(false);

// prompt user input message box //
int close = JOptionPane.showConfirmDialog(panel,"Are you sure?",
"Information", JOptionPane.YES_NO_OPTION);

// await user selection.
if(close == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
};
worker.execute(); //Execute the worker

关于java - 在按钮单击内显示加载 GIF,并在执行操作后再次隐藏它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59773638/

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