如何在不关闭窗口的情况下终止Java程序

时间:2018-02-06 14:49:40

标签: java swing terminate

我创建了一个Java应用程序,用户单击“运行”按钮后会浏览数百个文档。有没有办法终止程序并让GUI运行?我希望能够停止的只是阅读文件的过程。

当我的整个应用关闭时,

System.exit(0)不是我正在寻找的解决方案。

1 个答案:

答案 0 :(得分:0)

如果没有看到你的申请,很难说些什么。但是这段代码可能会帮助您了解如何实现您想要的内容:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;

public class SwingWorkerTest implements Runnable {

    private JButton cancelButton = new JButton("Cancel");

    private JButton runButton = new JButton("Run");

    private JLabel label = new JLabel("Press 'Run' to start");

    private LongWorker longWorker;

    @Override
    public void run() {
        JFrame frm = new JFrame("Long task test");
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        cancelButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                longWorker.terminate();
            }
        });
        runButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                longWorker = new LongWorker();
                runButton.setEnabled(false);
                cancelButton.setEnabled(true);
                label.setText("Task in progress. Press 'Cancel' to terminate.");
                longWorker.execute();
            }
        });
        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        bottomPanel.add(runButton);
        bottomPanel.add(cancelButton);
        frm.add(label);
        frm.add(bottomPanel, BorderLayout.SOUTH);
        frm.setSize(400, 200);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SwingWorkerTest());
    }

    private class LongWorker extends SwingWorker<Void, Void> {
        private volatile boolean terminated;

        @Override
        protected Void doInBackground() throws Exception {
            // check special variable tov determine whether this task still active
            for (int i = 0; i < 1000 && !terminated; i++) {
                readFile();
            }
            return null;
        }

        @Override
        protected void done() {
            if (terminated) {
                label.setText("Process terminated. Press 'Run' to restart.");
            } else {
                label.setText("Process done. Press 'Run' to restart.");
            }
            cancelButton.setEnabled(false);
            runButton.setEnabled(true);
        }

        // dummy method - make 10 milliseconds sleep
        private void readFile() {
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                // Nothing here
            }
        }

        public void terminate() {
            terminated = true;
        }
    }
}