0
我花了很多时间处理这个问题,尝试了不同的解决方案,但没有成功。我想从ViewPart2
类发送计数值(按钮点击次数)到ViewPart1
类。在ViewPart1
中,我想更新标签文本。JavaFx:将值传递给另一个类并更新标签文本
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;
}
}
你的第一个代码片段中缺少某些东西。有3'}'但只有2'{'。请编辑这个问题。我建议在代码中删除一些emptylines。那么帖子会更简洁。 – rtruszk 2014-11-21 22:09:10
你说得对。我发布了错误的课程,我改变了它,对于这个错误感到抱歉。 – 2014-11-22 09:00:35