JavaFX, HelloWorld - continued

    HelloWorld from an example provided by Oracle in "Getting Started with JavaFX" on a Windows PC. Development of a simple application to the login and password entry window. Still using the command line with ajar of the misunderstandings of the tutorial in which the code for this application is given.



    In a previous post, it turned out to get an example from the first section of Getting Started with JavaFX .
    HelloWorld.java
    package 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() {
                @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 example was compiled, launched, packaged in jar and launched from 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
    

    Each command was hidden in a .cmd file for convenience. Funny, but clear. We continue to use these commands.

    In the second section, it is proposed to make a login and password entry form, with blackjet controls. Let's try to develop the existing example, leaving the package and class & mdash helloworld.HelloWorld as they are. jfxpub-get_started suggests creating a new project in NetBeans. However, we skip the first three paragraphs from “Create the Project”, and the fourth, replacing the body of the “start” method with the code from Example 2-1, applies to the existing HelloWorld.java file:
        @Override
    	public void start(Stage primaryStage) {
    	    primaryStage.setTitle("JavaFX Welcome");
    	    primaryStage.show();
    	}
    

    The task is to delete lines 18 through 29 and replace the primaryStage header. In this form, the file will compile and run, but such nonsense will turn out, nothing interesting. The tutorial further suggests using the GridPane layout, because it allows you to create columns and rows to place controls, and it's kind of convenient. We obey and add the code before 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 will not compile:
    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: GridPane, Pos, and Insets classes not found. And where to look for them? I didn’t know either. But on the Oracle site I found just such a "reference" . It is clear from it that
    GridPane - Class in javafx.scene.layout
        GridPane lays out its children within a flexible grid of rows and columns.
    Pos - Enum in javafx.geometry
        A set of values ​​for describing vertical and horizontal positioning and alignment.
    Insets - Class in javafx.geometry
        A set of inside offsets for the 4 side of a rectangular area
    

    This means adding import lines:
    import javafx.scene.layout.GridPane;
    import javafx.geometry.*;
    

    Now the file will compile and run, but still nothing interesting. We continue to fill in according to the recommendations of the tutorial. Add code with controls after the line that sets the Padding property of the grid table, that is, before Scene scene = new Scene (grid, 300, 275).
    Additive to HelloWorld.java
    	    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() {
    		@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. It turns out that we don’t know who Text, Font, Label, Color and some of their friends are. We will rummage in the mentioned reference book and see in which packages their homeland is. Add import:
    import javafx.scene.text.*;
    import javafx.scene.control.*;
    import javafx.scene.paint.*;
    import javafx.scene.layout.HBox;
    

    Such here I have here an arbitrary attitude towards the stars. For educational purposes, it will do so.

    Now compiles and works. It is necessary to 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 the elements.
    • Our scene is based on GridPane, which is like a table, in the cells of which you can arrange the interface elements. HGap and VGap define the gap between 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 set by its properties. Instead, we place some elements in specific cells (by specifying a column and row), and the table "itself" grows to the desired number. The grid.add method adds an element (the first parameter) to the specified cell (the second and third parameters - column, row). You can add the fourth and fifth parameters (as when adding a scenetitle) - how many columns and rows to use for the element.
    • The main () method is not necessary for JavaFX applications if the JAR file is created using the javafxpackager (it embeds the JavaFX Launcher in the JAR file). However, it is useful to leave this method to be able to run a JAR file created without the JavaFX Launcher, for example, in an IDE with incomplete integration with JavaFX. Again, Swing applications that include JavaFX code require the main () method.
    • HBox is such a special socket that allows you to set the alignment of a button inserted into this socket to be different from the rest of the elements - from the right edge.
    • The rest is pretty obvious from the code.

    It remains to color it all with CSS, draw with FXML and combine these letters together in one application.

    And one more interesting point that I would like to share in the end. In a previous post, I mentioned that if you call package HelloWorld in the application code, then the HelloWorld folder will be created in the ./out folder, the program will compile, but will not start - the class will not be found. Correct the case of characters in the package name - everything will work again, although the case of the folder’s characters will remain “incorrect”. Windows OS does not care about the folder name register, 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 up. In the archive, the folder will be with the incorrect (already without quotes) case of two characters, and this is enough to not find the main class of the application. You’ll have to delete the ./out/HelloWorld folder,

    Also popular now: