你可以使用這個應用程序來解決你的問題。
首先,創建一個名爲properties.txt
的文本文件。其次,爲這個文本文件添加一些默認值。現在,在舞臺關閉時使用setOnCloseRequest
保存Stage
的屬性。接下來,如果您有一個關閉舞臺的Button
,請在使用Button
的onAction
處理程序按下按鈕時保存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);
}
}
}
如果窗口最大化不救的尺寸(您可以在維度保存到0 ,0並且從不使用它們。),只需在加載時保存isMaximized = true並將setMaximized設置爲true即可。如果未將最大化集最大化爲假並保存尺寸。加載時使用preferredSize()或perferredHeight和preferredWidth()。 – Sedrick
這沒有幫助。當窗口最大化時,如果窗口未被最大化,我們需要獲得窗口{X,Y,W,H}。 – JoshuaD