Introduction
This is how the basic template for a JavaFX applications look like:package javafx.tutorials; import javafx.application.Application; import javafx.stage.Stage; public class Tutorial1 extends Application { @Override public void start(Stage stage) throws Exception { } public static void main(String[] args) { Application.launch(args); } }We have to derive our main class from the javafx.application.Application class. Doing this will force us to override the start(Stage stage) function. That member function is the main entrance to start JavaFX applications.
- The Application() class has a static member method launch(). We have to call that in the main() method to initialize JavaFX. I have to admit I didn't read the documentation so I don't no exactly what the method is doing with the arguments of the main() but at least I know that calling it will call the start(Stage stage) method.
- The Stage() class is the main, let's say window. Everything we will see is managed by that class. Scene Graph is used to manage elements in the scene. The Scene Graph is called Scene() and the elements are called Node(). So we can put some nodes into the scene like Buttons, TextFields, Tables etc.
- A Scene can have only one root node. Every other elements we are going to use will go into that node.
The first raw window
Here is a short example how to show a window with a title. Later we will see we don't have to specify the width and height of the window in the stage. We'll do that in the Scene itselft.
package javafx.tutorials; import javafx.application.Application; import javafx.stage.Stage; public class Tutorial1 extends Application { @Override public void start(Stage stage) throws Exception { // Set the title of the main windows. stage.setTitle("Tutorial 1"); // Set the width of the window. stage.setWidth(320); // Set the height of the window. stage.setHeight(200); // Show the window. stage.show(); } public static void main(String[] args) { Application.launch(args); } }
Nice and quite easy without any problems. Now if we want to put some elements onto the window like a button we have to do more. The Stage() is just the window now we need the Scene() which will holds the elements we are going to put on the Stage window. In the next section we go one step further in the development process. :D
The first more "advanced" example.
There is a Node() container called VBox() and I'm going to use that as root. Every added Node() within that container will be arranged vertically. Easy right :)?I call the main window Tutorial 1 and will add 2 buttons into the Scene.
package javafx.tutorials; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Tutorial1 extends Application { @Override public void start(Stage stage) throws Exception { // Set the title of the main windows. stage.setTitle("Tutorial 1"); // Here we create our root node. VBox root = new VBox(); // And here we create our first scene with the width 200 and height 100. Scene scene = new Scene(root, 200, 100); // Let's add some stuff to the root node. Button button = new Button("Click Me!"); Button button2 = new Button("Click Me Too!"); // Now we add the button to the root node. root.getChildren().add(button); root.getChildren().add(button2); stage.setScene(scene); stage.show(); } public static void main(String[] args) { Application.launch(args); } }If we start now we will see two buttons. Super easy. Clicking onto the buttons will not do anything so we need some kind of trigger.
Buttons events
We can set the setOnAction() ActionEvent handler for the buttons and get some interaction. See here:package javafx.tutorials; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Tutorial1 extends Application { @Override public void start(Stage stage) throws Exception { // Set the title of the main windows. stage.setTitle("Tutorial 1"); // Here we create our root node. VBox root = new VBox(); // And here we create our first scene with the width 200 and height 100. Scene scene = new Scene(root, 200, 100); // Let's add some stuff to the root node. Button button = new Button("Click Me!"); button.setOnAction(new EventHandlerIf we click the buttons we can see some text on the console. If we don't have a console we don't see anything hahahahaha :D. Just a joke sorry. Ok than let's add same text. We can use the Label() class to put some labels on the Screen. I will put the label object after the buttons. Here the final code.() { @Override public void handle(ActionEvent event) { System.out.println("You clicked me."); } }); Button button2 = new Button("Click Me Too!"); button2.setOnAction(new EventHandler () { @Override public void handle(ActionEvent event) { System.out.println("You clicked me too."); } }); // Now we add the button to the root node. root.getChildren().add(button); root.getChildren().add(button2); stage.setScene(scene); stage.show(); } public static void main(String[] args) { Application.launch(args); } }
package javafx.tutorials; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Tutorial1 extends Application { @Override public void start(Stage stage) throws Exception { // Set the title of the main windows. stage.setTitle("Tutorial 1"); // Here we create our root node. VBox root = new VBox(); // And here we create our first scene with the width 200 and height 100. Scene scene = new Scene(root, 200, 100); final Label label1 = new Label(); final Label label2 = new Label(); // Let's add some stuff to the root node. Button button = new Button("Click Me!"); button.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { System.out.println("You clicked me."); label1.setText("You clicked me."); } }); Button button2 = new Button("Click Me Too!"); button2.setOnAction(new EventHandler () { @Override public void handle(ActionEvent event) { System.out.println("You clicked me too."); label2.setText("You clicked me too."); } }); // Now we add the button to the root node. root.getChildren().add(button); root.getChildren().add(button2); root.getChildren().add(label1); root.getChildren().add(label2); scene.setRoot(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) { Application.launch(args); } }
And the picture above is how it looks like. Quite fun here. We see here where we are going with all that hopefully. That's how easy it is to write RCA with JavaFX. See you in the next tutorial.
No comments:
Post a Comment