当用户关闭程序时,我想保存一些有关主窗口的信息,因此下次用户打开程序时,窗口具有相同的属性。
对于窗口是否最大化,这很容易做到:
如果窗口没有最大化,这也很容易做到:
但是,如果窗口最大化,这些获取功能可以为我们提供最大化窗口的尺寸和位置。如果我设置这些值并且用户将窗口恢复到非最大化状态,则窗口保持与其最大化大小相似,而不是像用户期望的那样缩小。
如何保存"窗口"正在关闭的当前最大化窗口的阶段尺寸和位置,然后在重新打开程序时恢复此信息?
答案 0 :(得分:0)
您可以使用此应用来解决您的问题。
首先,创建一个名为Stage
的文本文件。其次,为此文本文件添加一些默认值。现在,使用setOnCloseRequest
在阶段关闭时保存Button
的属性。接下来,如果您有Stage
关闭舞台,请在使用Button
的{{1}}处理程序按下按钮时保存onAction
的属性。不要忘记在应用程序启动时始终加载这些设置。
的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);
}
}
}