2014-02-13 10 views
0

如何在我的應用程序中使用java.util.Properties文件記住它上一次的狀態。我想我的應用程序在相同的位置和大小相同打開,因爲它有它什麼時候被closed.This是我的代碼部分:當打開窗口時,是否可以使用java.util.Properties-files來設置窗口大小?

private static void createAndShowGUI() { 

    JFrame.setDefaultLookAndFeelDecorated(true); 
    JFrame frame = new JFrame("Infinity"); 

    v demo = new v(); 
    frame.setContentPane(demo.createContentPane()); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.setSize(new Dimension(740, 480)); 
    frame.setVisible(true); 
} 

public static void main(String[] args) { 
    //Schedule a job for the event-dispatching thread: 
    //creating and showing this application's GUI. 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 

      Properties prop = new Properties(); 

      // add some properties 
      prop.setProperty("Height", "200"); 
      prop.put("Width", "1500"); 

      // print the list 
      try { 
       // store the properties list in an output stream 
       prop.store(System.out, "PropertiesDemo"); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      createAndShowGUI(); 
     } 
    }); 
}} 
+0

你試過了嗎? – Sinkingpoint

+0

是的,我上傳我的代碼 – user3061922

回答

0

是,只需調用setProperty方法。然後,在關閉之前,請撥打save

當您啓動時,請按照您現有的方式撥打load


即使比所有這些都好,請使用Preferences

+0

是否有可能檢查我的代碼它不工作? – user3061922

+0

@ user3061922 1)你沒有調用'load'。你只是「存儲」在控制檯上。你需要把它放在一個文件中。你知道什麼是更好的解決方案嗎? [Preferences](http://docs.oracle.com/javase/7/docs/api/java/util/prefs/Preferences.html)類。 –

+0

你是什麼意思加載?我必須使用文件內的屬性製作txt文件,然後加載它? – user3061922

相關問題