Swing/CenterFrame のバックアップソース(No.20)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - 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をスクリーン中央に表示 tags: [JFrame] author: aterai pubdate: 2003-12-29 description: フレームやダイアログなどをスクリーンの中央に表示します。 --- * 概要 [#b05e1eeb] フレームやダイアログなどをスクリーンの中央に表示します。 #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTIvEn-69I/AAAAAAAAATQ/Fw4dLY4C0EE/s800/CenterFrame.png) * サンプルコード [#o0de8a54] #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); frame.setVisible(true); }} * 解説 [#v308e626] `JFrame#setLocationRelativeTo`メソッドで、基準となる親ウィンドウを`null`にすると、そのフレームは画面中央に表示されます。 `JFrame#setLocation`メソッドで任意の位置を指定する場合は、フレームの左上隅座標を計算します。 どちらも、フレームを`pack()`、もしくは`setSize(int,int)`した後で実行するようにしてください。 * コメント [#b68bf302] #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}; #comment