Question

In Java, specifically using javafx and NOT SWING , thanks! To write a program that illustrates...

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).

Homework Answers

Answer #1

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:-

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
Java Servlet Program that represents an authentication platform where user will authenticate their username, passwordand usercode....
Java Servlet Program that represents an authentication platform where user will authenticate their username, passwordand usercode. After successful authentication, a message will be printed on the screen. All the wrong use of credentials will result in error that you will handle and there will also be a message displayed on the screen. Following are minimum guidelines: 1. The front end should have a background color and a heading like “User authentication System”. 2. It should have a text field with...
5) Create a Java program using Scanner: Write a program where you will enter the grade...
5) Create a Java program using Scanner: Write a program where you will enter the grade score for 5 classes, then it will display total points and average and it will display the grade based on percentage. Steps: 1) Declare variable integer for English, Chemistry, Java Programming, Physics and Math 2) Declare variable total and percentage 3) Create Scanner object and prompt the user to enter grades for each class and input grades after each class 4) Calculate total of...
Which of the following would NOT be considered an "event"? Pressing the up arrow key to...
Which of the following would NOT be considered an "event"? Pressing the up arrow key to move text on a GUI Compiling the code for a GUI Dragging the mouse across the GUI Clicking a button on a GUI QUESTION 2 Which which Event class does MouseEvent extend? InputEvent ActionEvent KeyEvent WindowEvent QUESTION 3 There are three parts of an event. Which of the following is NOT part of an event? The source - the source is where the event...
Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to...
Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to get 4 integers from the user and store them in variables. The program should calculate the average of the 6 numbers as a floating point. Output all of the original input and the calculated average in the Command window. The code is expected to be commented and user-friendly.
Write a Java program called Decision that includes a while loop to prompt the user to...
Write a Java program called Decision that includes a while loop to prompt the user to enter 5 marks using the JOptionPane statement and a System. Out statement to output a message inside the loop highlighting a pass mark >= 50 and <= 100. Any other mark will output a messaging stating it’s a fail. Example of the expected output is as follows:
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
JAVA Write a program that will search a text file of strings representing numbers of type...
JAVA Write a program that will search a text file of strings representing numbers of type int and will write the largest and the smallest numbers to the screen. Include appropriate error handling in case a line contains more than one int or it contains a String. Wrap all the work you are doing with the file in a try/catch/finally block with appropriate "catch" sections; the finally block will be where you close your file object (if desired, look this...