我的代码存在问题。这是我的第一个UI应用程序。我声明了我的标签,试图绑定一个值并修改它,但没有任何反应。显然问题是我的标签位于另一个窗格内的窗格内。在这种情况下如何修改我的标签?
Main.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("fxml/gui.fxml"));
primaryStage.setTitle("program name");
primaryStage.setScene(new Scene(root, 1280, 720));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
myClasses \ Controller.java
package myClasses;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable{
public Button hourglass;
public Label currentMonthLabel;
public SimpleIntegerProperty pastMonths = new SimpleIntegerProperty(1);
@Override
public void initialize(URL location, ResourceBundle resources) {
currentMonthLabel.textProperty().bind(pastMonths.asString());
hourglass.setOnAction(e-> {
pastMonths.setValue(pastMonths.getValue() + 1);
System.out.println(pastMonths.getValue());
});
}
}
FXML \ gui.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:controller="myClasses.Controller" fx:id="anchorPane" maxHeight="1280.0" maxWidth="1280.0" minHeight="1280.0" minWidth="720.0" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<SplitPane dividerPositions="0.1" orientation="VERTICAL" prefHeight="720.0" prefWidth="1280.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="546.0" prefWidth="1756.0">
<children>
<HBox prefHeight="0.0" prefWidth="0.0">
<children>
<Label minHeight="16.0" minWidth="40.0" text="Money:" />
<Label fx:id="moneyLabel" minHeight="16.0" minWidth="40.0" text="" />
</children>
</HBox>
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TabPane prefHeight="645.0" prefWidth="1280.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Help">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab text="AnotherTab">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button fx:id="hourglass" layoutX="282.0" layoutY="78.0" mnemonicParsing="false" text="Next Month" />
</children></AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children></AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>