2013-08-04 46 views
2

請參閱該代碼訪問屬性課外

import javafx.application.Application; 
    import javafx.event.ActionEvent; 
    import javafx.event.EventHandler; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Button; 
    import javafx.scene.control.ButtonBuilder; 
    import javafx.scene.layout.StackPane; 
    import javafx.stage.Stage; 

    public class Test extends Application { 

     private String pageTitle; 

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

     @Override 
     public void start(Stage stage) throws Exception { 
      this.setPageTitle("Hello World"); 

      Button btn = ButtonBuilder.create() 
        .text("Test Button") 
        .onAction(new EventHandler<ActionEvent>() { 
         @Override 
         public void handle(ActionEvent actionEvent) { 
          // this handle should change the property if Test.pageTitle property 
          //this.setPageTitle("Change button"); 
          System.out.println("Testing button action"); 
         } 
        }) 
        .build(); 

      StackPane root = new StackPane(); 
      root.getChildren().add(btn); 

      stage.setTitle(getPageTitle()); 
      stage.setScene(new Scene(root, 400, 400)); 
      stage.show(); 
     } 

     public String getPageTitle() { 
      return pageTitle; 
     } 

     public void setPageTitle(String pageTitle) { 
      this.pageTitle = pageTitle; 
     } 
    } 

我想每次都是按鈕點擊後,它會改變應用程序的標題。 如何使事件處理程序可以訪問Text類的屬性?

回答

3

你應該參考試驗班

Test.this.setPageTitle("Change button"); 
+0

稀釋的當前實例。我懂了。所以我也需要添加這個類的名字。謝謝,它有效。 :d – GusDeCooL