Question: To write a program that illustrates how to code a box with buttons on the screen and to create an event class to handle the

To write a program that illustrates how to code a box with buttons on the screen and to create an event class to handle the click of a button

Write a program using a layout manager. You must write the program to meet the following criteria:

Create a window with a layout.

Add 6 buttons to the window, each with a number to identify it such as Button1, Button2, Button3, Button4, Button5, Button6.

When a button is clicked, it will create an event object.

Create a listener object that will respond to the event object by displaying a message to the screen stating which button was clicked.

Hint: Make a separate class to handle the event (clicking the button).

I keep getting an error while trying to run this program

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.FlowPane; public class Main extends Application { @Override public void star(Stage primaryStage) { FlowPane root = new FlowPane(); root.setHgap(10); root.setVgap(10); root.setPadding(new Insets(25,25,25,25)); Button btn1 = new Button ("button 1"); Button btn2 = new Button ("button 2"); Button btn3 = new Button ("button 3"); Button btn4 = new Button ("button 4"); Button btn5 = new Button ("button 5"); Button btn6 = new Button ("button 6"); Handler h1 = new Handler( "button1 was clicked"); Handler h2 = new Handler( "button2 was clicked"); Handler h3 = new Handler( "button3 was clicked"); Handler h4 = new Handler( "button4 was clicked"); Handler h5 = new Handler( "button5 was clicked"); Handler h6 = new Handler( "button6 was clicked"); bt1.setOnAction(h1); bt2.setOnAction(h2); bt3.setOnAction(h3); bt4.setOnAction(h4); bt5.setOnAction(h5); bt6.setOnAction(h6); root.getChildren().addAll(btn1,btn2,btn3,btn4,btn5,btn6); Scene scene = new Scene(grid, 500, 600); primaryStage.setScene(scene); primaryStage.show(); } public static void main (String[] args) { launch(args); } }

import javafx.event.ActionEvent; import javafx.event.EventHandler; public Handler implements EventHandler { String message; public Handler(String message){ this.message = message; } @Override public void handle(ActionEvent a) { System.out.println(this.message); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!