1. Develop a java program that have 11 text fields one submit button.
When you
press the button first 10 text field’s average has to be displayed in the 11th
text field.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class AverageCalculator extends Application {
private TextField[] textFields = new TextField[11];
private Button submitButton;
@Override
public void start(Stage primaryStage) {
[Link]("Average Calculator");
VBox root = new VBox(10); // Spacing of 10 between elements
[Link](new Insets(10));
// Create 11 TextFields
for (int i = 0; i < 11; i++) {
textFields[i] = new TextField();
textFields[i].setPromptText("Enter number " + (i + 1));
[Link]().add(textFields[i]);
}
// Create Submit Button
submitButton = new Button("Calculate Average");
[Link](e -> calculateAndDisplayAverage());
[Link]().add(submitButton);
Scene scene = new Scene(root, 300, 400);
[Link](scene);
[Link]();
}
private void calculateAndDisplayAverage() {
double sum = 0;
int count = 0;
for (int i = 0; i < 10; i++) {
try {
double value = [Link](textFields[i].getText());
sum += value;
count++;
} catch (NumberFormatException ex) {
// Handle non-numeric input (e.g., display an error or skip the field)
[Link]("Invalid number in field " + (i + 1) + ": " +
textFields[i].getText());
}
}
if (count > 0) {
double average = sum / count;
textFields[10].setText([Link]("%.2f", average)); // Display average formatted
to 2 decimal places
} else {
textFields[10].setText("N/A"); // No valid numbers entered
}
}
public static void main(String[] args) {
launch(args);
}
}
2. Develop a java code that keeps the count of right clicks of mouse.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class RightClickCounter extends Application {
private int rightClickCount = 0;
private Label countLabel;
@Override
public void start(Stage primaryStage) {
countLabel = new Label("Right Clicks: 0");
StackPane root = new StackPane();
[Link]().add(countLabel);
Scene scene = new Scene(root, 300, 200);
// Add an event handler for mouse clicks on the scene
[Link](this::handleMouseClick);
[Link]("Right Click Counter");
[Link](scene);
[Link]();
}
private void handleMouseClick(MouseEvent event) {
// Check if the clicked button is the secondary (right) button
if ([Link]() == [Link]) {
rightClickCount++;
[Link]("Right Clicks: " + rightClickCount);
}
}
public static void main(String[] args) {
launch(args);
}
}
3. Develop java program that changes the color of a filled circle when you make a right
click.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class RightClickColorChange extends Application {
private Circle circle;
private Random random = new Random();
@Override
public void start(Stage primaryStage) {
// Create a Pane as the root layout
Pane root = new Pane();
// Create a Circle
circle = new Circle(150, 150, 50); // CenterX, CenterY, Radius
[Link]([Link]); // Initial color
// Add the circle to the pane
[Link]().add(circle);
// Set up the event handler for mouse clicks on the circle
[Link](event -> {
if ([Link]() == [Link]) { // Check for right-click
changeCircleColor();
}
});
// Create a Scene and set it on the Stage
Scene scene = new Scene(root, 300, 300); // Width, Height
[Link]("Right-Click Color Change");
[Link](scene);
[Link]();
}
/**
* Changes the fill color of the circle to a random color.
*/
private void changeCircleColor() {
double r = [Link]();
double g = [Link]();
double b = [Link]();
[Link](new Color(r, g, b, 1.0)); // Create a new random color
}
public static void main(String[] args) {
launch(args);
}
}
[Link] a program to develop HBox and VBox
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HBoxVBoxExample extends Application {
@Override
public void start(Stage primaryStage) {
// Create a VBox for the first set of controls
VBox leftVBox = new VBox(10); // 10 pixels spacing between children
[Link]([Link]);
[Link](new Insets(15)); // Padding around the VBox
Label label1 = new Label("Left Section");
Button button1 = new Button("Click Me 1");
[Link]().addAll(label1, button1);
// Create a VBox for the second set of controls
VBox rightVBox = new VBox(10); // 10 pixels spacing between children
[Link]([Link]);
[Link](new Insets(15)); // Padding around the VBox
Label label2 = new Label("Right Section");
Button button2 = new Button("Click Me 2");
[Link]().addAll(label2, button2);
// Create an HBox to hold the two VBoxes
HBox mainHBox = new HBox(20); // 20 pixels spacing between children (VBoxes)
[Link]([Link]);
[Link](new Insets(20)); // Padding around the HBox
[Link]().addAll(leftVBox, rightVBox);
// Create a Scene and set it on the Stage
Scene scene = new Scene(mainHBox, 400, 200); // Width, Height
[Link]("HBox and VBox Example");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
5. Border Pane
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class BorderPaneDemo extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
[Link](new Button("Top"));
[Link](new Button("Bottom"));
[Link](new Button("Left"));
[Link](new Button("Right"));
[Link](new Button("Center"));
Scene scene = new Scene(root, 300, 200);
[Link]("BorderPane Demo");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
HBox
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HBoxDemo extends Application {
@Override
public void start(Stage primaryStage) {
HBox root = new HBox(10); // Spacing of 10 pixels between children
[Link]().addAll(
new Button("Button 1"),
new Button("Button 2"),
new Button("Button 3")
);
Scene scene = new Scene(root, 300, 100);
[Link]("HBox Demo");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
VBOX
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class VBoxDemo extends Application {
@Override
public void start(Stage primaryStage) {
VBox root = new VBox(10); // Spacing of 10 pixels between children
[Link]().addAll(
new Button("Button A"),
new Button("Button B"),
new Button("Button C")
);
Scene scene = new Scene(root, 200, 200);
[Link]("VBox Demo");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
GridPane
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class GridPaneDemo extends Application {
@Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
[Link](10); // Horizontal gap
[Link](10); // Vertical gap
[Link](new Button("0,0"), 0, 0);
[Link](new Button("1,0"), 1, 0);
[Link](new Button("0,1"), 0, 1);
[Link](new Button("1,1"), 1, 1);
Scene scene = new Scene(root, 250, 150);
[Link]("GridPane Demo");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
Stack Pane
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class StackPaneDemo extends Application {
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
[Link]().addAll(
new Button("Bottom Layer"),
new Button("Middle Layer"),
new Button("Top Layer")
);
Scene scene = new Scene(root, 200, 150);
[Link]("StackPane Demo");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
6. Write a program to create a frame with the following menus, such that the corresponding
geometric object is created when a menu is clicked.
[Link]. [Link]. [Link]. [Link] for the rectangle
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ShapeMenuApp extends Application {
private Pane drawingPane; // Pane to draw shapes on
@Override
public void start(Stage primaryStage) {
// Create the drawing pane
drawingPane = new Pane();
[Link]("-fx-background-color: lightgray;");
// Create the menu bar
MenuBar menuBar = new MenuBar();
// Create the "Shapes" menu
Menu shapesMenu = new Menu("Shapes");
// Create menu items
MenuItem circleItem = new MenuItem("Circle");
MenuItem rectangleItem = new MenuItem("Rectangle");
MenuItem lineItem = new MenuItem("Line");
MenuItem diagonalItem = new MenuItem("Diagonal (for Rectangle)");
// Add action handlers to menu items
[Link](e -> drawCircle());
[Link](e -> drawRectangle());
[Link](e -> drawLine());
[Link](e -> drawDiagonal());
// Add menu items to the "Shapes" menu
[Link]().addAll(circleItem, rectangleItem, lineItem, diagonalItem);
// Add the "Shapes" menu to the menu bar
[Link]().add(shapesMenu);
// Create the main layout
BorderPane root = new BorderPane();
[Link](menuBar);
[Link](drawingPane);
// Create the scene and set it to the stage
Scene scene = new Scene(root, 600, 400);
[Link]("JavaFX Shape Menu");
[Link](scene);
[Link]();
}
// Method to draw a circle
private void drawCircle() {
[Link]().clear(); // Clear previous shapes
Circle circle = new Circle(100, 100, 50); // x, y, radius
[Link]([Link]);
[Link]().add(circle);
}
// Method to draw a rectangle
private void drawRectangle() {
[Link]().clear(); // Clear previous shapes
Rectangle rectangle = new Rectangle(150, 50, 100, 70); // x, y, width, height
[Link]([Link]);
[Link]().add(rectangle);
}
// Method to draw a line
private void drawLine() {
[Link]().clear(); // Clear previous shapes
Line line = new Line(50, 200, 250, 150); // startX, startY, endX, endY
[Link]([Link]);
[Link](3);
[Link]().add(line);
}
// Method to draw a diagonal for a rectangle
private void drawDiagonal() {
[Link]().clear(); // Clear previous shapes
Rectangle rectangle = new Rectangle(150, 50, 100, 70); // Same rectangle as in
drawRectangle()
[Link]([Link]); // Make it slightly different for visibility
[Link]().add(rectangle);
Line diagonal = new Line([Link](), [Link](),
[Link]() + [Link](), [Link]() +
[Link]());
[Link]([Link]);
[Link](2);
[Link]().add(diagonal);
}
public static void main(String[] args) {
launch(args);
}
}
7. Simple menu program
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SimpleMenuApp extends Application {
@Override
public void start(Stage primaryStage) {
// Create a MenuBar
MenuBar menuBar = new MenuBar();
// Create a "File" Menu
Menu fileMenu = new Menu("File");
// Create MenuItems for the "File" Menu
MenuItem openMenuItem = new MenuItem("Open");
MenuItem saveMenuItem = new MenuItem("Save");
MenuItem exitMenuItem = new MenuItem("Exit");
// Add action listeners to MenuItems (optional, but common)
[Link](event -> [Link]("Open clicked!"));
[Link](event -> [Link]("Save clicked!"));
[Link](event -> [Link]()); // Close the application
// Add MenuItems to the "File" Menu
[Link]().addAll(openMenuItem, saveMenuItem, exitMenuItem);
// Create an "Edit" Menu
Menu editMenu = new Menu("Edit");
// Create MenuItems for the "Edit" Menu
MenuItem cutMenuItem = new MenuItem("Cut");
MenuItem copyMenuItem = new MenuItem("Copy");
MenuItem pasteMenuItem = new MenuItem("Paste");
// Add action listeners to MenuItems
[Link](event -> [Link]("Cut clicked!"));
[Link](event -> [Link]("Copy clicked!"));
[Link](event -> [Link]("Paste clicked!"));
// Add MenuItems to the "Edit" Menu
[Link]().addAll(cutMenuItem, copyMenuItem, pasteMenuItem);
// Add Menus to the MenuBar
[Link]().addAll(fileMenu, editMenu);
// Create a VBox to hold the MenuBar
VBox root = new VBox(menuBar);
// Create a Scene
Scene scene = new Scene(root, 300, 200);
// Set the Scene to the Stage
[Link]("Simple Menu App");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}