• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JFrameをスクリーン中央に表示
#navi(../)
*JFrameをスクリーン中央に表示 [#b05e1eeb]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-01-07~
更新日:&lastmod;
---
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]
フレームやダイアログなどをスクリーンの中央に表示します。
* サンプルコード [#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);
frame.setVisible(true);
}}

#screenshot
* 解説 [#explanation]
`Window#setLocationRelativeTo(Component)`メソッドで引数で指定した基準となるコンポーネントを`null`にした場合、`JFrame`は画面中央に配置されます。

**サンプルコード [#o0de8a54]
 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);
- `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)`でそのサイズを設定した後で実行する必要がある

-&jnlp;
-&jar;
-&zip;
* コメント [#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};

**解説 [#v308e626]
JFrame#setLocationRelativeToメソッドで、基準となる親ウィンドウをnullにすると、そのフレームは画面中央に表示されます。

JFrame#setLocationメソッドで任意の位置を指定する場合は、フレームの左上隅座標を計算してやります。

どちらも、フレームをpack()したあとで実行するようにしてください。

**参考リンク [#a98bdd9a]
-[[How can I open a JFrame at the center of my screen ?>http://forum.java.sun.com/thread.jspa?forumID=57&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