Javafx 一些节点赢了';t出现在HBox中
我正在制作一款Yahtzee游戏,只是为了发展我的编程技能,但我似乎无法让所有的标签和单选按钮都出现在HBox上。以下是所有相关代码:Javafx 一些节点赢了';t出现在HBox中,javafx,nodes,vbox,hbox,borderpane,Javafx,Nodes,Vbox,Hbox,Borderpane,我正在制作一款Yahtzee游戏,只是为了发展我的编程技能,但我似乎无法让所有的标签和单选按钮都出现在HBox上。以下是所有相关代码: public class Main extends Application{ Label diceRoll1 = new Label("1"); Label diceRoll2 = new Label("2"); Label diceRoll3 = new Label("3"); Label diceRoll4 = new Label("4"); Label
public class Main extends Application{
Label diceRoll1 = new Label("1");
Label diceRoll2 = new Label("2");
Label diceRoll3 = new Label("3");
Label diceRoll4 = new Label("4");
Label diceRoll5 = new Label("5");
RadioButton holdRButton1 = new RadioButton("Hold");
RadioButton holdRButton2 = new RadioButton("Hold");
RadioButton holdRButton3 = new RadioButton("Hold");
RadioButton holdRButton4 = new RadioButton("Hold");
RadioButton holdRButton5 = new RadioButton("Hold");
Button rollDice = new Button("Roll!");
Stage window;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage)throws Exception{
VBox diceRoll1Layout = new VBox(5);
diceRoll1Layout.getChildren().addAll(diceRoll1, holdRButton1);
VBox diceRoll2Layout = new VBox(5);
diceRoll2Layout.getChildren().addAll(diceRoll2, holdRButton2);
VBox diceRoll3Layout = new VBox(5);
diceRoll3Layout.getChildren().addAll(diceRoll3, holdRButton3);
VBox diceRoll4Layout = new VBox(5);
diceRoll4Layout.getChildren().addAll(diceRoll4, holdRButton4);
VBox diceRoll5Layout = new VBox(5);
diceRoll5Layout.getChildren().addAll(diceRoll5, holdRButton5);
HBox diceRolls = new HBox(5);
diceRolls.getChildren().addAll(diceRoll1Layout,diceRoll2Layout,diceRoll3Layout,
diceRoll4Layout,diceRoll5Layout, rollDice);
BorderPane rootPane = new BorderPane();
rootPane.setBottom(diceRolls);
Scene scene = new Scene(rootPane, 400, 500);
window = primaryStage;
window.setTitle("Yahtzee!");
window.setScene(scene);
window.show();
}
}
但出于某种原因,每当我运行代码时,窗口中只会出现diceRoll1、diceRoll2、holdRButton1、holdRButton2和rollDice。它们的编码都是一样的,所以我不知道为什么它们都不出现。我试着将它们设置为可见,但没有任何区别。我错过了什么吗?这对我来说很好。但是,我强烈建议您不要在初始值设定项中实例化控件(即在声明控件的内联位置)。在
start()
方法中初始化它们。(您仍然可以将它们声明为字段。但是,您可能不需要这样做,只需在start()
中将它们声明为局部变量即可)我之所以在此处声明它们,是因为它们都由以下几种方法使用,而我在此处没有包括这些方法。这比把我需要的所有变量都传递到每个方法中要容易得多。我可能只是将代码复制到一个新项目中,看看它是否在那里工作。你仍然可以在那里声明它们,只是不在那里初始化它们。更新:我在一个新项目中从头开始重建了整个项目,我没有遇到过任何相同的问题。这就是我从简单的做事中得到的。