java - 如何访问动作事件处理程序(javafx 按钮)中的外部变量?
问题描述
我有点 javafx 菜鸟。我想让这个按钮更新名为“viewGraph”的“GraphView”对象,但我不知道如何正确执行此操作,因为我无法访问 setOnAction 事件中的外部变量。我也想在事件类之外创建 startVertice 和 endVertice,但我不知道该怎么做。
我认为答案是以某种方式在使用参数中传递变量,但我不知道该怎么做。
我尝试查找内容并尝试随机语法内容,但我仍然不知道该怎么做。不过看起来很简单。
WeightedGraph<Campus> graph = new WeightedGraph<>(vertices, edges);
Label startCampus = new Label("Starting Campus: ");
Label endCampus = new Label(" Ending Campus: ");
TextField startText = new TextField();
TextField endText = new TextField();
Button displayShortestPathBtn = new Button("Display Shortest Path");
HBox input = new HBox();
input.getChildren().addAll(startCampus, startText, endCampus, endText, displayShortestPathBtn);
input.setPadding(new Insets(25, 0, 0, 0));
VBox vbox = new VBox();
GraphView viewGraph = new GraphView(graph);
vbox.getChildren().addAll(viewGraph, input);
vbox.setPadding(new Insets(50, 50, 25, 50));
displayShortestPathBtn.setOnAction((event) -> {
int startVertice = Integer.parseInt(startText.getText());
int endVertice = Integer.parseInt(endText.getText());
WeightedGraph<Campus>.ShortestPathTree shortestPathGraph = graph.getShortestPath(startVertice);
ArrayList<Integer> path = (ArrayList)shortestPathGraph.getPath(endVertice);
/*
What I want to do:
viewGraph = new GraphView(graph, path);
*/
});
primaryStage.setTitle("Final Exam");
primaryStage.setScene(new Scene(vbox));
primaryStage.show();
我只想能够访问外部变量,但如果可能的话,或者如果我首先正确地解决了问题,我想知道。
解决方案
推荐阅读
- jsonschema - 寻找依赖 JSON 模式中的最小值/最大值的可能性
- r - 按日期时间列子集数据框?
- python - 无法以 SavedModel 格式保存 Tensorflow
- apache-spark - 为在同一台机器上运行的多个执行器导出 spark executor jmx 指标
- ocr - Mirador 单词突出显示
- python - 如何在 Clickhouse 的远程服务器上保存从 Python 控制台接收到的数据?
- python - 将excel列转换为变量
- apache-spark - SparkSQL 与 ScalaPB:在输出原型格式中使用 MapType 会在调用 toByteString 时产生 scala.MatchError
- ios - Xcode 12 Beta - 找不到模拟器运行时
- laravel - AWS ElasticBeanstalk 上的 Laravel:在使用 .ebextensions 部署失败时运行工匠迁移命令