所以我剛剛進入秋千工人,我不知道如何阻止它我試圖這boolean
方式。它不工作,你可以編輯我的代碼的方式,它會工作或什麼的!非常感謝!SwingWorker不停止
沒有錯誤顯示它只是不停止!
class MessageBox {
public static void infoBox(String infoMessage, String location)
{
JOptionPane.showMessageDialog(null, infoMessage, "Message: " + location, JOptionPane.INFORMATION_MESSAGE);
}
}
public class GUI extends JFrame
{
private JButton button1;
private JPanel Gpanel;
private JTextField textField1;
private JTextField howmany;
private JLabel countlabel;
private JLabel statuslabel;
private JCheckBox anyEmail;
private JButton stopButton;
private boolean stopped;
public GUI() {
super("Program V0.1");
this.setIconImage(new ImageIcon(getClass().getResource("icon.png")).getImage());
setContentPane(Gpanel);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
start();
}
});
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stopped = true;
}
});
}
private void start() {
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
if (!stopped){
for (int i = 0; i < Integer.parseInt(howmany.getText()); i++) {
if (anyEmail.isSelected()) {
URL url = null;
try {
String Hostemail = "http://example.com/";
url = new URL(Hostemail);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Map<String, Object> variables = new LinkedHashMap<>();
variables.put("email", textField1.getText());
variables.put("fullname", "");
variables.put("pw", "");
variables.put("pw-conf", "");
variables.put("digest", "1");
variables.put("email-button", "Subscribe");
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : variables.entrySet()) {
if (postData.length() != 0) {
postData.append('&');
}
try {
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
postData.append('=');
try {
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
byte[] postDataBytes = new byte[0];
try {
postDataBytes = postData.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String line;
HttpURLConnection conn = null;
try {
assert url != null;
conn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
try {
assert conn != null;
conn.setRequestMethod("POST");
} catch (ProtocolException e) {
e.printStackTrace();
}
conn.setRequestProperty("Cache-Control", "no-cache,private,Private");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
try {
conn.getOutputStream().write(postDataBytes);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
try {
assert in != null;
while ((line = in.readLine()) != null) {
String success = "Your subscription request has been received";
if (line.contains(success)) {
countlabel.setText(String.valueOf(i));
}
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
URL url = null;
try {
String Host = "http://example2.com";
url = new URL(Host);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Map<String, Object> variables = new LinkedHashMap<>();
variables.put("strEmail", textField1.getText());
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : variables.entrySet()) {
if (postData.length() != 0) {
postData.append('&');
}
try {
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
postData.append('=');
try {
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
byte[] postDataBytes = new byte[0];
try {
postDataBytes = postData.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String line;
HttpURLConnection conn = null;
try {
assert url != null;
conn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
try {
assert conn != null;
conn.setRequestMethod("POST");
} catch (ProtocolException e) {
e.printStackTrace();
}
conn.setRequestProperty("Cache-Control", "no-cache,private,Private");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
try {
conn.getOutputStream().write(postDataBytes);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
try {
statuslabel.setText("Sending Emails...");
assert in != null;
while ((line = in.readLine()) != null) {
String success = "success";
String error = "not";
if (line.contains(success)) {
countlabel.setText(String.valueOf(i));
} else if (line.contains(error)) {
MessageBox.infoBox("[Email Not Found]: " + textField1.getText(), "Error");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
statuslabel.setText("Done");
return null;
}
};
worker.execute();
if (stopped){
worker.cancel(true);
}
}
}
你有沒有試過呼叫工人的取消? – MadProgrammer 2014-08-30 22:22:31
這就是程序人我不知道如何在動作監聽器中調用它:/所以我嘗試使用bool – Shrekt 2014-08-30 22:24:31
然後你需要一個引用正在運行的工作者以調用'cancel'改變你的'start'方法,以便它創建一個'SwingWorker'分配給一個類實例變量,而不是一個局部變量。 – MadProgrammer 2014-08-30 22:32:27