java - 初级阶段改变场景大小

标签 java css javafx-2

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

scenebuilder

我已经把它放在初级阶段了,我觉得还不错:

resizable

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

resizable

为什么场景在右边和底部增长?在我看来,它没有什么特别之处:

@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;   
}

提前致谢!

最佳答案

我的测试程序是这样的:

@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 为真时,setWidth 只被调用一次:

width: 0 -> 320

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

因此,如果您希望解决该问题,我建议您对该问题投赞成票 ;)

关于java - 初级阶段改变场景大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18312563/

相关文章:

java - 错误 : required: android:support. v4.FragmentManager,找到 : android. app.FragmentManager

html - 图像拆分为表 : Remove space between rows

javascript - jquery 使用按钮在小 div 容器中平移大图像

java - JavaFX 8 是否要实现文本字段验证支持?

JavaFX 2.2 删除分页的选定页面标签

当内存已经空闲时,Java 分配更多 RAM

java - 如何在 jackrabbit-standalone 命令行 (cli) 中切换工作区?

java - 将 POST 从一个 api 转发到另一个 api 时出现错误代码 415

css - 样式选项组标签

java - IP 地址问题 java 在 Web 浏览器中部署(使用 dtjava.js 部署)