当我试图在start()
方法(或更早)中更改或设置标签文字时,它似乎无法正常工作。
其他事情也是如此,例如。为按钮设置onAction - 在start()
中设置时不起作用。
如果我稍后在按钮激活的nextTurn()
方法中设置这些内容,则会设置它们。
在添加FXML之前设置标签和onAction
。
我怀疑这些值可能会重置为它们在FXML中的含义。如果FXML中没有值,则标签为空。
如果是这种情况(FXML基值覆盖start()
方法中设置的值),我应该在哪里设置标签和其他东西的初始值?假设初始值不能直接在FXML中输入,因为它们需要在显示屏幕之前计算。
代码下面(因为我尝试了各种选项,有点凌乱):
核心课程:
public class TheCitadel extends Application {
GameState gameState = GameInitializer.initializeGame(new GameState());
PlayerCamp playerCamp = (PlayerCamp) gameState.getCamps().get(0);
//Labels
@FXML
Label monthLabel = new Label();
@FXML
Label populationLabel = new Label();
@FXML
Label foodLabel = new Label();
@FXML
Label tradeGoodsLabel = new Label();
@FXML
Label craftsmenLabel = new Label();
//Buttons
@FXML
Button nextTurnButton = new Button("Next turn");
@FXML
Button addCreaftsmenButton = new Button("Add Craftsmen");
@FXML
Button removeCreaftsmenButton = new Button("Remove Craftsmen");
@Override
public void start(Stage primaryStage) throws Exception {
//stage and layout
Stage mainWindow = primaryStage;
mainWindow.setTitle("The Citadel");
//GridPane layout = new GridPane();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/MainScreen.fxml"));
Parent layout = loader.load();
Scene mainScene = new Scene(layout);
mainWindow.setScene(mainScene);
mainWindow.show();
monthLabel = new Label("Month: " + gameState.getTime().getCurrentMonth().toString());
populationLabel = new Label("Population: " + String.valueOf(playerCamp.getPopulation()));
populationLabel.setText ("Population: " + String.valueOf(playerCamp.getPopulation()));
foodLabel = new Label("Food: " + String.valueOf(playerCamp.getFood()));
tradeGoodsLabel = new Label("Trade goods: " + String.valueOf(playerCamp.getTradeGoods()));
craftsmenLabel = new Label("Craftsmen: " + String.valueOf(playerCamp.getPopulationAsCraftsmen()));
//buttons
nextTurnButton.setOnAction(e -> {
proceedGameTime();
monthLabel.setText(gameState.getTime().getCurrentMonth().toString());
populationLabel.setText("Population: " + String.valueOf(playerCamp.getPopulation()));
foodLabel.setText("Food: " + String.valueOf(playerCamp.getFood()));
tradeGoodsLabel.setText("Trade goods: " + String.valueOf(playerCamp.getTradeGoods()));
craftsmenLabel.setText("Craftsmen: " + String.valueOf(playerCamp.getPopulationAsCraftsmen()));
}
);
addCreaftsmenButton.setOnAction(e -> {
if (playerCamp.getPopulation() >= 1) {
playerCamp.setPopulation(playerCamp.getPopulation() - 1);
populationLabel.setText("Population: " + String.valueOf(playerCamp.getPopulation()));
playerCamp.setPopulationAsCraftsmen(playerCamp.getPopulationAsCraftsmen() + 1);
craftsmenLabel.setText("Craftsmen: " + String.valueOf(playerCamp.getPopulationAsCraftsmen()));
}
});
removeCreaftsmenButton.setOnAction(e -> {
if (playerCamp.getPopulationAsCraftsmen() >= 1) {
playerCamp.setPopulation(playerCamp.getPopulation() + 1);
populationLabel.setText("Population: " + String.valueOf(playerCamp.getPopulation()));
playerCamp.setPopulationAsCraftsmen(playerCamp.getPopulationAsCraftsmen() - 1);
craftsmenLabel.setText("Craftsmen: " + String.valueOf(playerCamp.getPopulationAsCraftsmen()));
}
});
//set layout
// layout.getChildren().addAll(nextTurnButton, monthLabel, craftsmenLabel, populationLabel, foodLabel, tradeGoodsLabel, addCreaftsmenButton, removeCreaftsmenButton);
/*
layout.setPadding(new Insets(10, 10, 10, 10));
layout.setVgap(5);
layout.setHgap(5);
GridPane.setConstraints(textArea, 1, 0);
GridPane.setConstraints(nextTurnButton, 1, 2);
GridPane.setConstraints(monthLabel, 0, 2);
GridPane.setConstraints(populationLabel, 0, 0);
GridPane.setConstraints(foodLabel, 0, 1);
GridPane.setConstraints(tradeGoodsLabel, 1, 1);
GridPane.setConstraints(addCreaftsmenButton, 2, 0);
GridPane.setConstraints(removeCreaftsmenButton, 1, 0);
GridPane.setConstraints(craftsmenLabel, 3, 0);
*/
}
public static void main(String[] args) {
//TheCitadel citadel = new TheCitadel();
launch(args);
}
@FXML
public void nextTurn () {
proceedGameTime();
//setting labels and button text here works
monthLabel.setText(gameState.getTime().getCurrentMonth().toString());
populationLabel.setText("Population: " +
String.valueOf(playerCamp.getPopulation()));
foodLabel.setText("Food: " + String.valueOf(playerCamp.getFood()));
tradeGoodsLabel.setText("Trade goods: " +
String.valueOf(playerCamp.getTradeGoods()));
craftsmenLabel.setText("Craftsmen: " +
String.valueOf(playerCamp.getPopulationAsCraftsmen()));
addCreaftsmenButton.setText("THIS BUTTON SHOULD HAVE THIS TEXT AND WORK
EARLIER!!!");
//setting onAction here works
addCreaftsmenButton.setOnAction(e -> {
if (playerCamp.getPopulation() >= 1) {
playerCamp.setPopulation(playerCamp.getPopulation() - 1);
populationLabel.setText("Population: " + String.valueOf(playerCamp.getPopulation()));
playerCamp.setPopulationAsCraftsmen(playerCamp.getPopulationAsCraftsmen() + 1);
craftsmenLabel.setText("Craftsmen: " + String.valueOf(playerCamp.getPopulationAsCraftsmen()));
}
});
}
public void proceedGameTime() {
gameState.getTime().proceedGameTime(1);
}
}
-
FXML:
<AnchorPane prefHeight="600.0" prefWidth="335.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.TheCitadel">
<children>
<Button fx:id="nextTurnButton" onAction="#nextTurn" layoutY="551.0" mnemonicParsing="false" prefHeight="48.0" prefWidth="194.0" text="Next turn" AnchorPane.bottomAnchor="1.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
<AnchorPane layoutY="200.0" prefHeight="232.0" prefWidth="335.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
<TabPane layoutX="14.0" layoutY="-17.0" prefHeight="280.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="400.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab text="Untitled Tab 1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="280.0" prefWidth="200.0">
<children>
<Label text="33333333" fx:id="populationLabel" layoutX="14.0" layoutY="9.0" />
<Label fx:id="tradeGoodsLabel" layoutX="14.0" layoutY="40.0" />
<Label fx:id="foodLabel" layoutX="172.0" layoutY="22.0" />
<Button fx:id="addCreaftsmenButton" layoutY="551.0" mnemonicParsing="false" prefHeight="48.0" prefWidth="194.0" text="addCreaftsmenButton" AnchorPane.bottomAnchor="1.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
答案 0 :(得分:1)
您不能实例化使用@FXML注释的节点,它们将由JavaFX注入,并且您正在重新分配它们。摆脱所有new Label()
等。
此外,您不应将Application类用作控制器。将标签,按钮和事件处理程序移动到单独的类。然后,在你的fxml中,输入#onAction=yourEventHandlerMethod
。
JavaFX支持
@FXML
private void initialize()
控制器中的方法,您可以在其中放置初始化逻辑。