JavaFX如何每次更新标签文本值?

时间:2018-06-21 08:10:25

标签: javafx-8

我正在使用IntellijIDEA。我有ui.fxml,Controller.java和GUI.java。这是代码: ui.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-1.0" minWidth="-1.0" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="sample.Controller">
  <children>
    <Button fx:id="exitBtn" cancelButton="true" layoutX="530.0" layoutY="366.0" mnemonicParsing="false" text="Exit" />
    <Button fx:id="stopBtn" layoutX="460.0" layoutY="367.0" mnemonicParsing="false" text="Stop" />
    <Button fx:id="startBtn" layoutX="387.0" layoutY="368.0" mnemonicParsing="false" text="Start" />
    <Label fx:id="threadsLabel" layoutX="19.0" layoutY="23.0" prefWidth="45.0" text="Threads" />
    <Label fx:id="threadsValue" layoutX="160.0" layoutY="26.0" prefWidth="45.0" text="0" />
    <Label fx:id="smLabel" layoutX="19.0" layoutY="42.0" prefWidth="108.0" text="submit_sm sended" />
    <Label fx:id="smValue" layoutX="160.0" layoutY="42.0" prefWidth="45.0" text="0" />
    <Label fx:id="deliverLabel" layoutX="19.0" layoutY="61.0" prefWidth="115.0" text="deliver_sm received" />
    <Label fx:id="deliverValue" layoutX="160.0" layoutY="61.0" prefWidth="45.0" text="0" />
    <Label fx:id="tpsLabel" layoutX="19.0" layoutY="77.0" prefWidth="78.0" text="Current TPS" />
    <Label fx:id="tpsValue" layoutX="160.0" layoutY="77.0" prefWidth="45.0" text="0" />
    <Slider fx:id="speed" layoutX="213.0" layoutY="28.0" max="1000.0" min="10.0" prefWidth="317.0" showTickLabels="true" showTickMarks="false" />
  </children>
</AnchorPane>

GUI.java

    public class GUI extends Application {
        public Logger log = Main.getLogger("log");
        @Override
        public void start(Stage primaryStage) throws Exception{
            FXMLLoader loader = new FXMLLoader();
            Parent root = loader.load(getClass().getResource("ui.fxml"));
            primaryStage.setTitle("ESME Emulator");
            Scene mainScene = new Scene(root, 600, 400);
            primaryStage.setScene(mainScene);
            primaryStage.show();

        }


        public void show(String[] args) {
            launch(args);
        }

    }

Controller.java

    public class Controller implements Initializable {
    public Logger log=Main.getLogger("log");

    @FXML
    public ResourceBundle resources;

    @FXML
    public URL location;

    @FXML
    public Label deliverLabel;

    @FXML
    public Label deliverValue;

    @FXML
    public Button exitBtn;

    @FXML
    public Label smLabel;

    @FXML
    public Label smValue;

    @FXML
    public Slider speed;

    @FXML
    public Button startBtn;

    @FXML
    public Button stopBtn;

    @FXML
    public Label threadsLabel;

    @FXML
    public Label threadsValue;

    @FXML
    public Label tpsLabel;

    @FXML
    public Label tpsValue;

    public Sample sample;
    public Controller()
    {
        this.sample=new Sample(this);
    }
    public void initialize(URL location, ResourceBundle resources) {
        stopBtn.setDisable(true);
        startBtn.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                try {
                    Main.startEmulation();
                    startBtn.setDisable(true);
                    stopBtn.setDisable(false);
                }catch(Exception e)
                {

                    log.error("Exception while \"Start\" button click event: "+e.getMessage());
                }
            }
        });
        stopBtn.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                try {
                    Main.stopEmulation();
                    startBtn.setDisable(false);
                    stopBtn.setDisable(true);
                }catch(Exception e)
                {

                    log.error("Exception while \"Stop\" button click event: "+e.getMessage());
                }
            }
        });
        exitBtn.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                try {
                    Main.stopEmulation();
                    System.exit(0);
                }catch(Exception e)
                {

                    log.error("Exception while \"Exit\" button click event: "+e.getMessage());
                }
            }
        });
    }
}

我还有另一个java类,它提供了一段时间的无穷循环,并且我需要每次迭代都通过thredsValue.setText(“ some text”)更改threadsValue,我该怎么做? p.s.请问我的英语...

我这样做了,仍然没有结果,怎么了?

    UpdateTask updTask = new UpdateTask();
    Label.textProperty().bind(updTask.valueProperty());
    Thread changes = new Thread(updTask);
    changes.setDaemon(true);
    changes.setName("UPDATE");
    changes.start();

class UpdateTask extends Task<String> {

    @Override
    protected String call() throws Exception {
        while(isRunning()) {
            updateValue(Integer.toString(Main.getThreadsCount()));

        }
        return null;
    }

}

0 个答案:

没有答案