2017-04-04 20 views
-2

私はJFrameで動作するこのウェブカムプログラムを持っています。フレームを閉じると、いつものように "Closed"がプリントアウトされますが、私のIDEはそれがまだ動作していると言います。これはなぜですか、どうすれば修正できますか?私はプログラムのどこにもスレッドを実行していません。これは既定のクローズ操作とは何の関係もありません。すでにテスト済みです。終了時にこのJavaプログラムが閉じないのはなぜですか?

public class Webcam extends JPanel { 

private static BufferedImage image; 

public Webcam() { 
    super(); 
} 

public static void main(String args[]) { 
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 

    JFrame frame = new JFrame("Webcam"); 
    Webcam panel = new Webcam(); 

    // Initialize JPanel parameters 
    //frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    frame.setSize(1080, 720); 
    frame.setContentPane(panel); 
    frame.setVisible(true); 
    frame.addWindowListener(new WindowAdapter() 
    { 
    @Override 
    public void windowClosing(WindowEvent e) 
    { 
     System.out.println("Closed"); 
     e.getWindow().dispose(); 
     System.exit(0); 
    } 
    }); 
    Mat currentImage = new Mat(); 

    VideoCapture capture = new VideoCapture(0); 
    if(capture.isOpened()) { 
    // Infinitely update the images 
    while(true) { 
     // VideoCapture returns current Mat 
     capture.read(currentImage); 
     if(!currentImage.empty()) { 
      frame.setSize(currentImage.width() + 40, currentImage.height() + 60); 
      image = panel.matrixToBuffer(currentImage); 
      // Update the panel 
      panel.repaint(); 
     } 
     else { 
      System.out.println("Error: no frame captured"); 
      frame.dispose(); 
      System.exit(0); 
      break; 
     } 
    } 
    } 
    return; 

}

+5

[閉じるJavaでのJFrameの終了](http://stackoverflow.com/questions/7799940/jframe-exit-on-close-java) – nhouser9

+0

主な理由は、まだデーモンではないスレッドが実行されているためです。あなたは['JFrame#setDefaultCloseOperation'](https://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation-int-)を使って見て、 'EXIT_ON_CLOSE '、しかし、まだ実行中のスレッドがある理由を知ることができるかどうかを見てください。 – MadProgrammer

+0

再現できません。 'Mat currentImage = new Mat();'を取り除いた後、IDEAとEclipseで起動できますが、xボタンでフレームを閉じるとプログラムが終了します。 – glee8e

答えて

0

さて、私は有益なコメントのすべてに感謝しながら、問題はVideoCaptureは雌ねじランニングを持っていたことだった、とそれを修正私のリスナーにcapture.release()を追加します。

関連する問題