In Java, specifically using javafx and NOT SWING , thanks!
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).
Code:-
import javafx.scene.control.Label;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.layout.*; //importing
public class button extends Application { //button class which
extends the Application class
public void start(Stage s) //start function
{
Button
button1,button2,button3,button4,button5,button6; //declaring
buttons
button1 = new Button("Button1"); //creating
button1
button2 = new Button("Button2"); //creating
button2
button3 = new Button("Button3"); //creating
button3
button4 = new Button("Button4"); //creating
button4
button5 = new Button("Button5"); //creating
button5
button6 = new Button("Button6"); //creating
button6
TilePane lay = new TilePane(); //layout TitlePane
Label l = new Label("Nothing Clicked"); //label to sho which button
is clicked when no button is clicked it displays (Nothing
Clicked)
EventHandler<ActionEvent> event1 = new
EventHandler<ActionEvent>() { //creating object for
eventhandler
public void handle(ActionEvent e) //listener function when button1
is clicked
{
l.setText("Button1 clicked");
}
};
EventHandler<ActionEvent> event2 = new
EventHandler<ActionEvent>() { //creating object for
eventhandler
public void handle(ActionEvent e) //listener function when button2
is clicked
{
l.setText("Button2 clicked");
}
};
EventHandler<ActionEvent> event3 = new
EventHandler<ActionEvent>() { //creating object for
eventhandler
public void handle(ActionEvent e) //listener function when button3
is clicked
{
l.setText("Button3 clicked");
}
};
EventHandler<ActionEvent> event4 = new
EventHandler<ActionEvent>() { //creating object for
eventhandler
public void handle(ActionEvent e) //listener function when button4
is clicked
{
l.setText("Button4 clicked");
}
};
EventHandler<ActionEvent> event5 = new
EventHandler<ActionEvent>() { //creating object for
eventhandler
public void handle(ActionEvent e) //listener function when button5
is clicked
{
l.setText("Button5 clicked");
}
};
EventHandler<ActionEvent> event6 = new
EventHandler<ActionEvent>() { //creating object for
eventhandler
public void handle(ActionEvent e) //listener function when button6
is clicked
{
l.setText("Button6 clicked");
}
};
button1.setOnAction(event1); //onclick listener for
button1
button2.setOnAction(event2); //onclick listener for button2
button3.setOnAction(event3); //onclick listener for button3
button4.setOnAction(event4); //onclick listener for button4
button5.setOnAction(event5); //onclick listener for button5
button6.setOnAction(event6); //onclick listener for button6
lay.getChildren().addAll(button1,button2,button3,button4,button5,button6,l);
//adding buttons to the TitlePane layout
Scene sc = new Scene(lay, 200, 200); //adding layout to scene
s.setScene(sc); //setting scene
s.show(); //showing scene
}
public static void main(String args[]) //main function
{
launch(args); //launching which calls start function
automatically
}
}
Output:-
Get Answers For Free
Most questions answered within 1 hours.