2013-08-19 88 views
2

我设计了一个场景制作器中带有几个按钮的简单场景。没什么特别对:小学舞台改变场景大小

scenebuilder http://i41.tinypic.com/2zzlzls.jpg

我已经把那初级阶段,它看起来OK我:

resizable http://i39.tinypic.com/8zpr8n.jpg

但现在,如果我改变的可调整大小的属性主要舞台为假,场景在右侧和底部增长:

resizable http://i41.tinypic.com/1zea6i0.jpg

为什么场景会在右侧和底部增长?在我oppionion没有什么特别的就可以了:

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

提前感谢!

+0

请发布fxml。 – Sebastian

+0

我已经添加了fxml。 – Qri

+0

在Sceen构建器中,将所有按钮的pref宽度/高度更改为计算(或max_size? - 现在不在FX构建器之前),将最大宽度/高度更改为max_Value也应该这样做。 – assylias

回答

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(); 
    } 

当舞台#reziable标志被设置为false,例如,setWidth被调用了两次。随着你的榜样FXML,我的输出如下:

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

而当调整大小是真实的,setWidth只调用一次:

width: 0 -> 320 

我不能看到那里的10个额外的像素是从哪里来的,这与场景的内部没有任何关系(为了测试,我切换到一个空的StackPane)。 我认为这是JavaFX的一个bug,以及快速的JIRA搜索发现以下https://javafx-jira.kenai.com/browse/RT-30647

所以,如果你想要的是固定的,我建议你给予好评这个问题;)

+0

谢谢。我是JavaFX的新手,所以我首先想到的可能是关于JavaFX的特别知识。但在第二次看来,我也假设了这一点。 – Qri

0

我m不熟悉FXML,但是你在启动方法中尝试stage.sizeToScene();

编辑:对不起,它不起作用,阅读塞巴斯蒂安的答案使它很明显。

+0

感谢您的意见,但没有奏效。与上面所写的效果相同。 – Qri