因為是另外一個窗口調用了次類,所以使用JFrame.DISPOSE_ON_CLOSE以免把其他窗口都關了。但是退出的時候并沒有把另外一個線程關掉。public class ABC extends JFrame implements Runnable { public static void main(String[] args) { ABC abc= new ABC(); } public ABC(){ Thread thread = new Thread(this); //此處的線程還在運行 thread.start(); this.setSize(600,430); this.setResizable(false);//固定窗體大小 this.setLocationRelativeTo(null);//打開時相對window居中 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //此處會導致退出窗口后線程還在運行 this.setVisible(true); } @Override public void run() { while (true){ System.out.println("ABC"); try { Thread.sleep(1000); }catch (Exception e){ e.printStackTrace(); } } }}
請問我的這個線程要怎么停掉?
慕田峪9158850
2019-03-21 22:19:39