// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private static final int MW = 320;
  private static final int MH1 = 100;
  private static final int MH2 = 150;

  private MainPanel(JFrame frame) {
    super(new BorderLayout());

    JLabel label = new JLabel();
    label.addComponentListener(new ComponentAdapter() {
      @Override public void componentResized(ComponentEvent e) {
        ((JLabel) e.getComponent()).setText(frame.getSize().toString());
      }
    });

    JCheckBox checkbox1 = new JCheckBox("the minimum size of this window: " + MW + "x" + MH1, true);
    checkbox1.addActionListener(e -> {
      if (!((JCheckBox) e.getSource()).isSelected()) {
        return;
      }
      initFrameSize(frame);
    });

    JCheckBox checkbox2 = new JCheckBox("the minimum size of this window(since 1.6): " + MW + "x" + MH2, true);
    checkbox2.addActionListener(e -> frame.setMinimumSize(checkbox2.isSelected() ? new Dimension(MW, MH2) : null));

    frame.setMinimumSize(new Dimension(MW, MH2));
    frame.addComponentListener(new ComponentAdapter() {
      @Override public void componentResized(ComponentEvent e) {
        if (!checkbox1.isSelected()) {
          return;
        }
        initFrameSize((JFrame) e.getComponent());
      }
    });

    Box box = Box.createVerticalBox();
    box.add(checkbox1);
    box.add(checkbox2);
    add(box, BorderLayout.NORTH);
    add(label);
    setPreferredSize(new Dimension(320, 240));
  }

  protected static void initFrameSize(JFrame frame) {
    int fw = frame.getSize().width;
    int fh = frame.getSize().height;
    frame.setSize(Math.max(MW, fw), Math.max(MH1, fh));
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    // int MAX = 500;
    // // frame.setMaximumSize(new Dimension(MAX, MAX));
    // Robot r;
    // Robot r2;
    // try {
    //   r = new Robot();
    // } catch (AWTException ex) {
    //   r = null;
    // }
    // r2 = r;
    // frame.getRootPane().addComponentListener(new ComponentAdapter() {
    //   @Override public void componentResized(ComponentEvent e) {
    //     Point loc = frame.getLocationOnScreen();
    //     Point mouse = MouseInfo.getPointerInfo().getLocation();
    //     if (Objects.nonNull(r2) && (mouse.getX() > loc.getX() + MAX || mouse.getY() > loc.getY() + MAX)) {
    //       r2.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
    //       frame.setSize(Math.min(MAX, frame.getWidth()), Math.min(MAX, frame.getHeight()));
    //     }
    //   }
    // });
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel(frame));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

@@ -394,28 +394,16 @@
 inPanel(
-JFrame frame
 ) %7B%0A    
@@ -425,25 +425,24 @@
 rLayout());%0A
-%0A
     JLabel l
@@ -575,32 +575,100 @@
 onentEvent e) %7B%0A
+        Window w = SwingUtilities.getWindowAncestor(getRootPane());%0A
         ((JLabel
@@ -695,21 +695,17 @@
 setText(
-frame
+w
 .getSize
@@ -887,108 +887,181 @@
     
-if (!((JCheckBox) e.getSource()).isSelected()) %7B%0A        return;%0A      %7D%0A      initFrameSize(frame);
+Object o = e.getSource();%0A      if (o instanceof JCheckBox && ((JCheckBox) o).isSelected()) %7B%0A        initFrameSize(SwingUtilities.getWindowAncestor(getRootPane()));%0A      %7D
 %0A   
@@ -1215,21 +1215,91 @@
 er(e -%3E 
-frame
+%7B%0A      Window w = SwingUtilities.getWindowAncestor(getRootPane());%0A      w
 .setMini
@@ -1361,24 +1361,31 @@
  : null)
+;%0A    %7D
 );%0A%0A    
 frame.se
@@ -1372,29 +1372,128 @@
    %7D);%0A%0A    
-frame
+EventQueue.invokeLater(() -%3E %7B%0A      Window w = SwingUtilities.getWindowAncestor(getRootPane());%0A      w
 .setMinimumS
@@ -1525,21 +1525,19 @@
 ));%0A    
-frame
+  w
 .addComp
@@ -1567,32 +1567,34 @@
 nentAdapter() %7B%0A
+  
       @Override 
@@ -1654,13 +1654,14 @@
     
+ 
+ 
 if (
-!
 chec
@@ -1690,32 +1690,8 @@
     
-      return;%0A        %7D%0A
     
@@ -1709,22 +1709,22 @@
 meSize((
-JFrame
+Window
 ) e.getC
@@ -1734,33 +1734,57 @@
 onent());%0A      
-%7D
+    %7D%0A        %7D%0A      %7D);
 %0A    %7D);%0A%0A    Bo
@@ -2004,22 +2004,22 @@
 ameSize(
-JFrame
+Window
  frame) 
@@ -3532,21 +3532,16 @@
 inPanel(
-frame
 ));%0A    

use Window#getSize() instead of JFrame