• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JFrameの位置・サイズを記憶する
#navi(../)
*JFrameの位置・サイズを記憶する [#c6d374e2]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2003-09-25~
更新日:&lastmod;

#contents

**概要 [#n874c5f3]
Preferences(レジストリなど)にフレーム(パネル)の位置・サイズを記憶しておきます。

//#screenshot
#screenshot

**サンプルコード [#o43d0c70]
 public abstract class AbstractHogePanel extends JPanel{
   final private Point     pos = new Point(0, 0);
   final private Dimension dim = new Dimension(900, 800);
   private JFrame frame;
   
   public AbstractHogePanel() {
     super();
     frame = new JFrame();
     frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
     frame.addWindowListener(new java.awt.event.WindowAdapter() {
       public void windowClosing(WindowEvent e) {
         exitActionPerformed(null);
#code{{
 public MainPanel(final JFrame frame) {
   super(new BorderLayout());
   this.frame = frame;
   this.prefs = Preferences.userNodeForPackage(getClass());
   frame.addWindowListener(new java.awt.event.WindowAdapter() {
     public void windowClosing(WindowEvent e) {
       saveLocation();
       frame.dispose();
     }
   });
   frame.addComponentListener(new ComponentAdapter() {
     public void componentMoved(ComponentEvent e) {
       if(frame.getExtendedState()==JFrame.NORMAL) {
         try{
           pos.setLocation(frame.getLocationOnScreen());
         }catch(IllegalComponentStateException icse) {}
       }
     });
     frame.addComponentListener(new ComponentAdapter() {
       public void componentMoved(ComponentEvent e) {
         JFrame frm = (JFrame)e.getSource();
         if(frm.getExtendedState()==JFrame.NORMAL) {
           try{
             pos.setLocation(frm.getLocationOnScreen());
           }catch(IllegalComponentStateException icse) {}
         }
     }
     public void componentResized(ComponentEvent e) {
       if(frame.getExtendedState()==JFrame.NORMAL) {
         dim.setSize(getSize());
       }
       public void componentResized(ComponentEvent e) {
         JFrame frm = (JFrame)e.getSource();
         if(frm.getExtendedState()==JFrame.NORMAL) {
           dim.setSize(getSize());
         }
       }
       //void componentHidden(ComponentEvent e) {}
       //void componentShown(ComponentEvent e)
     });
   }
   
   private String prefix = "";
   protected void setPrefix(String str) {
     str = str.toLowerCase().trim();
     if(str!=null && str.length()>0) {
       prefix = str+"_";
     }
   }
   protected void initLocation(Preferences lprefs) {
     int xpos = lprefs.getInt(prefix+"locx", pos.x);
     int ypos = lprefs.getInt(prefix+"locy", pos.y);
     pos.setLocation(xpos,ypos);
     getFrame().setLocation(pos.x, pos.y);
     int wdim = lprefs.getInt(prefix+"dimw", dim.width);
     int hdim = lprefs.getInt(prefix+"dimh", dim.height);
     dim.setSize(wdim, hdim);
     this.setPreferredSize(dim);
   }
   protected void saveLocation(Preferences lprefs) {
     lprefs.putInt(prefix+"locx", pos.x);
     lprefs.putInt(prefix+"locy", pos.y);
     lprefs.putInt(prefix+"dimw", dim.width);
     lprefs.putInt(prefix+"dimh", dim.height);
     try{
       lprefs.flush();
     }catch(java.util.prefs.BackingStoreException e) {
       Logger.global.info("バッキングストアに障害が発生したか、
       バッキングストアにアクセスできないことが原因で、設定操作を完了
       できなかった。");
   });
   exitButton.setAction(new AbstractAction("終了") {
     public void actionPerformed(ActionEvent evt) {
       saveLocation();
       frame.dispose();
     }
   }
   public JFrame getFrame() {
     return frame;
   }
   
   public void showPanel() {
     if(getFrame()!=null) {
       JFrame f = getFrame();
       f.setExtendedState(JFrame.NORMAL);
       f.getContentPane().add(this, BorderLayout.CENTER);
       f.pack();
       f.show();
     }
   }
   
   protected class ExitAction extends AbstractAction{
     public ExitAction() {
       super("exit");
     }
   });
   clearButton.setAction(
      new AbstractAction("レジストリなどに保存した値をクリアして終了") {
     public void actionPerformed(ActionEvent evt) {
       exitActionPerformed(evt);
       try{
         prefs.clear();
         prefs.flush();
       }catch(java.util.prefs.BackingStoreException e) {
         e.printStackTrace();
       }
       frame.dispose();
     }
   });
   Box box = Box.createHorizontalBox();
   box.add(Box.createHorizontalGlue());
   box.add(clearButton);
   box.add(Box.createHorizontalStrut(2));
   box.add(exitButton);
   add(new JLabel("テスト"), BorderLayout.CENTER);
   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(java.util.prefs.BackingStoreException e) {
     e.printStackTrace();
   }
   abstract public void exitActionPerformed(ActionEvent e);
 }
}}
//-&jnlp;
-&jar;
-&zip;

**解説 [#u2bec200]
上記のサンプルコードは、exitActionPerformedを以下の要領で実装して使います。
上記のサンプルでは、対象フレームが最大化、最小化された状態で終了した場合、その前の位置サイズを記憶しておくようになっています。

 public void exitActionPerformed(ActionEvent e) {
   Preferences prefs = Preferences.userNodeForPackage(getClass());
   //Preferences prefs = getPreferences();
   //prefs.putBoolean("file_flag", xmlCheck.isSelected());
   saveLocation(prefs);
   JFrame fram = getFrame();
   //fram.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
   fram.dispose();
 }

対象フレームが最大化、最小化された状態で終了した場合、その前の位置サイズを記憶するようになっています。

**コメント [#g4e001a1]
#comment