13.2 Wecome to JavaFX Program

Cliquez içi pour plus de vidéos disponibles sur notre chaine youtube !

Let’s start changing a few things. Let’s, start by changing the title of the window to "Hello javafx". Now, let’s say "Welcome to JavaFX". We can do this using code or modifying the fxml file Let’s change the application for now so that it doesn’t use the fxml file at all. So I’m going to comment on the call of the fxml file on line 13. And then we will add the code that creates a grid pane node we will define the node as the root of our scene. So to do this, I’ll start typing GridPane root = new GridPane() Then root.setAlignement In parentheses. Pos.center that will center our text. Then the next line, root.setVgap ( 10). root.set Hgap(10)* The alignment determines where the grid will lie in the width and height of the grid shutters. When we perform our application, Now we get the same result as before this time without using the fxml file Then what we’re going to do is add the welcome message on javaFX to the window So we’re going to use label control. So I’m going to type «label», we have to import it as a variable name, welcome = new Label (Welcome to JavaFx ) Then we’ll do root.getChildren(). add (welcome) We actually added our label to GridPane And we did this by calling the child method that returns a list and then we use the add method to add our label to the list. So let’s run our application you can now see it says welcome on javafx in the center of the window. All right, then, let’s change the color of the text to green. We do this by using the labels and the setTextFill() method that allows you to make the color and we pass in parameter the color we want So welcome.setTextFill (Color .GREEN) we must import color. If, we execute the application We now see the text displayed in green. All right, let’s change the font now and put the font in bold. To do this, we call the font method and define the font on something we want. Welcome.setFont In parentheses. Font point font In parentheses. Times new Roman I’m going to put the fat of the text in bold Font Weight.BOLD and size 70 And if we reread the application you can see that it is now quite large. In fact, the text is too big for the window so we could reduce the font size but instead increase the window size by changing 300 here to 710 It’s still too big, so 730. Then, we run the program again now, it shows well That was all for the first part, in the next video, we will see the second part. I’ll see you around.