2014-11-21 33 views
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; 

    } 

} 
+0

你的第一個代碼片段中缺少某些東西。有3'}'但只有2'{'。請編輯這個問題。我建議在代碼中刪除一些emptylines。那麼帖子會更簡潔。 – rtruszk 2014-11-21 22:09:10

+0

你說得對。我發佈了錯誤的課程,我改變了它,對於這個錯誤感到抱歉。 – 2014-11-22 09:00:35

回答

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。