Create a Stage model and write Text inside it in the JavaFX framework - ProgrammerTech
Language:

Search

Create a Stage model and write Text inside it in the JavaFX framework

  • Share this:
Create a Stage model and write Text inside it in the JavaFX framework

Create a Stage Model in JavaFX

Stage model in the JavaFX library, in this lesson we will learn how to build the first application in JavaFX, as we said in the previous lesson that JavaFX provides us with ready-made applications and functions to help us create the application with ease. Today we will learn how to create a simple Stage model and then we will follow the rest of the lessons step by step. We will learn to build a model using the Stage package. We will need two packages, Application and Stage. With the application package the class we created inherits from Application, and Application is of type Abstract. As long as our class inherits from Application, it must execute the function provided by Application. The Stage package means the form itself, and it divides the window into two parts, the first section is the menu bar and the second section is the area in which we add things to the window, meaning that everything that will be added to the window will be linked with Stage.

Example 1

package sajafx;
import javafx.application.Application;
import javafx.stage.Stage;
public class Sajafx extends Application{
    public void start(Stage s)
    {
        s.setTitle("welcome programmer tech");
        s.show();
    }
     public static void main(String []args){
        launch(args);
     }
}

We called the two packages that we needed in this example, the Application package and the Stage package, then the class Sajafx was inheriting from Application. After that we create the interface form via public void start(Stage s) and specify the title that appears in the title of the interface name using s.setTitle and show the form using s.show. And we entered the main program and called the args function of the main function start.

And when the code is executed, the form we created will appear as follows.

create a stage javafx

 

Example 2

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class Main extends Application {
    public void start(Stage stage) {
        Text text = new Text("Saja CS");
        text.setFont(new Font(42));
        text.setX(60);
        text.setY(160);
        Group root = new Group();
        root.getChildren().add(text);
        Scene scene = new Scene(root, 600, 300);
        stage.setTitle("Saja CS");
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

At the beginning of the code, we called some packages that will commit us to this lesson, then we created the class of the text that will appear inside the window, which is Text text, and we set the font size 42 using setFont. And we choose where the text appears inside the frame and we specify the name of this form via setTitle. And we show the model using stage.show.

And when you execute this code, the result will be.

stage javafx

 

Write a title in the scene Scene

We will learn how to write a title that appears on the scene and then add the scene to the stage form. To write an address we will use the text.Text package and to format the address we will use the text.Font package These two packages are children of scene . We start inside the start function. We got acquainted with it in the previous lessons and said that it belongs to the Application Interface, and as long as we inherit from the Interface, we must compulsorily implement the start function. And inside the start function we build the program.

Example 3

package mhamad;
import javafx.application.Application;
import javafx.event.EventType;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Mhamad extends Application {
    public void start(Stage stage) {
        Text txt=new Text();
        txt.setX(50.0);
        txt.setY(100.0);
        txt.setText("welcome in my website");
        txt.setFont(new Font(30));
        Group root=new Group(txt);
        Scene scene=new Scene(root,450,300);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

At the beginning of this program, we called several packages that required us to write the address, including the text.Font package. In the format of the address we will write and the text.Text package is a private package. By writing the title and the rest of the packages that we summoned, we explained it in the previous lessons. Then we entered the start function and started writing the program. We first started by writing the class of creating the address Text, then we set the point X with the setX function and set its alignment from right to left. And we set the coordinates of the Y point and align it from top to bottom and then we write the address that will appear in the scene using the setText function. And then we chose the font size 30 by setFont and then we added the element to the root because it works with graphics.

And as we used to in the previous lessons, we define the width and length of the model and add it to the root, then add the root to the scene and then show the model. Then we run the start function from within the main program via launch(args).

And when we run the program, we will get the following result.

welcome a stage javafx

Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com