File: "fx1.java"
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.event.* ;
public class fx1 extends Application
{
// launch the application
public void start(Stage stageObj )
{
// set title for the stage
stageObj.setTitle("creating buttons");
// create a button
Button buttonObj = new Button("button");
buttonObj.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println( "Button clicked." ) ;
}
});
// create a stack pane
StackPane pane = new StackPane();
// add button
pane.getChildren().add(buttonObj);
// create a scene
Scene sc = new Scene(pane, 200, 200);
// set the scene
stageObj.setScene(sc);
stageObj.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output:
The "EventHandler" is not from the AWT package. In fact all the classes used in the above program are from the Java FX package. A "stage" is a top level container ( similar to a Frame in AWT or a JFrame in Swing ) . A scene can contain visual elements . It can contain panes that have layout managers and can contain buttons, text fields and other graphic elements.
Exercise 1
Solution 1