Question

In: Computer Science

Java Write a JavaFX application that displays a button and a number. Every time the button...

Java

Write a JavaFX application that displays a button and
a number. Every time the button is pushed, change the number
to a random value between 1 and 100.

Thank you and can you show a picture of the result as well

Solutions

Expert Solution

import java.util.Random;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class DisplayRandom extends Application {
  
@Override
public void start(Stage primaryStage) {
Button btn = new Button("Generate Random");
Random rand = new Random();
int num = rand.nextInt((100-1)+1)+1;
Label l1 = new Label(""+num);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
l1.setText("");
int num = rand.nextInt((100-1)+1)+1;
l1.setText(""+num);

}
});
  
Pane root = new Pane();
btn.setLayoutX(100);
btn.setLayoutY(100);
l1.setLayoutX(150);
l1.setLayoutY(50);
root.getChildren().add(btn);
root.getChildren().add(l1);
  
Scene scene = new Scene(root, 300, 250);
  
primaryStage.setTitle("Generate Random");
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
  
}

/* OUTPUT */


Related Solutions

Write a JavaFX application that displays a Label containing the opening sentence or two from your...
Write a JavaFX application that displays a Label containing the opening sentence or two from your favorite book. Save the project as FXBookQuote1a. Add a button to the frame in the FXBookQuote program. When the user clicks the button, display the title of the book that contains the quote in a second label. Save the project as FXBookQuote1b // FXBookQuote1a.java import javafx.application.Application; import static javafx.application.Application.launch; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.Pane; import javafx.scene.text.Font; import javafx.stage.Stage; public class FXBookQuote1a...
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
javaFX is there a way to change the text of a button and then that button...
javaFX is there a way to change the text of a button and then that button repeats to actions of the first button
Write a JavaFX application that presents two buttons and a number (initially 50) to the user....
Write a JavaFX application that presents two buttons and a number (initially 50) to the user. Label the buttons Increment and Decrement. When the increment button is pushed, increment the displayed value. Likewise, decrement the value when the decrement button is pushed. This has to use Java.Fx and not Java.Awt
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
Java 2D Drawing Application. The application will contain the following elements: a) an Undo button to...
Java 2D Drawing Application. The application will contain the following elements: a) an Undo button to undo the last shape drawn. b) a Clear button to clear all shapes from the drawing. c) a combo box for selecting the shape to draw, a line, oval, or rectangle. d) a checkbox which specifies if the shape should be filled or unfilled. e) a checkbox to specify whether to paint using a gradient. f) two JButtons that each show a JColorChooser dialog...
Java In a JavaFX application there is a TextField control(tfRadius) for taking input of a circle’s...
Java In a JavaFX application there is a TextField control(tfRadius) for taking input of a circle’s radius. A respective square whose diagonal is the circle's diameter can then be created. There are three other controls in this application: a button(btCalculate), two textareas(trCircleArea, trSquareArea). When the button is clicked, the circle's area is calculated and displayed on trCircleArea, furthermore, the square's area is also calculated and displayed on trSquareArea. Please write the portions of codes which calculates the circle’s area and...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls: A Label control with the following text displayed: "First Number:" A Label control with the following text displayed: "Second Number:" An empty TextField control beside the First Number label. An empty TextField control beside the Second Number label. Five buttons: Button 1 labeled + Button 2 labeled - Button 3 labeled * Button 4 labeled / Button 5 labeled = An empty Label control...
Create a PowersTable application that displays a table of of powers. ( Java Programing )
Create a PowersTable application that displays a table of of powers. ( Java Programing )
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT