Chap. 1 - Basic GUI Programming
Chap. 1 - Basic GUI Programming
ADVANCED PROGRAMMING
Chapter 1 Basic GUI Programming
Motivations
JavaFX is a new framework for developing Java GUI
programs. The JavaFX API is an excellent example of how
the object-oriented principle is applied. This chapter serves
two purposes. First, it presents the basics of JavaFX
programming. Second, it uses JavaFX to demonstrate
OOP. Specifically, this chapter introduces the framework
of JavaFX and discusses JavaFX GUI components and
their relationships.
Stage
Scene
Button
MyJavaFX Run
MultipleStageDemo Run
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
ButtonInPane Run
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
NodeStyleRotateDemo Run
pane.setRotate(45);
pane.setStyle(
"-fx-border-color: red; -fx-background-color: lightgray;");
FontDemo Run
ShowImage Run
ShowFlowPane Run
ShowGridPane
Run
public class ShowGridPane extends Application { // Create a scene and place it in the stage
@Override Scene scene = new Scene(pane);
// Override the start method in the Application class primaryStage.setTitle("ShowGridPane"); // Set the stage title
public void start(Stage primaryStage) { primaryStage.setScene(scene); // Place the scene in the stage
// Create a pane and set its properties primaryStage.show(); // Display the stage
GridPane pane = new GridPane(); }
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5)); /**
pane.setHgap(5.5); * The main method is only needed for the IDE with limited
pane.setVgap(5.5); * JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
ShowBorderPane Run
ShowHBoxVBox Run
Throughout this book, the prefixes lbl, bt, chk, rb, tf, pf, ta, cbo, lv,
scb, sld, and mp are used to name reference variables for Label,
Button, CheckBox, RadioButton, TextField, PasswordField,
TextArea, ComboBox, ListView, ScrollBar, Slider, and
MediaPlayer.
LabelWithGraphic Run
ButtonDemo Run
return pane;
}
36 Chapter 1 Basic GUI Programming CSC 3104 Advanced Programming
TextField
A text field can be used to enter or display a string. TextField is a
subclass of TextInputControl.
TextFieldDemo Run