Java 中的 SwingWorker [英] SwingWorker in Java
问题描述
我有问题.
我有一个 JFrame
.它将创建一个 JDialog
.
I have a JFrame
. It will create a JDialog
.
当 JDialog
上的按钮被按下时,它应该被处理并发送一封电子邮件.同时,我需要另一个 JDialog
以不确定的 JProgressBar
出现.发送电子邮件时,JDialog
应该被处理(并生成新的)或者它的内容应该改变.
When button on JDialog
is pressed it should be disposed and an email should be sent.
At the same time, I need another JDialog
to appear with indetermined JProgressBar
. When email is sent, JDialog
should either be disposed (and new one creted) or it's contents should change.
我已经失败了几个小时了,所以我问大家他(或她)是否愿意给我写一个伪代码来做我想做的事.
I have been failing with this for several hours now, so I'm asking enyone if he (or she) would be kind enough to write me a pseudo-code that will do what I want.
只是看看应该在 SwingWorker
类中包含什么(或者如果你认为它更好,则使用多线程),何时应该创建/处置 JDialog
(s),以及在哪里坚持发送电子邮件...
Just to see what should be included in SwingWorker
class (or use multithreading if you think it's better), when JDialog
(s) should be created/disposed, and where to stick in the email sending...
我知道我在这里要求一个完整的解决方案,但我在一个截止日期并且已经失败了很多次......这是我最后的手段......
I know I'm asking for a finished solution here, but I'm on a dedline and have failed so many times already... This was my last resort...
推荐答案
我为你做了一个简短的例子,希望对你有所帮助.基本上显示了一个带有按钮的 JFrame
:
I did a short example for you hope it helps. Basically a JFrame
witha button is shown:
当点击框架上的 JButton
时,会出现一个 JDialog
和另一个 JButton
(发送电子邮件)- 这将代表电子邮件对话框:
when the JButton
on the frame is clicked a JDialog
will appear with another JButton
(Send Email) - this would represent the email dialog:
当 emailDialog
上的 JButton
被按下时,它会处理 emailDialog
并创建一个新的 JDialog
将保存 progressbar(或者在这种情况下是一个简单的 JLabel
):
When the JButton
on the emailDialog
is pressed it disposes of the emailDialog
and creates a new JDialog
which will hold the progressbar (or in this case a simple JLabel
):
然后它创建并执行 SwingWorker
以发送电子邮件和 JDialog
的 dispose()
完成并显示 JOptionPane
显示发送成功的消息:
and then it creates and executes the SwingWorker
to send the email and dispose()
of the JDialog
when its done and show a JOptionPane
message showing success of the sending:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test {
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Test().createAndShowUI();
}
});
}
private void createAndShowUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents(frame);
frame.setPreferredSize(new Dimension(300, 300));//testing purposes
frame.pack();
frame.setVisible(true);
}
private void initComponents(final JFrame frame) {
final JDialog emailDialog = new JDialog(frame);
emailDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
emailDialog.setLayout(new BorderLayout());
JButton sendMailBtn = new JButton("Send Email");
sendMailBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//get content needed for email from old dialog
//get rid of old dialog
emailDialog.dispose();
//create new dialog
final JDialog emailProgressDialog = new JDialog(frame);
emailProgressDialog.add(new JLabel("Mail in progress"));
emailProgressDialog.pack();
emailProgressDialog.setVisible(true);
new Worker(emailProgressDialog).execute();
}
});
emailDialog.add(sendMailBtn, BorderLayout.SOUTH);
emailDialog.pack();
JButton openDialog = new JButton("Open emailDialog");
openDialog.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
emailDialog.setVisible(true);
}
});
frame.getContentPane().add(openDialog);
}
}
class Worker extends SwingWorker<String, Object> {
private final JDialog dialog;
Worker(JDialog dialog) {
this.dialog = dialog;
}
@Override
protected String doInBackground() throws Exception {
Thread.sleep(2000);//simulate email sending
return "DONE";
}
@Override
protected void done() {
super.done();
dialog.dispose();
JOptionPane.showMessageDialog(dialog.getOwner(), "Message sent", "Success", JOptionPane.INFORMATION_MESSAGE);
}
}
这篇关于Java 中的 SwingWorker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!