Terai Atsuhiro 2021-04-09 (金) 19:43:05

http://terai.xrea.jp/swing/closeoperation/screenshot.png

private static int number = 0;
public static JFrame createFrame(String title) {
  JFrame frame = new JFrame(title);
  if(number==0) {
    frame.getContentPane().add(new MainPanel());
  }
  number++;
  WindowListener l = new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
      number--;
      if(number==0) {
        JFrame f = (JFrame)e.getSource();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    }
  };
  frame.addWindowListener(l);
  return frame;
}