如何禁用调整大小,同时允许最大化?

huangapple go评论70阅读模式
英文:

How to disable resizing while allowing maximizing?

问题

我有一个JavaFX应用程序,我想知道如何禁用可调整大小的选项,但不禁用最大化按钮,因为我可能需要将窗口最大化。

我正在使用以下代码片段:

stage.setResizable(false);
英文:

I have an application in javaFX, I would like to know how it is possible to disable the resizable option, but not to disable the maximize button, because if I need to maximize the window.

I am using the following snippet:

stage.setResizable (false);

答案1

得分: 2

这个解决方案并不是很优雅或者说不是"干净的代码",但至少它是有效的:

你可以通过为舞台设置最小和最大尺寸,并将两个尺寸设为相同的值来实现。这将导致窗口在用户尝试调整大小时保持不变。但是因为 resizable 属性仍然设置为 true,所以最大化按钮仍然是可用的。不幸的是,最大化的窗口也会具有您输入的最大尺寸,所以它不会真正地最大化。要修复这个问题,你需要在窗口最大化后立即从舞台中移除最大尺寸(这是解决方案中不太美观的部分)。

你可以像这样实现:

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class MaximizableButNotResizableExample extends Application {
    
    public static final int WIDTH = 600;
    public static final int HEIGHT = 400;
    
    //flag that indicates the window is re-maximized by the application (to prevent setting the maximum size when un-maximizing the stage)
    private boolean reMaximize = false;
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = new AnchorPane();
            Scene scene = new Scene(root, WIDTH, HEIGHT);
            primaryStage.setScene(scene);
            primaryStage.show();
            
            //set the stages maximum and minimum size so it can't be resized
            primaryStage.setMinWidth(WIDTH);
            primaryStage.setMaxWidth(WIDTH);
            primaryStage.setMinHeight(HEIGHT);
            primaryStage.setMaxHeight(HEIGHT);
            
            //add a listener to the maximized property to disable the maximum size of the window when it gets maximized
            primaryStage.maximizedProperty().addListener((observer, oldVal, newVal) -> {
                if (newVal) {
                    //remove the maximum size when maximizing the stage
                    primaryStage.setMaxWidth(Integer.MAX_VALUE);
                    primaryStage.setMaxHeight(Integer.MAX_VALUE);
                    if (!reMaximize) {
                        //re-maximize the stage programmatically to make it realize there is no maximum size anymore
                        reMaximize = true;
                        primaryStage.setMaximized(false);
                        primaryStage.setMaximized(true);
                    }
                    reMaximize = false;
                }
                else {
                    //set the maximum size only if the un-maximize was caused by the user
                    if (!reMaximize) {
                        primaryStage.setMaxWidth(WIDTH);
                        primaryStage.setMaxHeight(HEIGHT);
                    }
                }
            });
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我真诚地希望 JavaFX 能够有一个更好的解决方案来处理这个问题,我可能只是还没有发现它。但如果没有的话,这个解决方法也应该能够满足需求。

英文:

This solution is not realy pretty or 'clean code', but at least it's working:

You can achieve this by setting a minimum and maximum size to the stage, with the same value. That will cause the window to not be changed in size if the user tries to resize it. But because the resizable property is still set to true the maximize button is still enabled. Unfortunately the maximized window will also have the maximum size, that you entered, so it won't be realy maximized. To fix this you need to remove the maximum size from the stage as soon as the window is maximized (which is the ugly part of this solution).

You can do it like this:

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class MaximizableButNotResizableExample extends Application {
public static final int WIDTH = 600;
public static final int HEIGHT = 400;
//flag that indicates the window is re-maximized by the application (to prevent setting the maximum size when un-maximizing the stage)
private boolean reMaximize = false;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
Parent root = new AnchorPane();
Scene scene = new Scene(root, WIDTH, HEIGHT);
primaryStage.setScene(scene);
primaryStage.show();
//set the stages maximum and minimum size so it can't be resized
primaryStage.setMinWidth(WIDTH);
primaryStage.setMaxWidth(WIDTH);
primaryStage.setMinHeight(HEIGHT);
primaryStage.setMaxHeight(HEIGHT);
//add a listener to the maximized property to disable the maximum size of the window when it gets maximized
primaryStage.maximizedProperty().addListener((observer, oldVal, newVal) -> {
if (newVal) {
//remove the maximum size when maximizing the stage
primaryStage.setMaxWidth(Integer.MAX_VALUE);
primaryStage.setMaxHeight(Integer.MAX_VALUE);
if (!reMaximize) {
//re-maximize the stage programmatically to make it realize there is no maximum size anymore
reMaximize = true;
primaryStage.setMaximized(false);
primaryStage.setMaximized(true);
}
reMaximize = false;
}
else {
//set the maximum size only if the un-maximize was caused by the user
if (!reMaximize) {
primaryStage.setMaxWidth(WIDTH);
primaryStage.setMaxHeight(HEIGHT);
}
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
}

I realy hope javafx has a better solution for this, that I just haven't found, but if not, this workarround should also do.

huangapple
  • 本文由 发表于 2020年8月30日 11:14:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63653625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定