gpt4 book ai didi

java - 访问类外的属性

转载 作者:行者123 更新时间:2023-12-04 04:46:56 25 4
gpt4 key购买 nike

请看这个代码

    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的属性类(class)?

最佳答案

您应该引用 Test 类的当前实例

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

关于java - 访问类外的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18046281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com