---
category: swing
folder: Preferences
title: JFrameの位置・サイズを記憶する
tags: [JFrame, Preferences]
author: aterai
pubdate: 2003-09-29
description: Preferences(レジストリなど)にフレーム(パネル)の位置・サイズを記憶しておきます。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRGT4S7mI/AAAAAAAAAgo/GDUrxdRJ4x4/s800/Preferences.png
---
* Summary [#summary]
`Preferences`(レジストリなど)にフレーム(パネル)の位置・サイズを記憶しておきます。
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRGT4S7mI/AAAAAAAAAgo/GDUrxdRJ4x4/s800/Preferences.png)
* Source Code Examples [#sourcecode]
#code(link){{
public MainPanel(JFrame frame) {
super(new BorderLayout());
this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.prefs = Preferences.userNodeForPackage(getClass());
frame.addWindowListener(new WindowAdapter() {
@Override public void windowClosing(WindowEvent e) {
saveLocation();
e.getWindow().dispose();
}
});
frame.addComponentListener(new ComponentAdapter() {
@Override public void componentMoved(ComponentEvent e) {
JFrame frame = (JFrame) e.getComponent();
if (frame.getExtendedState() == Frame.NORMAL) {
Point pt = frame.getLocationOnScreen();
if (pt.x < 0 || pt.y < 0) {
return;
}
try {
pos.setLocation(pt);
} catch (IllegalComponentStateException icse) {
icse.printStackTrace();
}
}
}
@Override public void componentResized(ComponentEvent e) {
JFrame frame = (JFrame) e.getComponent();
if (frame.getExtendedState() == Frame.NORMAL) {
dim.setSize(getSize());
}
}
});
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
box.add(clearButton);
box.add(Box.createHorizontalStrut(2));
box.add(exitButton);
add(new JLabel("TEST"));
add(box, BorderLayout.SOUTH);
int wdim = prefs.getInt(PREFIX + "dimw", dim.width);
int hdim = prefs.getInt(PREFIX + "dimh", dim.height);
dim.setSize(wdim, hdim);
setPreferredSize(dim);
Rectangle screen = frame.getGraphicsConfiguration().getBounds();
pos.setLocation(screen.x + screen.width / 2 - dim.width / 2,
screen.y + screen.height / 2 - dim.height / 2);
int xpos = prefs.getInt(PREFIX + "locx", pos.x);
int ypos = prefs.getInt(PREFIX + "locy", pos.y);
pos.setLocation(xpos, ypos);
frame.setLocation(pos.x, pos.y);
}
private void saveLocation() {
prefs.putInt(PREFIX + "locx", pos.x);
prefs.putInt(PREFIX + "locy", pos.y);
prefs.putInt(PREFIX + "dimw", dim.width);
prefs.putInt(PREFIX + "dimh", dim.height);
try {
prefs.flush();
} catch (BackingStoreException e) {
e.printStackTrace();
}
}
}}
* Description [#explanation]
* Description [#description]
上記のサンプルでは、対象フレームが最大化、最小化された状態で終了した場合、その前の位置サイズを記憶するよう設定しています。
* Reference [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/util/prefs/Preferences.html Preferences (Java Platform SE 8)]
- [https://docs.oracle.com/javase/jp/8/docs/technotes/guides/preferences/overview.html Preferences APIの概要]
-- メモ: `Java 6`の[https://docs.oracle.com/javase/jp/6/technotes/guides/preferences/overview.html Preferences API の概要]にある「`Java` コレクション `API` の設計に関する `FAQ`」は「`Preferences API` の設計に関する `FAQ`」の間違い
-- `Java 8`の「`Preferences API`の概要」で修正済み
- [[PersistenceServiceを使ってJFrameの位置・サイズを記憶>Swing/PersistenceService]]
* Comment [#comment]
#comment
- メモ: [http://sqs.cmr.sfc.keio.ac.jp/tdiary/20070212.html#p01 Preferences APIがJava6上では動かない - 日々是開発: SQS Development(2007-02-12)] -- &user(aterai); &new{2007-06-14 (木) 14:42:00};
- 最大化した状態で終了すると、`(x, y)`が`(-4, -4)`で記録される。最大化すると、どうやら`Frame.NORMAL`のまま左端に移動してそれから最大扱いになってるようです。だから`componentMoved`が誤爆してる。 -- &user(Tomopy); &new{2007-10-26 (金) 12:20:16};
-- ご指摘ありがとうございます。位置がマイナスの場合は保存しないほうがよさそうですね。修正しておきます。 -- &user(aterai); &new{2007-10-26 (金) 13:37:15};
#comment