Swing/CenterFrame のバックアップの現在との差分(No.16)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/CenterFrame へ行く。
- 1 (2004-03-11 (木) 15:48:20)
- 2 (2004-06-02 (水) 09:51:48)
- 3 (2004-06-07 (月) 11:32:08)
- 4 (2004-08-25 (水) 07:15:35)
- 5 (2004-10-08 (金) 06:18:08)
- 6 (2004-11-04 (木) 10:03:04)
- 7 (2005-04-28 (木) 04:32:51)
- 8 (2005-05-11 (水) 00:22:09)
- 9 (2005-10-12 (水) 21:18:33)
- 10 (2006-02-27 (月) 15:32:15)
- 11 (2006-04-12 (水) 19:36:21)
- 12 (2006-05-28 (日) 01:49:34)
- 13 (2006-10-12 (木) 17:37:00)
- 14 (2007-03-06 (火) 03:36:16)
- 15 (2007-05-30 (水) 21:02:17)
- 16 (2008-09-12 (金) 14:26:07)
- 17 (2013-02-20 (水) 15:40:23)
- 18 (2013-02-26 (火) 16:02:34)
- 19 (2013-05-10 (金) 10:53:02)
- 20 (2014-11-19 (水) 14:59:37)
- 21 (2015-12-26 (土) 15:59:16)
- 22 (2016-08-18 (木) 12:43:29)
- 23 (2017-03-31 (金) 16:08:04)
- 24 (2018-03-13 (火) 14:12:32)
- 25 (2020-03-19 (木) 20:00:00)
- 26 (2021-09-24 (金) 14:46:58)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JFrameをスクリーン中央に表示 #navi(../) *JFrameをスクリーン中央に表示 [#b05e1eeb] Posted by [[terai]] at 2004-01-07 --- category: swing folder: CenterFrame title: JFrameをスクリーン中央に表示 tags: [JFrame] author: aterai pubdate: 2003-12-29T15:48:20+09:00 description: JFrameやJDialogなどのWindowがスクリーンの中央に配置されるように設定します。 image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTIvEn-69I/AAAAAAAAATQ/Fw4dLY4C0EE/s800/CenterFrame.png --- * 概要 [#summary] `JFrame`や`JDialog`などの`Window`がスクリーンの中央に配置されるように設定します。 #contents #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTIvEn-69I/AAAAAAAAATQ/Fw4dLY4C0EE/s800/CenterFrame.png) **概要 [#r4fdfcc6] フレームやダイアログなどをスクリーンの中央に表示します。 -&jnlp; -&jar; -&zip; #screenshot **サンプルコード [#o0de8a54] #code{{ * サンプルコード [#sourcecode] #code(link){{ JFrame frame = new JFrame("フレームをスクリーン中央に表示"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(new MainPanel()); frame.pack(); frame.setLocationRelativeTo(null); //以下は自前で位置を計算する場合 //Rectangle screen = frame.getGraphicsConfiguration().getBounds(); //frame.setLocation(screen.x + screen.width/2 - frame.getSize().width/2, // screen.y + screen.height/2 - frame.getSize().height/2); // 以下は自前で中央配置になる位置を計算する場合 // Rectangle screen = frame.getGraphicsConfiguration().getBounds(); // frame.setLocation(screen.x + screen.width / 2 - frame.getSize().width / 2, // screen.y + screen.height / 2 - frame.getSize().height / 2); frame.setVisible(true); }} **解説 [#v308e626] JFrame#setLocationRelativeToメソッドで、基準となる親ウィンドウをnullにすると、そのフレームは画面中央に表示されます。 * 解説 [#explanation] `Window#setLocationRelativeTo(Component)`メソッドで引数で指定した基準となるコンポーネントを`null`にした場合、`JFrame`は画面中央に配置されます。 JFrame#setLocationメソッドで任意の位置を指定する場合は、フレームの左上隅座標を計算してやります。 - `JFrame`の左上隅座標を計算して`JFrame#setLocation(...)`メソッドを使用し、`JFrame`を画面中央に配置する方法もある #code{{ Rectangle screen = frame.getGraphicsConfiguration().getBounds(); frame.setLocation( screen.x + screen.width / 2 - frame.getSize().width / 2, screen.y + screen.height / 2 - frame.getSize().height / 2); }} - どちらの場合も`JFrame`を`pack()`、または`setSize(int, int)`でそのサイズを設定した後で実行する必要がある どちらも、フレームをpack()したあとで実行するようにしてください。 * コメント [#comment] #comment - `1.4`以降なら、`setLocationRelativeTo(null)`でも中央になりますよ。 -- &user(Wata); &new{2004-06-07 (月) 17:47:08}; -- こんな方法があるのですね。参考になりました。 -- &user(aterai); &new{2004-06-07 (月) 19:26:17}; -- というわけで、`src.zip`などを更新してみました。ありがとうございました。 -- &user(aterai); &new{2004-06-07 (月) 19:44:21}; **参考リンク [#a98bdd9a] -[[Swing [Archive] - How can I open a JFrame at the center of my screen ?>http://forums.sun.com/thread.jspa?threadID=508444]] **コメント [#b68bf302] -1.4以降なら、setLocationRelativeTo(null)でも中央になりますよ。 -- [[Wata]] &new{2004-06-07 (月) 17:47:08}; -こんな方法があったんですね。参考になりました。 -- [[terai]] &new{2004-06-07 (月) 19:26:17}; -というわけで、src.zipなどを更新してみました。 -- [[terai]] &new{2004-06-07 (月) 19:44:21}; #comment