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);
}
}
Explanation / Answer
Explanation: Below is the Java code for above problem with proper description provided within comments itself. If you need any other help for this Comment me below. Thanks Java code : 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.HBox; import javafx.stage.Stage; public class Threebuttons extends Application { public static void main(String[] args) { launch(args); } HBox hBox; @Override // *Override the start method in the Application class public void start(Stage primaryStage) { // Hold three buttons in an 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 // Inner class Listener btOne.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { System.out.println("Hello World! Btn 1"); } }); // - Anonymous Class Listener btTwo.setOnAction(new OneListener()); // Lambda expression btThree.setOnAction((event) -> { System.out.println("Hello World! Btn 3"); }); // 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 two - Anonymous Class Listener class OneListener implements EventHandler { @Override public void handle(ActionEvent e) { System.out.println("Hello World! Btn 2"); } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.