• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JFrameの最小サイズ
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2003-09-25
#tags(JFrame, JDialog)
RIGHT:Posted by &author(aterai); at 2003-09-22
*JFrameの最小サイズ [#u3b1e41a]
JFrameやJDialogの最小サイズを指定します。
``JFrame``や``JDialog``の最小サイズを指定します。

-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTP41PdCsI/AAAAAAAAAes/cxniHSm55rQ/s800/MinimumFrame.png)

**サンプルコード [#i2728958]
#code(link){{
//1.6以上で有効
frame.setMinimumSize(new Dimension(320, 150));
}}

**解説 [#nb3f1b60]
上記のサンプルでは、JFrameなどを縮小する際の最小サイズを設定しています。
上記のサンプルでは、``JFrame``などを縮小する際の最小サイズを設定しています。

JDK 1.6 以上の場合、JFrame#setMinimumSizeメソッドを使用することで、最小サイズを指定することができます。
``JDK 1.6``以上の場合、``JFrame#setMinimumSize``メソッドを使用することで、最小サイズを指定することができます。

JDK 1.5 で、JFrame#setMinimumSizeメソッドを使う場合は、JFrame.setDefaultLookAndFeelDecorated(true)、かつウインドウのリサイズに応じてレイアウトを再評価するようにしておく必要があるようです。
``JDK 1.5``で、``JFrame#setMinimumSize``メソッドを使う場合は、``JFrame.setDefaultLookAndFeelDecorated(true)``、かつウインドウのリサイズに応じてレイアウトを再評価するようにしておく必要があるようです。

#code{{
JFrame.setDefaultLookAndFeelDecorated(true);
Toolkit.getDefaultToolkit().setDynamicLayout(true);
}}

----
JFrame#setMinimumSizeメソッドを使わず、ComponentListenerでサイズを制限する場合は、この制限を超えて縮小しようとしても、マウスを放した時点で設定した最小サイズまでフレームの大きさは戻されます。
``JFrame#setMinimumSize``メソッドを使わず、``ComponentListener``でサイズを制限する場合は、この制限を超えて縮小しようとしても、マウスを放した時点で設定した最小サイズまでフレームの大きさは戻されます。

#code{{
//ComponentListenerを使用
final int mw = 320;
final int mh = 100;
final JFrame frame = new JFrame();
frame.addComponentListener(new ComponentAdapter() {
  public void componentResized(ComponentEvent e) {
    int fw = frame.getSize().width;
    int fh = frame.getSize().height;
    frame.setSize((mw>fw)?mw:fw, (mh>fh)?mh:fh);
  }
});

//JFrame#validateをオーバーライド
//final Dimension minDim = new Dimension(320, 100);
//JFrame frame = new JFrame() {
//  public void validate() {
//    Rectangle rect = getBounds();
//    if(minDim.width>rect.width || minDim.height>rect.height) {
//      int mw = ((minDim.width>rect.width)?minDim.width:rect.width);
//      int mh = ((minDim.height>rect.height)?minDim.height:rect.height);
//      setBounds(rect.x, rect.y, mw, mh);
//    }
//    super.validate();
//  }
//};
}}

JFrame#setMaximumSizeが無効な環境でも、上記のようにComponentListenerを使えば最大サイズを制限する((リサイズした後で最大サイズに戻しているだけなので表示は酷い))ことができます。
``JFrame#setMaximumSize``が無効な環境でも、上記のように``ComponentListener``を使えば最大サイズを制限する(リサイズした後で最大サイズに戻しているだけ)ことができます。

----
Robotを使う方法もあるようです。
``Robot``を使う方法もあるようです。

-[http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6464548 Bug ID: 6464548 Reopen 6383434: Frame.setMaximumSize() doesn't work]

#code{{
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MaximumSizeTest{
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() { createAndShowGUI(); }
    });
  }
  private static final int MAX = 500;
  public static void createAndShowGUI() {
    final JFrame frame = new JFrame();
    frame.setMinimumSize(new Dimension(240,120));
    //frame.setMaximumSize(new Dimension(400,400));
    Robot r;
    final Robot r2;
    try{
      r = new Robot();
    }catch (AWTException ex) {
      r = null;
    }
    r2 = r;
    frame.getRootPane().addComponentListener(new ComponentAdapter() {
      public void componentResized(ComponentEvent e) {
        Point loc   = frame.getLocationOnScreen();
        Point mouse = MouseInfo.getPointerInfo().getLocation();
        if(r2!=null && (mouse.getX()>loc.getX()+MAX ||
                        mouse.getY()>loc.getY()+MAX)) {
          r2.mouseRelease(InputEvent.BUTTON1_MASK); 
          r2.mouseRelease(InputEvent.BUTTON1_MASK);
          frame.setSize(Math.min(MAX, frame.getWidth()),
                        Math.min(MAX, frame.getHeight()));
        }
      }
    });
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setSize(320, 240);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}
}}

**参考リンク [#z43489b3]
-[http://forums.sun.com/thread.jspa?threadID=762041 Swing - Have JFrame respect the minimum size (stop resizing) - Partial solution]
-[[DynamicLayoutでレイアウトの動的評価>Swing/DynamicLayout]]

**コメント [#b1623254]
- 最大サイズも同じ要領で・・・とありますがJFrame#setMaximumSizeはうまくいかないですね(JDK1.6.0_u1)色々調べているのですが、いい方法あるんでしょうか? -- [[sawshun]] &new{2009-07-27 (月) 11:51:11};
-- 同じ要領なのは、ComponentListenerを使う場合…のつもりです。%%わかりづらいのであとで修正しますm(_ _)m。%% すこし修正しました。setMaximumSizeは、ちょっと難しいのかも([http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6200438 Bug ID: 6200438 Frame's size must be validated against maximized bounds when resizing, win32])。 -- [[aterai]] &new{2009-07-27 (月) 12:05:33};
- 最大サイズも同じ要領で・・・とありますが``JFrame#setMaximumSize``はうまくいかないですね(``JDK1.6.0_u1``)色々調べているのですが、いい方法あるんでしょうか? -- [[sawshun]] &new{2009-07-27 (月) 11:51:11};
-- 同じ要領なのは、``ComponentListener``を使う場合…のつもりです。%%わかりづらいのであとで修正しますm(_ _)m。%% すこし修正しました。``setMaximumSize``は、ちょっと難しいのかも([http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6200438 Bug ID: 6200438 Frame's size must be validated against maximized bounds when resizing, win32])。 -- [[aterai]] &new{2009-07-27 (月) 12:05:33};

#comment