Skip to content

Instantly share code, notes, and snippets.

@SamuelSchwent
Created March 3, 2016 15:56
Show Gist options
  • Select an option

  • Save SamuelSchwent/5a024bb8a9629fa89ff4 to your computer and use it in GitHub Desktop.

Select an option

Save SamuelSchwent/5a024bb8a9629fa89ff4 to your computer and use it in GitHub Desktop.
Java - Scene Builder Application 2
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="150.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Challenge4.TipTaxTotalController">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Meal Cost: " />
<TextField fx:id="mealCost" layoutX="73.0" layoutY="10.0" prefHeight="25.0" prefWidth="118.0" />
<Button fx:id="tttButton" layoutX="47.0" layoutY="43.0" mnemonicParsing="false" onAction="#getTipTaxTotal" text="Tip Tax and Total" />
<Label alignment="CENTER_RIGHT" layoutX="22.0" layoutY="80.0" prefWidth="50.0" text="Tip:" textAlignment="CENTER" />
<Label alignment="CENTER_RIGHT" layoutX="22.0" layoutY="97.0" prefWidth="50.0" text="Tax:" textAlignment="CENTER" />
<Label alignment="CENTER_RIGHT" layoutX="22.0" layoutY="114.0" prefWidth="50.0" text="Total:" textAlignment="CENTER" />
<Label fx:id="tipLabel" layoutX="82.0" layoutY="80.0" prefWidth="100.0" textAlignment="CENTER" />
<Label fx:id="taxLabel" layoutX="82.0" layoutY="97.0" prefWidth="100.0" textAlignment="CENTER" />
<Label fx:id="totalLabel" layoutX="82.0" layoutY="114.0" prefWidth="100.0" textAlignment="CENTER" />
</children>
</AnchorPane>
package Challenge4;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TipTaxTotal extends Application{
@Override
public void start(Stage stage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("TipTaxTotal.fxml"));
Scene scene = new Scene(parent);
stage.setTitle("Tip Tax Total");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package Challenge4;
import java.text.NumberFormat;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class TipTaxTotalController {
@FXML
private Button tttButton;
@FXML
private TextField mealCost;
@FXML
private Label tipLabel;
@FXML
private Label taxLabel;
@FXML
private Label totalLabel;
private final NumberFormat nf = NumberFormat.getNumberInstance();
{
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
}
public void getTipTaxTotal()
{
double cost = Double.parseDouble(mealCost.getText());
double tip = cost * .18;
double tax = cost * .07;
double total = (cost + tip + tax);
tipLabel.setText("$" + nf.format(tip));
taxLabel.setText("$" + nf.format(tax));
totalLabel.setText("$" + nf.format(total));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment