【发布时间】:2016-05-08 09:52:17
【问题描述】:
当我单击检查按钮时,应用程序在处理作业时停止。
我想获得实时结果。
这是我的代码。
我用这个应用程序检查了一些雅虎帐户。
点击check的时候我想一个一个的得到结果,但是点击check的时候必须等到所有的账户都被check之后才能得到结果。
package package_1;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import package_1.ProgressBarDemo2.Task;
import javax.swing.JSeparator;
import javax.mail.AuthenticationFailedException;
import javax.mail.Session;
import javax.mail.Store;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Properties;
import javax.swing.JProgressBar;
public class EmailChecker extends SwingWorker<Void, Void>{
private JFrame frame;
private JTextField textField;
public static boolean state = true;
public Task task;
/**
* Launch the application.
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
EmailChecker window = new EmailChecker();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void checkcurrent(String[] _Account,String[] _Pass,JTextArea _textArea,JTextArea _textArea_1,int _i,JProgressBar _progressBar,int _size) throws InterruptedException{
if(_i<_size && state == true){
Properties props = System.getProperties();
props.setProperty("mail.debug", "false");
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
try {
store.connect("imap.mail.yahoo.com", _Account[_i], _Pass[_i]);
} catch (AuthenticationFailedException e) {
}
if(store.isConnected() == true){
_textArea.setText(_Account[_i]+"/"+_Pass[_i]+ "\n" + _textArea.getText() );
}else{
_textArea_1.setText(_Account[_i]+"/"+_Pass[_i] + "\n" +_textArea_1.getText());
}
} catch (Exception e) {
}
_progressBar.setValue(100);
_i++;
System.out.println("checked");
checkcurrent(_Account,_Pass,_textArea,_textArea_1,_i,_progressBar,_size);
}else{
state = false;
}
}
/**
* Create the application.
*/
public EmailChecker() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
frame.setBounds(100, 100, 655, 433);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextPane textPane = new JTextPane();
textPane.setBounds(10, 34, 100, 140);
frame.getContentPane().add(textPane);
JTextPane textPane_1 = new JTextPane();
textPane_1.setBounds(120, 34, 92, 140);
frame.getContentPane().add(textPane_1);
JTextPane textPane_2 = new JTextPane();
textPane_2.setBounds(332, 34, 92, 140);
frame.getContentPane().add(textPane_2);
JTextPane textPane_3 = new JTextPane();
textPane_3.setBounds(222, 34, 100, 140);
frame.getContentPane().add(textPane_3);
JTextPane textPane_4 = new JTextPane();
textPane_4.setBounds(544, 34, 92, 140);
frame.getContentPane().add(textPane_4);
JTextPane textPane_5 = new JTextPane();
textPane_5.setBounds(434, 34, 100, 140);
frame.getContentPane().add(textPane_5);
JTextArea textArea = new JTextArea();
textArea.setToolTipText("Account Ok");
textArea.setBounds(10, 222, 292, 154);
frame.getContentPane().add(textArea);
JProgressBar progressBar = new JProgressBar();
progressBar.setMinimum(100);
progressBar.setBounds(10, 379, 626, 16);
frame.getContentPane().add(progressBar);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBounds(344, 222, 292, 154);
frame.getContentPane().add(textArea_1);
JButton btnCheck = new JButton("Check");
btnCheck.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] Account = textPane.getText().split("\\n");
String[] Pass = textPane_1.getText().split("\\n");
//String[] Proxy = textPane_2.getText().split("\\n");
//String[] Port = textPane_3.getText().split("\\n");
//String[] Login = textPane_4.getText().split("\\n");
//String[] PassPrx = textPane_5.getText().split("\\n");
int size = Account.length;
int i = 0;
try {
checkcurrent(Account,Pass,textArea,textArea_1,i,progressBar,size);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnCheck.setLocation(280, 185);
btnCheck.setBackground(Color.WHITE);
btnCheck.setSize(83, 29);
frame.getContentPane().add(btnCheck);
}
@Override
protected Void doInBackground() throws Exception {
return null;
// TODO Auto-generated method stub
}
}
【问题讨论】:
-
您必须让您的应用程序异步运行,以便获得非阻塞 UI,例如,通过在新的
Thread中运行您的checkcurrent()方法。这里有一个关于 Java 多线程的教程:beginnersbook.com/2013/03/multithreading-in-java -
感谢我是新的 Java 编码器,你能修改我的代码吗?