Swing/Preferences のバックアップの現在との差分(No.17)
TITLE:JFrameの位置・サイズを記憶する
Posted by aterai at 2003-09-25
JFrameの位置・サイズを記憶する
Preferences(レジストリなど)にフレーム(パネル)の位置・サイズを記憶しておきます。- category: swing folder: Preferences title: JFrameの位置・サイズを記憶する tags: [JFrame, Preferences] author: aterai pubdate: 2003-09-29 description: Preferences(レジストリなど)にフレーム(パネル)の位置・サイズを記憶しておきます。 image:
概要
Preferences
(レジストリなど)にフレーム(パネル)の位置・サイズを記憶しておきます。
- &jar;
- &zip;
Screenshot
Advertisement
サンプルコード
#spanend
#spandel
public MainPanel(final JFrame frame) {
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
#spanadd
public MainPanel(JFrame frame) {
#spanend
super(new BorderLayout());
this.frame = frame;
this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.prefs = Preferences.userNodeForPackage(getClass());
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.addWindowListener(new WindowAdapter() {
@Override public void windowClosing(WindowEvent e) {
saveLocation();
frame.dispose();
e.getWindow().dispose();
}
});
frame.addComponentListener(new ComponentAdapter() {
public void componentMoved(ComponentEvent e) {
if(frame.getExtendedState()==JFrame.NORMAL) {
@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{
if (pt.x < 0 || pt.y < 0) {
return;
}
try {
pos.setLocation(pt);
}catch(IllegalComponentStateException icse) {}
} catch (IllegalComponentStateException icse) {
icse.printStackTrace();
}
}
}
public void componentResized(ComponentEvent e) {
if(frame.getExtendedState()==JFrame.NORMAL) {
#spanadd
#spanend
@Override public void componentResized(ComponentEvent e) {
JFrame frame = (JFrame) e.getComponent();
if (frame.getExtendedState() == Frame.NORMAL) {
dim.setSize(getSize());
}
}
});
exitButton.setAction(new AbstractAction("終了") {
public void actionPerformed(ActionEvent evt) {
saveLocation();
frame.dispose();
}
});
clearButton.setAction(
new AbstractAction("レジストリなどに保存した値をクリアして終了") {
public void actionPerformed(ActionEvent evt) {
try{
prefs.clear();
prefs.flush();
}catch(java.util.prefs.BackingStoreException e) {
e.printStackTrace();
}
frame.dispose();
}
});
int wdim = prefs.getInt(PREFIX+"dimw", dim.width);
int hdim = prefs.getInt(PREFIX+"dimh", dim.height);
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);
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);
//......
}
#spanadd
#spanend
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.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(java.util.prefs.BackingStoreException e) {
} catch (BackingStoreException e) {
e.printStackTrace();
}
}
解説
上記のサンプルでは、対象フレームが最大化、最小化された状態で終了した場合、その前の位置サイズを記憶しておくようになっています。解説
上記のサンプルでは、対象フレームが最大化、最小化された状態で終了した場合、その前の位置サイズを記憶するよう設定しています。参考リンク
- Preferences API の概要
- この概要にある「Java コレクション API の設計に関する FAQ」は多分、「Preferences API の設計に関する FAQ」
参考リンク
- Preferences (Java Platform SE 8)
- Preferences APIの概要
- メモ:
Java 6
のPreferences API の概要にある「Java
コレクションAPI
の設計に関するFAQ
」は「Preferences API
の設計に関するFAQ
」の間違い -
Java 8
の「Preferences API
の概要」で修正済み
- メモ:
- PersistenceServiceを使ってJFrameの位置・サイズを記憶
コメント
- メモ: Preferences APIがJava6上では動かない - 日々是開発: SQS Development(2007-02-12) -- aterai
- 最大化した状態で終了すると、x,yが-4,-4で記録される。最大化すると、どうやらJFrame.NORMALのまま左端に移動してそれから最大扱いになってるようです。だからcomponentMovedが誤爆してる。 -- Tomopy?
- ご指摘ありがとうございます。位置がマイナスの場合は、保存しないほうがよさそうですね。修正しておきます。 -- aterai
コメント
- メモ: Preferences APIがJava6上では動かない - 日々是開発: SQS Development(2007-02-12) -- aterai
- 最大化した状態で終了すると、
(x, y)
が(-4, -4)
で記録される。最大化すると、どうやらFrame.NORMAL
のまま左端に移動してそれから最大扱いになってるようです。だからcomponentMoved
が誤爆してる。 -- Tomopy- ご指摘ありがとうございます。位置がマイナスの場合は保存しないほうがよさそうですね。修正しておきます。 -- aterai