簡體   English   中英

JavaFX:為什么 stage.setResizable(false) 會導致額外的邊距?

[英]JavaFX: Why does stage.setResizable(false) cause additional margins?

這個小型 JavaFX 測試應用程序

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class ApplicationWithNonResizableStage extends Application {

    public static void main(final String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage primaryStage) throws Exception {
        final Rectangle rectangle = new Rectangle(200, 100, Color.POWDERBLUE);
        final BorderPane pane = new BorderPane(rectangle);
        final Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}

生成一個帶有不需要的填充的窗口:

有余量的階段

刪除調用primaryStage.setResizable(false)也會刪除效果:

無余地階段

出了什么問題?

正如已經評論過的,這種不同的 !/resizable 行為聞起來像一個錯誤(有人可能會考慮提交問題;-)

一種更短(比手動調整大小)的方法是明確地使舞台適合場景:

primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.sizeToScene();

只是注意到這適用於 jdk8,但不適用於 jdk7。

為方便起見,錯誤更新:jewelsea 提交的原始報告已作為(在新坐標中) https://bugs.openjdk.java.net/browse/JDK-8089008的副本關閉 - 仍然打開,評論為勝利 -只有。

雖然這不是解釋,但它解決了問題:

@Override
public void start(final Stage primaryStage) throws Exception {
    final Dimension d = new Dimension(210, 110);
    final Rectangle rectangle = new Rectangle(d.width, d.height, Color.POWDERBLUE);
    final BorderPane pane = new BorderPane(rectangle);
    pane.maxWidth(d.height);
    pane.maxWidth(d.width);
    final Scene scene = new Scene(pane, d.width, d.height);
    primaryStage.setScene(scene);
    primaryStage.setResizable(false);
    primaryStage.setWidth(d.width);
    primaryStage.setHeight(d.height);
    primaryStage.show();
}

關鍵是在合適的時間設置Stage寬度和高度。

嘗試為您想要框架的大小創建一個維度 - Dimension name = new Dimension(height,width) - 然后將該維度大小設置為矩形 BorderPane 和 Scene。 您確實希望所有東西的大小都相同嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM