Swing/ConstrainedProportions のバックアップの現在との差分(No.12)
- category: swing folder: ConstrainedProportions title: JFrameの縦横比を一定にする tags: [JFrame] author: aterai pubdate: 2006-11-06T14:28:33+09:00 description: JFrameの幅と高さの比率が一定になるように制限します。 image:
概要
JFrame
の幅と高さの比率が一定になるように制限します。
Screenshot
Advertisement
サンプルコード
private static final int MW = 320;
private static final int MH = 200;
// ...
frame.addComponentListener(new ComponentAdapter() {
@Override public void componentResized(ComponentEvent e) {
int fw = frame.getSize().width;
int fh = MH * fw / MW;
frame.setSize(MW > fw ? MW : fw, MH > fh ? MH : fh);
}
});
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JFrame
のサイズを変更した後、その幅から縦横比が変更前と同じになるような高さを計算してJFrame#setSize(int,int)
で設定し直しています。
上記のサンプルでは、JFrame
のサイズを変更した後その幅から縦横比が変更前と同じになるような高さを計算してJFrame#setSize(int,int)
で設定し直しています。
Windows 10
+Java 8
以降では正常に動作しない