Java 荡秋千不停

Java 荡秋千不停,java,swing,swingworker,Java,Swing,Swingworker,伙计们,我刚进入swing worker,我不知道如何阻止它,我尝试了这种boolean方式。它不工作,你可以家伙的编辑我的代码的方式,它将工作或什么!非常感谢 没有错误显示它只是没有停止 class MessageBox { public static void infoBox(String infoMessage, String location) { JOptionPane.showMessageDialog(null, infoMessage, "Mes

伙计们,我刚进入swing worker,我不知道如何阻止它,我尝试了这种
boolean
方式。它不工作,你可以家伙的编辑我的代码的方式,它将工作或什么!非常感谢

没有错误显示它只是没有停止

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);
        }
    }
}
class消息框{
公共静态void信息框(字符串信息、字符串位置)
{
showMessageDialog(null,infoMessage,“Message:+位置,JOptionPane.INFORMATION\u Message);
}
}
公共类GUI扩展JFrame
{
私人按钮1;
私人JPanel Gpanel;
私有JTextField textField1;
私人JTextField有多少个;
私有JLabel计数标签;
专用JLabel状态标签;
私人JCheckBox anyEmail;
私人按钮停止按钮;
私有布尔停止;
公共图形用户界面(){
超级(“程序V0.1”);
这个.setIconImage(新的ImageIcon(getClass().getResource(“icon.png”)).getImage();
setContentPane(Gpanel);
包装();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(真);
button1.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
start();
}
});
addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
停止=真;
}
});
}
私有void start(){
SwingWorker worker=新SwingWorker(){
@凌驾
受保护的Void doInBackground()引发异常{
如果(!停止){
for(int i=0;i