// -*- 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 = 300;
  private static final int MH = 200;
  private final JCheckBox checkbox = new JCheckBox("Fixed aspect ratio, Minimum size: " + MW + "*" + MH);

  private MainPanel() {
    super(new BorderLayout());
    EventQueue.invokeLater(() -> {
      Container c = getTopLevelAncestor();
      if (c instanceof JFrame) {
        JFrame frame = (JFrame) c;
        frame.setMinimumSize(new Dimension(MW, MH)); // JDK 1.6.0
        frame.addComponentListener(new ComponentAdapter() {
          @Override public void componentResized(ComponentEvent e) {
            initFrameSize(frame);
          }
        });
      }
    });

    checkbox.addActionListener(e -> {
      Container c = getTopLevelAncestor();
      if (c instanceof JFrame) {
        initFrameSize((JFrame) c);
      }
    });
    // checkbox.setSelected(true);

    JLabel label = new JLabel();
    label.addComponentListener(new ComponentAdapter() {
      @Override public void componentResized(ComponentEvent e) {
        JLabel l = (JLabel) e.getComponent();
        Container c = getTopLevelAncestor();
        if (c instanceof JFrame) {
          l.setText(c.getSize().toString());
        }
      }
    });

    Toolkit.getDefaultToolkit().setDynamicLayout(false);
    JCheckBox check = new JCheckBox("Toolkit.getDefaultToolkit().setDynamicLayout: ");
    check.addActionListener(e ->
        Toolkit.getDefaultToolkit().setDynamicLayout(((JCheckBox) e.getSource()).isSelected()));

    JPanel p = new JPanel(new GridLayout(2, 1));
    p.add(checkbox);
    p.add(check);
    add(p, BorderLayout.NORTH);
    add(label);
    setPreferredSize(new Dimension(320, 240));
  }

  public void initFrameSize(JFrame frame) {
    if (!checkbox.isSelected()) {
      return;
    }
    int fw = frame.getSize().width;
    int fh = MH * fw / MW;
    frame.setSize(Math.max(MW, fw), Math.max(MH, 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@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

@@ -330,24 +330,25 @@
 t MH = 200;%0A
+%0A
   private fi
@@ -345,21 +345,64 @@
 private 
-final
+MainPanel() %7B%0A    super(new BorderLayout());%0A   
  JCheckB
@@ -488,63 +488,199 @@
 H);%0A
-%0A
   
-private MainPanel() %7B%0A    super(new BorderLayout());
+  checkbox.addActionListener(e -%3E %7B%0A      Container c = getTopLevelAncestor();%0A      if (c instanceof Window && checkbox.isSelected()) %7B%0A        initWindowSize((Window) c);%0A      %7D%0A    %7D);%0A
 %0A   
@@ -769,38 +769,38 @@
 f (c instanceof 
-JFrame
+Window
 ) %7B%0A        JFra
@@ -799,30 +799,31 @@
     
-JFrame frame = (JFrame
+Window window = (Window
 ) c;
@@ -827,29 +827,30 @@
  c;%0A        
-frame
+window
 .setMinimumS
@@ -898,21 +898,22 @@
         
-frame
+window
 .addComp
@@ -1037,267 +1037,125 @@
    i
-nitFrameSize(frame);%0A          %7D%0A        %7D);%0A      %7D%0A    %7D);%0A%0A    checkbox.addActionListener(e -%3E %7B%0A      Container c = getTopLevelAncestor();%0A      if (c instanceof JFrame) %7B%0A        initFrameSize((JFrame) c);%0A      %7D%0A
+f (checkbox.isSelected()) %7B%0A              initWindowSize(window);%0A            %7D%0A          %7D%0A        %7D);%0A 
     
+ 
 %7D
-);
 %0A    
-// checkbox.setSelected(true
+%7D
 );%0A%0A
@@ -1424,22 +1424,22 @@
 tanceof 
-JFrame
+Window
 ) %7B%0A    
@@ -1682,19 +1682,66 @@
 ner(e -%3E
+ %7B
 %0A  
+    JCheckBox c = (JCheckBox) e.getSource();%0A
       To
@@ -1787,35 +1787,9 @@
 out(
-((JCheckBox) e.getSource())
+c
 .isS
@@ -1798,16 +1798,23 @@
 ected())
+;%0A    %7D
 );%0A%0A    
@@ -2019,87 +2019,35 @@
 init
-FrameSize(JFrame frame) %7B%0A    if (!checkbox.isSelected()) %7B%0A      return;%0A    %7D
+WindowSize(Window window) %7B
 %0A   
@@ -2056,21 +2056,22 @@
 nt fw = 
-frame
+window
 .getSize
@@ -2107,29 +2107,30 @@
 w / MW;%0A    
-frame
+window
 .setSize(Mat

use Window instead of JFrame