Question: Take the three card program that is created and add 2 more cards and a button. When the button is clicked five cards will display.

Take the three card program that is created  and add 2 more cards and a button. When the button is clicked five cards will display.

Hint:

 

Take the program  and modify it:

 
  • Add 2 cards (5 total) to the pane (I recommend HBox)
  • Add a button call it anything you like (e.g. btShuffle)
  • Register an Anonymous Handler using Lambda Expression in the buttons setOnAction method
  • In the Lamda Expression
    • shuffle the list
    • clear the pane (e.g. cardPane.getChildren.clear();)
    • add the five card images (e.g. add(new ImageView("image/card/" + list.get(0) + ".png");

here is the code that i have


import javafx.application.Application;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.stage.Stage;


public class SimpleCalculator extends Application {


private TextField tf1 = new TextField();

private TextField tf2 = new TextField();

private TextField tfResult = new TextField();


private Button btAdd = new Button("Add");

private Button btSubtract = new Button("Subtract");

private Button btMultiply = new Button("Multiply");

private Button btDivide = new Button("Divide");

private Button btModulus = new Button("Modulus");



@Override

public void start(Stage primaryStage) throws Exception {

HBox hBox1 = new HBox(5);

HBox hBox2 = new HBox(5);

BorderPane pane = new BorderPane();


hBox1.getChildren().addAll(new Label( "Num 1: "), tf1, new Label("Num 2: "), tf2, new Label("Result: "), tfResult);

hBox1.setPadding(new Insets (15, 10, 15, 10));


hBox2.getChildren().addAll(btAdd, btSubtract, btMultiply, btDivide, btModulus);


pane.setTop(hBox1);

pane.setBottom(hBox2);


//Set UI properties

hBox1.setAlignment(Pos.CENTER);

hBox2.setAlignment(Pos.CENTER);


tf1.setPrefColumnCount(4);

tf2.setPrefColumnCount(4);

tfResult.setPrefColumnCount(4);

tfResult.setEditable(false);


//Process events

btAdd.setOnAction(e -> calculate('+'));

btSubtract.setOnAction(e -> calculate('-'));

btMultiply.setOnAction(e -> calculate('*'));

btDivide.setOnAction(e -> calculate('/'));

btModulus.setOnAction(e -> calculate('%'));




Scene scene = new Scene(pane, 400, 125);

primaryStage.setTitle("Simple Calculator");

primaryStage.setScene(scene);

primaryStage.show();


}


private void calculate(char c) {

double result = 0;

switch(c) {

case '+':

result = Double.parseDouble(tf1.getText()) + Double.parseDouble(tf2.getText());

break;

case '-':

result = Double.parseDouble(tf1.getText()) - Double.parseDouble(tf2.getText());

break;

case '*':

result = Double.parseDouble(tf1.getText()) * Double.parseDouble(tf2.getText());

break;

case '/':

result = Double.parseDouble(tf1.getText()) / Double.parseDouble(tf2.getText());

break;

case '%':

result = Double.parseDouble(tf1.getText()) % Double.parseDouble(tf2.getText());

}

tfResult.setText(String.valueOf(result));

}


public static void main(String[] args) {

launch(args);


}


}


Step by Step Solution

3.25 Rating (146 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

import javafxapplicationApplication import javafxcollectionsFXCollections import javafxcollectionsOb... View full answer

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 Programming Questions!