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);

    }

……

……

 

 

}

 

                                                   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值