2013-08-19 84 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