【问题标题】:Primary stage changes scene size初级阶段改变场景大小
【发布时间】:2013-08-21 04:14:39
【问题描述】:

我在场景构建器中设计了一个带有几个按钮的简单场景。没什么特别的:

我已经把它放在初级阶段,我觉得没问题:

但是现在,如果我将初级阶段的 resizable 属性更改为 false,则场景在右侧和底部增长:

为什么场景在右侧和底部增长?在我看来,没有什么特别的:

@Override
public void start(Stage stage) throws Exception {
    AnchorPane root = FXMLLoader.load(getClass().getResource("/fxml/start.fxml"));
    Scene scene = new Scene(root);
    stage.setResizable(false);
    stage.setTitle("Game");
    stage.setScene(scene);
    stage.centerOnScreen();
    stage.show();       

}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<AnchorPane fx:id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="310.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="bla.bla.bla.Controller">
  <children>
    <Button fx:id="btnNewGame" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="New Game" />
    <Button fx:id="btnLoadGame" layoutX="10.0" layoutY="70.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="Load Game" />
    <Button fx:id="btnEditor" layoutX="10.0" layoutY="130.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="Editor" />
    <Button fx:id="btnProperties" layoutX="10.0" layoutY="190.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="Properties" />
    <Button fx:id="btnExit" layoutX="10.0" layoutY="250.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="Exit" />
  </children>
  <stylesheets>
    <URL value="@start.css" />
  </stylesheets>
</AnchorPane>

还有css:

#rootPane{
-fx-background-color: #333333;
}

.button{
-fx-background-color: #555555;
-fx-text-fill: silver;
-fx-background-radius: 0;
-fx-border-radius: 0;
-fx-border-color: #444444;   
}

提前致谢!

【问题讨论】:

  • 请发布 fxml。
  • 我已经添加了 fxml。
  • 在屏幕生成器中,将所有按钮的首选项宽度/高度更改为计算值(或 max_size? - 现在不在 FX 生成器前面)并将最大宽度/高度更改为 max_Value 应该可以解决问题.

标签: java css javafx-2


【解决方案1】:

我对 FXML 不熟悉,但您是否在启动方法中尝试过 stage.sizeToScene();?

编辑:很抱歉它不起作用,阅读 Sebastian 的答案很明显。

【讨论】:

  • 感谢您的意见,但没有奏效。效果和上面写的一样。
【解决方案2】:

我的测试程序如下所示:

@Override
  public void start(final Stage stage) throws Exception {

    StackPane root = FXMLLoader.load(getClass().getResource("stage.fxml"));
    final Scene scene = new Scene(root);
    scene.widthProperty().addListener(new ChangeListener<Number>() {
      @Override
      public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
        System.out.println("width: "+number+" -> "+number2);
      }
    });
    boolean resizable = true;
    stage.setResizable(resizable);
    stage.setTitle("Stage Resizable "+ resizable);
    stage.setScene(scene);
    stage.centerOnScreen();
    stage.show();
  }

例如,当 Stage#reziable 标志设置为 false 时,setWidth 会被调用两次。使用您的示例 fxml,我的输出如下:

width: 0 -> 320
width: 320 -> 330

而当 resizable 为 true 时,setWidth 只被调用一次:

width: 0 -> 320

我看不到额外的 10 个像素来自哪里,这与场景内部无关(我切换到一个空的 StackPane 进行测试)。 我认为这是 JavaFX 中的一个错误,快速 jira 搜索发现以下内容:https://javafx-jira.kenai.com/browse/RT-30647

所以,如果你想解决这个问题,我建议你支持这个问题;)

【讨论】:

  • 谢谢。我是 JavaFX 的新手,所以我首先认为这可能是关于 JavaFX 的特别之处。但第二眼看,我也这么认为。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多