Introduction to GUIs
The JavaFX GUI framework allows you to create modern, professional-looking applications. You will begin to see that inheritance and interfaces are going to play a ginormous role here.
Important Packages
javafx.application
This contains theApplication
class, which will manage the life-cycle of a javaFX app.javafx.stage
This contains top-level windows, including things such as file choosers, pop-up windows, andStage
, which is the top-level container window for an application.javafx.scene
This package and its descendants includes a panoply of things we will press into service, These items include buttons, text areas, menus and slider bars. This is the home of many of widgets.javafx.event
This packages hold classes that are useful in responding to such things as keystrokes, mouse clicks, and the selection of menu items.
What is an Abstract Class? The class Application
is abstract; we will explore the meaning of this.
Abstract classes allow us to partially implement objects. Like regular
classes, they can have state. Unlike regular classes, they can have
unimplemented methods which we mark abstract
. Classes that
are not abstract are called concrete classes. You can only create
instances of concrete classes.
Here is the syntax for creating an abstract method. These methods are bodyless and are terminated with a semicolon.
access abstract returnType methodName(arg(s));
Here are the rule of the road.
- Any class can be declared abstract.
- If a method in a class is undefined, you must mark it abstract.
- If a class contains any abstract method, it must be marked abstract.
- Concrete classes that are subclasses of an abstract class must implement all abstract methods in the ancestor classes.
Interfaces vs. Abstract Classes
- Advantage vs. interfaces: You can have state in an abstract class. You cannot in an interface
- Interfaces can have default methods.
- You can only inherit once. Once you inherit, yoiu "Blow your inheritance," but can implement any number of interfaces.
A Minimal Program that Shows a Window We will build this in a couple of dozen lines of code.
import javafx.application.Application;
import javafx.stage.Stage;
public class SimpleGUI extends Application
{
@Override
public void start(Stage primary)
{
primary.setTitle("SimpleGUI Demo");
primary.show();
}
}
The Complete Program Life-Cycle We will write and app that demonstrates this.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.Scene;
import javafx.application.Platform;
public class LifeCycleDemo extends Application
{
public LifeCycleDemo()
{
System.out.println("I am being Constructed");
}
@Override
public void init()
{
System.out.println("I am setting up housekeeping");
}
@Override
public void start(Stage primary)
{
primary.setTitle("SimpleGUI Demo");
HBox v = new HBox();
Button butt = new Button("Quit Rudely");
butt.setOnAction( e -> Platform.exit());
v.getChildren().add(butt);
v.getChildren().add(new Button("button2"));
v.getChildren().add(new Button("button3"));
v.getChildren().add(new Button("button4"));
v.getChildren().add(new Button("button5"));
Scene s = new Scene(v, 400,400);
primary.setScene(s);
primary.show();
}
@Override
public void stop()
{
System.out.println("I am hosing down Bourbon Street.");
}
}
We saw this.
- Constructor runs.
init()
runs.start(Stage primary)
is our main application loop which runs until the user quits. Here the user quits by hitting the go-away button..- If the go-away button is clicked,
stop()
runs. - Execution terminates and the OS reclaims the process's memory.
An Abstract Class Use Case
We have no idea whatsoever as to how to draw a general "shape," so we put and
make its draw
method absract.
import javafx.scene.paint.Color;
import javafx.scene.canvas.GraphicsContext;
public abstract class AShape //you cannot create an instance of this class
{
private Color color;
private double xLoc;
private double yLoc;
public AShape(Color color, double xLoc, double yLoc)
{
this.color = color;
this.xLoc = xLoc;
this.yLoc = yLoc;
}
public Color getColor()
{
return color;
}
public abstract void draw(GraphicsContext pen);
}
Now let us subclass it with a Rectangle
class.
public class Rectangle extends AShape
{
public Rectangle(Color color, double xLoc, double yLoc)
{
super(color, xLoc, yLoc);
}
Here is where we start next time.