[英]JavaFX can't find symbol
package cs1302.gui;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
/**
* Represents a small example app written using JavaFX.
*/
public class ExampleApp extends Application {
/**
* {@inheritDoc}
*/
@Override
public void start(Stage stage) {
Text hello = new Text("Hello World!!!");
Button button = new Button("Click me!");
button.setOnAction(buttonHandler);
HBox root = new HBox();
root.getChildren().add(button);
root.getChildren().add(hello);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.sizeToScene();
stage.setTitle("ExampleApp!");
stage.show();
} // start
public final void setOnAction(EventHandler<ActionEvent> value) {
EventHandler<ActionEvent> buttonHandler = event -> System.out.println("you clicked me!");
}
} // ExampleApp
The problem is that when I compile, the error is cannot find button.setOnAction(buttonHandler)
;问题是当我编译时,错误是 cannot find
button.setOnAction(buttonHandler)
;
I want to know how to fix it.我想知道如何解决它。
You have not defined the buttonHandler
in the method that is calling the setOnAction
method.您尚未在调用
setOnAction
方法的方法中定义buttonHandler
。 My suggestion is to move the declaration into the main method:我的建议是将声明移到 main 方法中:
package cs1302.gui;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
/**
* Represents a small example app written using JavaFX.
*/
public class ExampleApp extends Application {
/**
* {@inheritDoc}
*/
@Override
public void start(Stage stage) {
Text hello = new Text("Hello World!!!");
Button button = new Button("Click me!");
EventHandler<ActionEvent> buttonHandler = event -> System.out.println("you clicked me!");
button.setOnAction(buttonHandler);
HBox root = new HBox();
root.getChildren().add(button);
root.getChildren().add(hello);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.sizeToScene();
stage.setTitle("ExampleApp!");
stage.show();
} // start
} // ExampleApp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.