Swing/Preferences のバックアップソース(No.18)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/Preferences へ行く。
- 1 (2004-05-10 (月) 12:45:22)
- 2 (2004-06-02 (水) 09:58:32)
- 3 (2004-08-04 (水) 03:07:23)
- 4 (2004-10-08 (金) 06:24:19)
- 5 (2004-11-04 (木) 10:10:03)
- 6 (2005-02-03 (木) 02:04:18)
- 7 (2005-04-28 (木) 04:32:37)
- 8 (2005-06-29 (水) 22:03:11)
- 9 (2005-11-03 (木) 16:30:33)
- 10 (2006-02-27 (月) 16:18:57)
- 11 (2006-05-30 (火) 11:29:26)
- 12 (2007-05-10 (木) 10:48:30)
- 13 (2007-06-14 (木) 14:42:00)
- 14 (2007-10-26 (金) 12:20:16)
- 15 (2007-10-26 (金) 13:37:15)
- 16 (2008-04-24 (木) 19:54:41)
- 17 (2011-05-04 (水) 18:57:53)
- 18 (2012-03-26 (月) 15:46:11)
- 19 (2013-02-26 (火) 14:53:13)
- 20 (2013-05-03 (金) 23:46:28)
- 21 (2014-12-25 (木) 16:08:29)
- 22 (2015-03-02 (月) 11:17:32)
- 23 (2015-06-23 (火) 16:15:40)
- 24 (2016-06-01 (水) 18:43:35)
- 25 (2017-04-04 (火) 14:13:45)
- 26 (2017-09-08 (金) 19:16:43)
- 27 (2018-02-27 (火) 14:34:35)
- 28 (2020-03-06 (金) 16:27:52)
- 29 (2021-08-20 (金) 13:59:29)
TITLE:JFrameの位置・サイズを記憶する #navi(../) RIGHT:Posted by &author(aterai); at 2003-09-25 *JFrameの位置・サイズを記憶する [#c6d374e2] Preferences(レジストリなど)にフレーム(パネル)の位置・サイズを記憶しておきます。 //-&jnlp; -&jar; -&zip; //#screenshot #ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTRGT4S7mI/AAAAAAAAAgo/GDUrxdRJ4x4/s800/Preferences.png) **サンプルコード [#o43d0c70] #code{{ public MainPanel(final JFrame frame) { super(new BorderLayout()); this.frame = frame; this.prefs = Preferences.userNodeForPackage(getClass()); frame.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(WindowEvent e) { saveLocation(); frame.dispose(); } }); frame.addComponentListener(new ComponentAdapter() { @Override public void componentMoved(ComponentEvent e) { if(frame.getExtendedState()==JFrame.NORMAL) { Point pt = frame.getLocationOnScreen(); if(pt.x<0 || pt.y<0) return; try{ pos.setLocation(pt); }catch(IllegalComponentStateException icse) {} } } @Override public void componentResized(ComponentEvent e) { if(frame.getExtendedState()==JFrame.NORMAL) { dim.setSize(getSize()); } } }); exitButton.setAction(new AbstractAction("終了") { @Override public void actionPerformed(ActionEvent evt) { saveLocation(); frame.dispose(); } }); clearButton.setAction( new AbstractAction("レジストリなどに保存した値をクリアして終了") { @Override 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); 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(java.util.prefs.BackingStoreException e) { e.printStackTrace(); } } }} **解説 [#u2bec200] 上記のサンプルでは、対象フレームが最大化、最小化された状態で終了した場合、その前の位置サイズを記憶しておくようになっています。 **参考リンク [#g94e34d8] -[http://java.sun.com/javase/ja/6/docs/ja/technotes/guides/preferences/overview.html Preferences API の概要] --この概要にある「Java コレクション API の設計に関する FAQ」は多分、「Preferences API の設計に関する FAQ」 **コメント [#g4e001a1] - メモ: [http://sqs.cmr.sfc.keio.ac.jp/tdiary/20070212.html#p01 Preferences APIがJava6上では動かない - 日々是開発: SQS Development(2007-02-12)] -- [[aterai]] &new{2007-06-14 (木) 14:42:00}; - 最大化した状態で終了すると、x,yが-4,-4で記録される。最大化すると、どうやらJFrame.NORMALのまま左端に移動してそれから最大扱いになってるようです。だからcomponentMovedが誤爆してる。 -- [[Tomopy]] &new{2007-10-26 (金) 12:20:16}; -- ご指摘ありがとうございます。位置がマイナスの場合は、保存しないほうがよさそうですね。修正しておきます。 -- [[aterai]] &new{2007-10-26 (金) 13:37:15}; #comment