Could you review and advise me on how to correct code to create an 1. btOne as a
ID: 3707979 • Letter: C
Question
Could you review and advise me on how to correct code to create an
1. btOne as a Inner Class Listener,
2. btTwo as a Anonymous Listener
3. btThree as a Lambda expression.
Thank you
package Threebuttons;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Threebuttons extends Application {
@Override //*Override the start method in the Application class
public void start(Stage primaryStage) {
// Hold three buttons in an HBox
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btOne = new Button("Button 1");
Button btTwo = new Button("Button 2");
Button btThree = new Button("Button 3");
hBox.getChildren().addAll(btOne, btTwo, btThree);
//*Create and Register the handler
btOne.setOnAction(new OneHandler());
BorderPane borderPane = new BorderPane();
borderPane.setCenter(hBox);
BorderPane.setAlignment(hBox, Pos. CENTER);
//*Create a scene and place it in the stage
Scene scene = new Scene(hBox, 300, 500);
primaryStage.setTitle("Three Buttons"); //*Set title
primaryStage.setScene(scene); //*Place the scene in the stage
primaryStage.show(); //*Display the stage
}
//*Button One - Inner Class Listener
class OneListener implements EventHandler {
@Override
public void handle(ActionEvent e) {
System.out.println("Hello World");
}
//*Button Two - Anonymous Listener
btTwo.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent e) {
System.out.println("Hello World");
}
});
//*Button Three - Lambda Expression p598 lines23-25
btThree.setOnAction((ActionEvent e) -> {
System.out.println("Hello World");
});
}
//*keep main method launch application
public static void main(String[] args) {
Application.launch(args);
}
}