TITLE:JFrameの複数作成と終了
#navi(../)
*JFrameの複数作成と終了 [#pc54259a]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2005-06-27~
更新日:&lastmod;

#contents
**概要 [#h7071c6a]
JFrameを複数作成し、これらをすべて閉じた時にアプリケーションを終了します。

#screenshot

**サンプルコード [#qf5934db]
#code{{
 private static int number = 0;
 public static JFrame createFrame(String title) {
   JFrame frame = new JFrame(title);
   number++;
   frame.addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent e) {
       number--;
       if(number==0) {
         JFrame f = (JFrame)e.getSource();
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
     }
   });
   return frame;
 }
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#s4224022]
上記のサンプルでは、WindowListenerで終了時に自分が最後のフレームの場合だけsetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)するように設定しています。

//**参考リンク
**コメント [#if26eeb6]
#comment