start()
でラベルのテキストを変更または設定しようとしているときメソッド(またはそれ以前)は動作していないようです。
同じことは他のことにも言えます。ボタンにonActionを設定-
start()
で設定すると機能しません
。
後でそれらを
nextTurn()
で設定した場合ボタンによってアクティブにされるメソッド、それらは設定されます。
ラベルと
onAction
の設定FXMLを追加する前は動作していました。
値がFXMLの値にリセットされる可能性があると思います。 FXMLに値がない場合、ラベルは空です。
その場合(FXMLベースの値は
start()
で設定された値をオーバーライドします)メソッド)、ラベルなどの初期値はどこに設定すればよいですか?初期値は、画面を表示する前に計算する必要があるため、FXMLに直接入力することはできません。
コードの下(さまざまなオプションを試していたやや厄介な原因):
teプログラムのコアクラス:
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>
@FXMLアノテーションが付けられたノードをインスタンス化しないでください。それらはJavaFXによって挿入され、再割り当てされます。すべての
new Label()
を取り除く 等 また、Applicationクラスをコントローラーとして使用しないでください。ラベル、ボタン、イベントハンドラーを別のクラスに移動します。次に、fxmlに#onAction=yourEventHandlerMethod
を入れます 。 JavaFXサポート初期化ロジックを配置できるコントローラーのメソッド。