TITLE:JFrameの位置・サイズを記憶する

JFrameの位置・サイズを記憶する

編集者:Terai Atsuhiro~

作成日:2003-09-25
更新日:2021-08-20 (金) 13:59:29
  • 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

概要

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

概要

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

サンプルコード

#spanend
#spanadd
public MainPanel(JFrame frame) {
#spanend
  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();
        }
      }
    }

#spandel
#screenshot
#spanend
    @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);
#spanadd
}
#spanend

#spandel
**サンプルコード [#o43d0c70]
#spanend
#spandel
#code{{
#spanend
 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) {}
       }
     }
     public void componentResized(ComponentEvent e) {
       if(frame.getExtendedState()==JFrame.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();
     }
   });
   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();
   }
 }
#spanadd
private void saveLocation() {
#spanend
  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();
  }
#spanadd
}
#spanend
View in GitHub: Java, Kotlin
  • &jar;
  • &zip;

解説

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

解説

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

コメント

参考リンク

コメント