import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class DemoFX extends Application { // constants: placeholders (names) that hold a value // once initialized, it cannot change // naming convention is all CAPS, underscore between words // keywords: static final public static final int APP_WIDTH = 400; public static final int APP_HEIGHT = 300; // GUI variables private HBox hbox; private Button bnOK, bnLeft, bnRight; // non-GUI, functional variables // note: global variables are initialized at declaration // to a value as close to 0 as possible for the type // boolean: false, numbers: 0, Object: null private int countOK; public void start(Stage stage) { setupLayout(); setupButtons(); Scene scene = new Scene(hbox, APP_WIDTH, APP_HEIGHT); stage.setScene(scene); stage.setTitle("CS112 Demo FX"); stage.show(); } private void setupLayout() { hbox = new HBox(); } private void setupButtons() { bnOK = new Button("OK"); countOK = 0; bnLeft = new Button("Left"); bnRight = new Button("Right"); // event listener: ActionEvent bnOK.setOnAction(e -> { ++countOK; System.out.printf("OK clicked %d times\n", countOK); }); bnLeft.setOnAction(e -> { System.out.printf("LEFT clicked\n"); }); bnRight.setOnAction(e -> { System.out.printf("RIGHT clicked\n"); }); // add buttons to hbox hbox.getChildren().addAll(bnLeft, bnOK, bnRight); } public static void main(String[] args) { launch(args); } }