我有一個處理某些文件的程序。有時候,我需要在處理過程中阻止它,但是在如何完成這個過程中遇到困難。我已經看到了幾種可能性,但我不確定要追求什麼。多線程,就像在遊戲中一樣嗎?我發現了一些關於取消的信息,但它似乎只適用於數據輸入過程中?我在下面包括我的GUI的代碼,也許我只是做了錯誤的事情?如何安全地退出Swing應用程序
需要明確的是,我們的目標是停止程序,無論它在哪裏處理?
private JRadioButton blockButton, unblockButton;
private JButton btnCancel;
private ButtonGroup group;
private JLabel text;
private JButton enter;
public Gui(){
super("Download CORS files");
setLayout(null);
text = new JLabel("Would you like to download data in a block of days or intermittently over time?");
add(text);
blockButton = new JRadioButton("Block of Days", true);
unblockButton = new JRadioButton("Intermittent Days", false);
btnCancel = new JButton("Cancel run");
add(blockButton);
add(unblockButton);
add(btnCancel);
group = new ButtonGroup();
group.add(blockButton);
group.add(unblockButton);
// Get the size of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
// Determine the new location of the window
int w = this.getSize().width;
int h = this.getSize().height;
int x = ((dim.width-w)/2)-200;
int y = ((dim.height-h)/2)-200;
// Move the window
this.setLocation(x, y);
enter = new JButton("Enter");
enter.addActionListener(this);
add(enter);
text.setBounds(5,5,700,25);
blockButton.setBounds(5, 25, 300, 25);
unblockButton.setBounds(5,50, 300, 25);
enter.setBounds(75,100,100,20);
btnCancel.setBounds(300,100,100,20);
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}});
調用'System.exit(1);'。這將阻止它。 – 2014-10-02 15:54:44
如果您試圖從一個Swing窗口終止一個選項,那不是線程安全的。您需要設置一個任務,以允許Swing的功能正常工作。 – Compass 2014-10-02 15:56:16
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#exit(int) – StackFlowed 2014-10-02 15:56:23