当用户关闭一个程序时,我想保存一些关于主窗口的信息,以便下次用户打开程序时,窗口具有相同的属性。
这对于窗口是否最大化很容易做到:
如果窗口没有最大化,这也很容易做到:
然而,如果窗口被最大化,这些get函数会给出最大化窗口的尺寸和位置。如果我设置了这些值,用户将窗口恢复到非最大化状态,窗口将保持与最大化大小相似,而不是像用户预期的那样缩小。
如何为正在关闭的当前最大化窗口保存“窗口化”阶段尺寸和位置,然后在程序重新打开时恢复这些信息?
你可以使用这个应用来解决你的问题。
首先,创建一个名为properties的文本文件。txt
。第二,向该文本文件添加一些默认值。现在,使用setOnCloseRequest
保存Stage关闭时的属性。接下来,如果您有一个关闭后台的
按钮
,请使用按钮的
操作
处理程序在按下按钮时保存后台的属性。不要忘记在应用程序启动时总是加载这些设置。
财产。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);
}
}
}
问题内容: 我正在使用Swing制作快速且肮脏的动画。我希望窗口最大化。我怎样才能做到这一点? 问题答案: 前提是您要扩展JFrame:
我想初始化一个窗口为最大化,但我不知道如何做。我在Windows7上使用Python3.3和Tkinter 8.6。我想答案就在这里:http://www.tcl.tk/man/Tcl/Tkcmd/wm.htmam.m8但是我不知道如何将它输入到我的python脚本中 此外,我需要得到窗口的宽度和高度(既是最大化的,如果用户重新缩放它之后),但我想我可以自己找到。
如题,我想在JavaFX中制作window,并使其永久最大化(即全屏)。 这是产生错误的示例代码…嗯,意外行为。 请注意 和 。一切正常,直到...我按 ⊞↓( ),此时我的窗口被调整大小,我无法再次最大化它。我正在Windows 10上运行NetBeans的代码。
问题内容: 有没有办法在所有浏览器中手动设置浏览器窗口的最小大小? 问题答案: 你可以试试 一旦视口小于600像素,您将获得一个水平滚动条。这仅适用于支持最小宽度CSS属性的现代浏览器。 我认为不可能限制用户的大小调整,也不应该!
本文向大家介绍用js实现最大化和最小化窗口相关面试题,主要包含被问及用js实现最大化和最小化窗口时的应答技巧和注意事项,需要的朋友参考一下 https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
我的代码创建了一个窗口,并按照我想要的方式排列它...最初。但是,如果我最大化窗口,边框窗格的顶部和底部不会留在中心。它们漂移到左上角和左下角。 我试着禁用最大化窗口选项,但它又一次打乱了页面的外观,顶部和底部都在移动。 这是我的代码: 我昨晚才开始自学JavaFX,但似乎无法弄清楚我哪里出了问题,或者找到了解决问题的方法。 提前感谢任何建议。