在单击按钮内显示加载 GIF,并在执行操作后再次隐藏它
Displaying a loading GIF inside a button click and hiding it again once the action is performed
我有以下 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
我有以下 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