HelloWorld from the example offered by Oracle in “Getting Started with JavaFX” on a Windows PC. The development of a simple application to the login window and password. Still using the command line with a glimpse of the misunderstandings of the tutorial, which contains the code for this application
In the
previous post, it was possible to get an example from the first section
Getting Started with JavaFX .
HelloWorld.javapackage helloworld; 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.StackPane; import javafx.stage.Stage; public class HelloWorld extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } }
The sample was compiled, run, packaged in a jar and launched from the jar using the command line.
Command line @"C:\Program Files\Java\jdk1.7.0_40\bin\javac" -d out -classpath "C:\Program Files\Java\jre7\lib\jfxrt.jar" src\helloworld\HelloWorld.java @"C:\Program Files\Java\jdk1.7.0_40\bin\java" -classpath "C:\Program Files\Java\jre7\lib\jfxrt.jar;.\out" helloworld.HelloWorld @"C:\Program Files\Java\jdk1.7.0_40\bin\javafxpackager" -createjar -appclass helloworld.HelloWorld -srcdir .\out -outfile HelloWorld -v @"C:\Program Files\Java\jre7\bin\java.exe" -jar HelloWorld.jar @pause
For convenience, each command was hidden in a .cmd file. Funny, but clear. Continue to use these commands.
')
In the second section, it is proposed to make a login and password entry form, with
blackjay controls. Let's try to develop an existing example, leaving the package and class as it is - helloworld.HelloWorld. jfxpub-get_started offers to create a new project in NetBeans. However, we will skip the first three points from “Create the Project”, and the fourth, replacing the body of the “start” method with the code from Example 2-1, applies to the HelloWorld.java file that we have:
@Override public void start(Stage primaryStage) { primaryStage.setTitle("JavaFX Welcome"); primaryStage.show(); }
The task is to remove lines 18 through 29 and replace the primaryStage header. In this form, the file will be compiled and run, but such nonsense will turn out, nothing interesting. Further, the tutorial suggests using the GridPane layout, because it allows you to create columns and rows for the placement of controls, and this seems to be convenient. Let's listen and add the code in front of primaryStage.show ():
GridPane grid = new GridPane(); grid.setAlignment(Pos.CENTER); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(25, 25, 25, 25)); Scene scene = new Scene(grid, 300, 275); primaryStage.setScene(scene)
But this is not compiled:
Compilation errors src\helloworld\HelloWorld.java:18: error: cannot find symbol GridPane grid = new GridPane(); ^ symbol: class GridPane location: class HelloWorld src\helloworld\HelloWorld.java:18: error: cannot find symbol GridPane grid = new GridPane(); ^ symbol: class GridPane location: class HelloWorld src\helloworld\HelloWorld.java:19: error: cannot find symbol grid.setAlignment(Pos.CENTER); ^ symbol: variable Pos location: class HelloWorld src\helloworld\HelloWorld.java:22: error: cannot find symbol grid.setPadding(new Insets(25, 25, 25, 25)); ^ symbol: class Insets location: class HelloWorld 4 errors
Total: the classes GridPane, Pos and Insets were not found. And where to find them? I did not know either. But on the Oracle site I found this
“directory” . From him it is clear that
GridPane - Class in javafx.scene.layout
The grid of rows and columns.
Pos - Enum in javafx.geometry
A set of values for describing the vertical and horizontal positioning and alignment.
Insets - Class in javafx.geometry
Offsets for a 4 side of a rectangular area
This means that you need to add import lines:
import javafx.scene.layout.GridPane; import javafx.geometry.*;
Now the file will be compiled and run, but still nothing interesting. We continue to fill on the recommendations of the tutorial. Add the code with the controls after the line that sets the Padding property of the grid table, that is, before the Scene scene = new Scene (grid, 300, 275).
HelloWorld.java Add-on Text scenetitle = new Text("Welcome"); scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20)); grid.add(scenetitle, 0, 0, 2, 1); Label userName = new Label("User Name:"); grid.add(userName, 0, 1); TextField userTextField = new TextField(); grid.add(userTextField, 1, 1); Label pw = new Label("Password:"); grid.add(pw, 0, 2); PasswordField pwBox = new PasswordField(); grid.add(pwBox, 1, 2); Button btn = new Button("Sign in"); HBox hbBtn = new HBox(10); hbBtn.setAlignment(Pos.BOTTOM_RIGHT); hbBtn.getChildren().add(btn); grid.add(hbBtn, 1, 4); final Text actiontarget = new Text(); grid.add(actiontarget, 1, 6); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { actiontarget.setFill(Color.FIREBRICK); actiontarget.setText("Sign in button pressed"); } });
We get a long sheet of errors. But they are already familiar and understandable. We, it turns out, are not aware of who Text, Font, Label, Color are and some of their friends. We rummage in the mentioned directory, we will look, in what packages their homeland. Add import:
import javafx.scene.text.*; import javafx.scene.control.*; import javafx.scene.paint.*; import javafx.scene.layout.HBox;
This here I have an arbitrary relationship to the asterisks. For educational purposes, it will come down like this.
Now compiled and running. We must briefly disassemble the done:
- javafx.application.Application is the main class of the JavaFX application.
- Its start () method is the entry point.
- The Stage class is a top-level container for creating a user interface (window).
- The Scene class is the next level container and contains all elements.
- Our scene is based on a GridPane, which is like a table, in the cells of which you can arrange interface elements. HGap and VGap define the gap between the columns and rows, respectively. Padding sets the gap between the edge of the table and the edge of the window, which the table will try to withstand as much as possible. The line "grid.setGridLinesVisible (true);" will show the table in the window - make its lines visible.
- Interestingly, the number of rows and columns of a table is not determined by its properties. Instead, we place some elements in certain cells (specifying a column and a row), and the table “itself” grows to the required number. The grid.add method adds an element (the first parameter) to the specified cell (the second and third parameter is a column, row). You can add the fourth and fifth parameters (as when adding a scenetitle) - how many columns and rows to use under the element.
- The main () method is not necessary for JavaFX applications if the JAR file is created using javafxpackager (it embeds JavaFX Launcher in a JAR file). However, it is useful to leave this method to be able to run a JAR file created without JavaFX Launcher, for example, in an IDE with incomplete integration with JavaFX. Again, Swing applications that include JavaFX code require a main () method.
- HBox is such a special panel that allows you to set the alignment of the button, tucked into this panel, which differs from the other elements - from the right edge.
- The rest is fairly obvious from the code.
It remains to paint all this with CSS, draw with FXML and combine these letters together in one application.
And another interesting point that I would like to share at the end. In the last post I mentioned that if in the application code the package is called Package HelloWorld, then the HelloWorld folder is created in the folder. If we correct the case of characters in the package name, everything will work again, although the case of the folder characters will remain “wrong”. Windows OS on the register of the folder name does not care at all, and the system will not recreate or rename it. This will not affect the launch of the program from the HelloWorld.class file. But if you now pack in .JAR and try to execute it, errors will come out. In the archive folder will be with the wrong (already without quotes) case of two characters, and this is enough not to find the main class of the application. You will have to erase the ./out/HelloWorld folder, recompile and create .jar again.