2013-05-27 54 views
0

我正在寫一個Java程序,我從這裏使用安德魯·湯普森的代碼:Best practice for setting JFrame locations設置在屏幕和存儲性能的JFrame位置

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.Properties; 
import java.io.*; 

class RestoreMe { 

    /** This will end up in the current directory 
    A more sensible location is a sub-directory of user.home. 
    (left as an exercise for the reader) */ 
    public static final String fileName = "options.prop"; 

    /** Store location & size of UI */ 
    public static void storeOptions(Frame f) throws Exception { 
     File file = new File(fileName); 
     Properties p = new Properties(); 
     // restore the frame from 'full screen' first! 
     f.setExtendedState(Frame.NORMAL); 
     Rectangle r = f.getBounds(); 
     int x = (int)r.getX(); 
     int y = (int)r.getY(); 
     int w = (int)r.getWidth(); 
     int h = (int)r.getHeight(); 

     p.setProperty("x", "" + x); 
     p.setProperty("y", "" + y); 
     p.setProperty("w", "" + w); 
     p.setProperty("h", "" + h); 

     BufferedWriter br = new BufferedWriter(new FileWriter(file)); 
     p.store(br, "Properties of the user frame"); 
    } 

    /** Restore location & size of UI */ 
    public static void restoreOptions(Frame f) throws IOException { 
     File file = new File(fileName); 
     Properties p = new Properties(); 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     p.load(br); 

     int x = Integer.parseInt(p.getProperty("x")); 
     int y = Integer.parseInt(p.getProperty("y")); 
     int w = Integer.parseInt(p.getProperty("w")); 
     int h = Integer.parseInt(p.getProperty("h")); 

     Rectangle r = new Rectangle(x,y,w,h); 

     f.setBounds(r); 
    } 

    public static void main(String[] args) { 
     final JFrame f = new JFrame("Good Location & Size"); 
     f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     f.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent we) { 
       try { 
        storeOptions(f); 
       } catch(Exception e) { 
        e.printStackTrace(); 
       } 
       System.exit(0); 
      } 
     }); 
     JTextArea ta = new JTextArea(20,50); 
     f.add(ta); 
     f.pack(); 

     File optionsFile = new File(fileName); 
     if (optionsFile.exists()) { 
      try { 
       restoreOptions(f); 
      } catch(IOException ioe) { 
       ioe.printStackTrace(); 
      } 
     } else { 
      f.setLocationByPlatform(true); 
     } 
     f.setVisible(true); 
    } 
} 

我有三個問題:

  1. 我設置全屏窗口(JFrame),然後關閉它,當我重新打開窗口時,它具有全屏的尺寸,但它不是全屏的,而是 。
  2. 此2°問題與Thompson的代碼 無關,當我將窗口移動到屏幕邊框時,它不會落在屏幕邊框後面並與屏幕邊距並排保持不變。
  3. 關於「option.prop」文件,目前 該文件存儲在我的.jar父文件夾中,我想將 存儲在我的.jar程序的同一個文件夾中,我該怎麼做?

感謝

+0

1.應該由'//先從'全屏'恢復幀! f.setExtendedState(Frame.NORMAL);'你是說它不是? 2.我不明白你想說什麼。 3.將選項放在'user.home'的子目錄中,OS製造商一直說不把應用程序設置保存在與應用程序相同的目錄中。 (或任何父目錄)。 –

+0

1.是的我說它不是,2.當我將我的程序的框架移動到屏幕邊框時,它會並排停留在邊框上,而正常程序隱藏在屏幕邊框上。 3.好的,我有紅色把它放在user.home中,但我可以這樣做嗎?用戶文件夾取決於操作系統。 謝謝 – Frank

回答

1

下面是根據安德魯稍微修改的例子。

解決方法1.和3.(考慮到問題的意見)。在最大化之前,它會跟蹤框架的位置和大小。顯然調用f.setExtendedState(Frame.NORMAL)不會立即將幀重新調整爲原始大小。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.Properties; 
import java.io.*; 

class RestoreMe { 

    /** 
    * This will end up in a sub-directory of user.home. 
    * (exercise done by the reader) 
    */ 
    public static final String fileDir = 
      System.getProperty("user.home") 
      + System.getProperty("file.separator") 
      + ".restoreMe"; 
    public static final String fileName = 
      fileDir 
      + System.getProperty("file.separator") 
      + "props.file"; 


    /** 
    * Store location & size of UI 
    */ 
    public static void storeOptions(Frame f, Properties p) throws Exception { 
     File file = new File(fileName); 

     // only need to update extended state in properties 
     p.setProperty("extState", String.valueOf(f.getExtendedState())); 

     BufferedWriter br = new BufferedWriter(new FileWriter(file)); 
     p.store(br, "Properties of the user frame"); 
    } 

    /** 
    * Restore location & size of UI 
    */ 
    public static void restoreOptions(Frame f, Properties p) throws IOException { 
     File file = new File(fileName); 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     p.load(br); 

     int extState = Integer.parseInt(p.getProperty("extState")); 

     int x = Integer.parseInt(p.getProperty("x")); 
     int y = Integer.parseInt(p.getProperty("y")); 
     int w = Integer.parseInt(p.getProperty("w")); 
     int h = Integer.parseInt(p.getProperty("h")); 

     Rectangle r = new Rectangle(x, y, w, h); 
     f.setBounds(r); 
     f.setExtendedState(extState); 
    } 

    public static void main(String[] args) { 
     // we keep track of a single instance of properties 
     final Properties p = new Properties(); 
     final JFrame f = new JFrame("Good Location & Size"); 
     f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     f.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent we) { 
       try { 
        storeOptions(f, p); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       System.exit(0); 
      }    
     }); 
     // keep track of frame movement and update properties accordingly 
     f.addComponentListener(new ComponentAdapter() { 
      @Override 
      public void componentResized(ComponentEvent e) { 
       if (f.getExtendedState() != JFrame.MAXIMIZED_BOTH) { 
        Dimension d = f.getSize(); 
        int w = (int) d.getWidth(); 
        int h = (int) d.getHeight();      
        p.setProperty("w", "" + w); 
        p.setProperty("h", "" + h); 
       } 
      } 
      @Override 
      public void componentMoved(ComponentEvent e) { 
       if (f.getExtendedState() != JFrame.MAXIMIZED_BOTH) { 
        Point l = f.getLocation(); 
        int x = (int) l.getX(); 
        int y = (int) l.getY(); 
        p.setProperty("x", "" + x); 
        p.setProperty("y", "" + y); 
       } 
      }    
     }); 
     JTextArea ta = new JTextArea(20, 50); 
     f.add(ta); 
     f.pack(); 

     // create directory hierarchy for our app 
     (new File(fileDir)).mkdirs(); 

     File optionsFile = new File(fileName); 
     if (optionsFile.exists()) { 
      try { 
       restoreOptions(f, p); 
      } catch (IOException ioe) { 
       ioe.printStackTrace(); 
      } 
     } else { 
      f.setLocationByPlatform(true); 
     } 
     f.setVisible(true); 
    } 
} 

至於2.,你不能指望它被回答,因爲你沒有發佈相關的代碼。我建議你爲此創建另一個問題。