简体   繁体   English

课外访问属性

[英]Access property outside class

please see this code 请看这段代码

    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;
        }
    }

I want every time is the button clicked, it will change the title of application. 我想每次都单击该按钮,它将更改应用程序的标题。 How to make the event handler can access the property of Text class? 如何使事件处理程序可以访问Text类的属性?

您应该参考Test类的当前实例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM