2017-06-19 70 views
2

當用戶關閉程序時,我想保存一些關於主窗口的信息,所以下次用戶打開程序時,窗口具有相同的屬性。保存並設置「未最大化」的窗口大小?

這容易使窗口是否最大化的事情:

  • 得到: stage.isMaximized()
  • 套裝: stage.setMaximized(布爾最大化);

這也是很容易做到,如果沒有最大化窗口:

  • 得到: stage.getX()/ stage.getY()/ stage.getWidth()/ stage.getHeight ();
  • 設置: stage.setX()/ stage.setY()/ stage.setWidth()/ stage.setHeight();

但是,如果窗口最大化,這些獲取函數會給我們最大化窗口的尺寸和位置。如果我設置這些值並且用戶將窗口恢復到非最大化狀態,則窗口將保持與其最大化尺寸相似,而不是像用戶所期望的那樣縮小。

如何保存正在關閉的當前最大化窗口的「窗口化」階段尺寸和位置,然後在程序重新打開時恢復此信息

+0

如果窗口最大化不救的尺寸(您可以在維度保存到0 ,0並且從不使用它們。),只需在加載時保存isMaximized = true並將setMaximized設置爲true即可。如果未將最大化集最大化爲假並保存尺寸。加載時使用preferredSize()或perferredHeight和preferredWidth()。 – Sedrick

+0

這沒有幫助。當窗口最大化時,如果窗口未被最大化,我們需要獲得窗口{X,Y,W,H}。 – JoshuaD

回答

0

你可以使用這個應用程序來解決你的問題。

首先,創建一個名爲properties.txt的文本文件。其次,爲這個文本文件添加一些默認值。現在,在舞臺關閉時使用setOnCloseRequest保存Stage的屬性。接下來,如果您有一個關閉舞臺的Button,請在使用ButtononAction處理程序按下按鈕時保存Stage的屬性。不要忘記在應用程序啓動時始終加載這些設置。

的properties.txt:

sceneHeight=300.0 
sceneWidth=450.0 
fullScreen=false 

主營:

import java.io.*; 
import java.util.*; 
import java.util.logging.*; 
import javafx.application.*; 
import javafx.event.*; 
import javafx.scene.*; 
import javafx.scene.control.*; 
import javafx.scene.layout.*; 
import javafx.stage.*; 

/** 
* 
* @author Sedrick 
*/ 
public class JavaFXApplication56 extends Application { 

    @Override 
    public void start(Stage primaryStage) 
    { 
     Map<String, String> properties = loadProperties(); 

     Button btn = new Button(); 
     btn.setText("Toggle fullScreen"); 
     btn.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent event) 
      { 
       if (properties.get("fullScreen").endsWith("true")) { 
        properties.put("fullScreen", "false"); 
        primaryStage.setFullScreen(false); 
       } 
       else { 
        properties.put("fullScreen", "true"); 
        primaryStage.setFullScreen(true); 
       } 
      } 
     }); 

     Button btnExit = new Button(); 
     btnExit.setText("Exit"); 
     btnExit.setOnAction((event) -> { 
      properties.put("fullScreen", Boolean.toString(primaryStage.isFullScreen())); 
      if (!primaryStage.isFullScreen()) { 
       properties.put("sceneHeight", Double.toString(primaryStage.getHeight())); 
       properties.put("sceneWidth", Double.toString(primaryStage.getWidth())); 
      } 

      System.out.println("Closing properties:"); 
      properties.forEach((key, value) 
        -> System.out.println("\t" + key + ":" + value)); 
      System.out.println(); 
      saveProperties(properties); 
      primaryStage.close(); 
     }); 

     HBox hBox = new HBox(); 
     hBox.getChildren().addAll(btn, btnExit); 

     BorderPane root = new BorderPane(); 
     root.setBottom(hBox); 

     if (properties.get("fullScreen").equals("true")) { 
      primaryStage.setFullScreen(true); 
     } 
     else { 
      primaryStage.setFullScreen(false); 
     } 

     double sceneWidth = Double.parseDouble(properties.get("sceneWidth")); 
     double sceneHeight = Double.parseDouble(properties.get("sceneHeight")); 
     Scene scene = new Scene(root, sceneWidth, sceneHeight); 

     primaryStage.setOnCloseRequest((event) -> { 
      properties.put("fullScreen", Boolean.toString(primaryStage.isFullScreen())); 
      if (!primaryStage.isFullScreen()) { 
       properties.put("sceneHeight", Double.toString(primaryStage.getHeight())); 
       properties.put("sceneWidth", Double.toString(primaryStage.getWidth())); 
      } 

      System.out.println("Closing properties:"); 
      properties.forEach((key, value) 
        -> System.out.println("\t" + key + ":" + value)); 
      System.out.println(); 
      saveProperties(properties); 
     }); 
     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) 
    { 
     launch(args); 
    } 

    public Map loadProperties() 
    { 

     File file = new File("properties.txt"); 
     Map<String, String> properties = new HashMap(); 
     try { 
      Scanner input = new Scanner(file); 
      while (input.hasNext()) { 
       String[] parts = input.nextLine().split("="); 
       properties.put(parts[0], parts[1]); 
      } 
     } 
     catch (FileNotFoundException ex) { 
      System.out.println(ex.toString()); 
     } 

     System.out.println("Loading properties:"); 
     properties.forEach((key, value) 
       -> System.out.println("\t" + key + ":" + value)); 
     System.out.println(); 
     return properties; 
    } 

    public void saveProperties(Map<String, String> properties) 
    { 
     StringBuilder stringBuilder = new StringBuilder(); 

     for (String key : properties.keySet()) { 
      stringBuilder.append(key).append("=").append(properties.get(key)).append("\n"); 
     } 

     try (BufferedWriter bw = new BufferedWriter(new FileWriter("properties.txt"))) { 
      bw.write(stringBuilder.toString()); 
     } 
     catch (IOException ex) { 
      Logger.getLogger(JavaFXApplication56.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
}