Java有Stop按钮退出正在运行的程序

1.       运行界面的主程序,注意 main4.run调用真正执行任务的程序

 

importjava.awt.FlowLayout; 

importjava.awt.event.ActionEvent; 

importjava.awt.event.ActionListener;

import java.io.IOException;

 

importjavax.swing.JButton; 

import javax.swing.JFrame; 

importjavax.swing.JPanel; 

 

public class Test extends JFrame { 

    SheThread thread = null

 

    public Test() { 

        try

            createFrame(); 

            } 

            catch(Exception e) { 

            e.printStackTrace(); 

        } 

    } 

 

    private void createFrame() throws IOException { 

 

        JPanel jp = new JPanel(new FlowLayout()); 

        this.add(jp); 

 

        JButton jbStart = new JButton("start "); 

        JButton jbEnd = new JButton("stop"); 

        //jp.add(jbStart); 

        jp.add(jbEnd); 

 

        this.setSize(300, 100); 

        this.setVisible(true); 

        this.setResizable(false); 

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

 

        jbStart.addActionListener(new ActionListener() { 

            public voidactionPerformed(ActionEvent e) { 

                if (thread != null

                    thread.stop()

                thread = new SheThread(); 

                thread.start(); 

            } 

        }); 

        jbEnd.addActionListener(new ActionListener() { 

            public voidactionPerformed(ActionEvent e) { 

                if (thread != null

                    thread.stop();

                  System.exit(0);

                thread = null

            } 

        }); 

       

        //需要执行的主程序

        Main4.run();

    } 

 

    public static void main(String[] args) { 

        new Test().show()

    } 

 

 

class SheThread extends Thread { 

    public SheThread() { 

    } 

 

    public void run() { 

        while (true) { 

            try

                sleep(1000); 

            } catch (InterruptedException e) { 

            } 

            System.out.println("this is a test!"); 

        } 

    } 

 

 

2.      执行代码的真正程序

public class Main4 {

    public static int Sub_ID = 0;

    public static String cliuid_2 = "";

    public static String unedname = "";

    public static String search_method = "";

    public static int rawID_Total = 0;

    public static JTextField filename = new JTextField("c:/p.xls");

    public static String URL = "";

    public static JFrame frame = new JFrame();

    public static PrintWriter writer;

    public static DB db;

    public static String str_SQL;

    public static String str_trim = "0000";

    public static String cliuid_2_unedname = "";

    public static String dirName = "";

    public static String fileName_final = "";

 

……

……

 

    public static void run() throws IOException {

        try {

            input();

            Sheet sheet;

            Workbook book;

            try {

                book = Workbook.getWorkbook(new File(filename.getText()));

                sheet = book.getSheet(0);

                rawID_Total = sheet.getRows(); // 获取行数

                for (int i = 1; i < rawID_Total; i++) {

                    readExcel(sheet, i);

                    initSearch(unedname, "bingSearch");

                    Sub_ID = 0;

                }

                book.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

        } catch (Exception e2) {

            JOptionPane.showMessageDialog(null, e2.getMessage());

        }

 

        System.exit(0);

    }

……

……

 

 

}

 

                                                   

你可能感兴趣的:(Java)