Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save SamuelSchwent/eff6be9ccf5e70d7769f to your computer and use it in GitHub Desktop.
Java - Scene Builder Application 3
<?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 minHeight="200.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Challenge5.DistanceTraveledController">
<children>
<Label layoutX="69.0" layoutY="23.0" text="Speed (mph):" />
<TextField fx:id="speed" layoutX="150.0" layoutY="19.0" prefHeight="25.0" prefWidth="80.0" />
<Button fx:id="fiveButton" layoutX="100.0" layoutY="54.0" mnemonicParsing="false" onAction="#driveFive" prefWidth="100.0" text="Drive 5 Hours" />
<Button fx:id="eightButton" layoutX="100.0" layoutY="88.0" mnemonicParsing="false" onAction="#driveEight" prefWidth="100.0" text="Drive 8 Hours" />
<Button fx:id="twelveButton" layoutX="100.0" layoutY="122.0" mnemonicParsing="false" onAction="#driveTwelve" prefWidth="100.0" text="Drive 12 Hours" />
<Label fx:id="outputLabel" alignment="CENTER" layoutX="50.0" layoutY="160.0" prefWidth="200.0" textAlignment="CENTER" />
</children>
</AnchorPane>
package Challenge5;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class DistanceTraveled extends Application{
@Override
public void start(Stage stage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("DistanceTraveled.fxml"));
Scene scene = new Scene(parent);
stage.setTitle("Distance Traveled");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package Challenge5;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class DistanceTraveledController {
@FXML
private Label outputLabel;
@FXML
private TextField speed;
@FXML
private Button fiveButton;
@FXML
private Button eightButton;
@FXML
private Button twelveButton;
public void initialize(){}
public void driveFive()
{
int mph = Integer.parseInt(speed.getText());
outputLabel.setText("Distance: " + (mph * 5) + " Miles");
}
public void driveEight()
{
int mph = Integer.parseInt(speed.getText());
outputLabel.setText("Distance: " + (mph * 8) + " Miles");
}
public void driveTwelve()
{
int mph = Integer.parseInt(speed.getText());
outputLabel.setText("Distance: " + (mph * 12) + " Miles");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment