JavaFx:将Value传递给另一个类并更新Label文本

时间:2014-11-21 21:57:42

标签: java javafx

我花了很多时间来解决这个问题尝试不同的解决方案而没有成功。我想从课程ViewPart2向课程ViewPart1发送一个计数值(按钮点击次数)。在ViewPart1中,我想更新标签文字。

public class Test extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {

        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 300, 200);
        stage.setScene(scene);
        stage.show();

        new ViewPart1().createGui(root);
        new ViewPart2().createGui(root);
    }

}

public class ViewPart2 {
    private int count = 0;

    public void createGui(BorderPane root) {
        Button btn = new Button("Click me!");
        root.setLeft(btn);

        btn.setOnAction(event -> {
            count++;
            new ViewPart1().setCount(count);
            // how can I send count value to ViewPart1 and update label text
        });

    }
}

public class ViewPart1 {
    private int count;

    public void createGui(BorderPane root) {

        Label lbl = new Label("-");
        root.setCenter(lbl);
        lbl.setText(count + "Clicks");

    }

    public void setCount(int count) {

        this.count = count;

    }

}

1 个答案:

答案 0 :(得分:0)

这个解决方案可能有些过度,但您可以创建一个模型类,将其作为输入传递给两个视图类。视图可以修改模型并观察模型的变化。

enter image description here

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class Clicker extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        final ClickCounter clickCounter = new ClickCounter();

        final ClickInputView  inputView  = new ClickInputView(clickCounter);
        final ClickOutputView outputView = new ClickOutputView(clickCounter);

        VBox layout = new VBox(10,
                inputView,
                outputView
        );
        layout.setPadding(new Insets(10));
        layout.setAlignment(Pos.CENTER);

        stage.setScene(new Scene(layout));
        stage.show();
    }

    private class ClickCounter {
        private final ReadOnlyIntegerWrapper numClicks =
                new ReadOnlyIntegerWrapper(0);

        public int getNumClicks() {
            return numClicks.get();
        }

        public ReadOnlyIntegerProperty numClicksProperty() {
            return numClicks.getReadOnlyProperty();
        }

        public void click() {
            numClicks.set(getNumClicks() + 1);
        }
    }

    private class ClickInputView extends StackPane {
        private final Button button = new Button("Click Me!");

        public ClickInputView(ClickCounter clickCounter) {
            button.setOnAction(event -> clickCounter.click());

            getChildren().setAll(button);
        }
    }

    private class ClickOutputView extends StackPane {
        private final Label clickCountLabel = new Label();

        public ClickOutputView(ClickCounter clickCounter) {
            clickCountLabel.textProperty().bind(
                    Bindings.format(
                            "Clicked %d times",
                            clickCounter.numClicksProperty()
                    )
            );
            clickCountLabel.setMinWidth(150);
            clickCountLabel.setAlignment(Pos.CENTER);

            getChildren().setAll(clickCountLabel);
        }
    }

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

因为模型在类之间共享,所以它允许视图的方式反映对公共数据结构的更改,而不需要知道彼此的存在。这也意味着模型代码与视图代码隔离,可以单独测试。

对于更完整的解决方案,您可以查看基于依赖项注入的框架,例如afterburner.fx,它还利用FXML将视图定义从Java移植到XML中,并将样式定义移动到CSS中。< / p>