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