Continue your JavaFX Components learning via this tutorial.
Example 1: Apps
Here are the code:
Step 1: Create Project
- Open your favorite Java IDE.
- In the menu go to
File --> Create New Project
.
Step 2: Dependencies
No dependencies are needed for this project.
Step 3: Write Code
Our code will comprise the following java files:
AppExample.java
- In your editor or IDE, create a file known as
AppExample.java
. - Then add the following code:
(a). AppExample.java
Go ahead and add the following imports:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
We will need to extend our class using the extend
keyword. By doing that our class
can make use of inheritance to derive properties and functions defined in the parent class
.
public class AppExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
First of all we will need a main method. This is the entry point of our Java application.
public static void main(String[] args) {
Here is the full code:
package com.jenkov.javafx.apps;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class AppExample extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
Label label = new Label("Minimal app");
Scene scene = new Scene(new Pane(label), 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Download
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |