This tutorial shows the benefits of using javafx fxml, which is an XML-based language that provides the structure for building a user interface separate from the application logic of your code. (This tutorial demonstrates the advantages of using javafx fxml. fxml is an XML-based language that provides a structure for building user interfaces by separating application logic)
If you started this document from the beginning, then you have seen how to create a login application using just javafx. (If you read this document from the beginning, you will see how to use javafx to create a logon program) Here, you use fxml to create the same Login User Interface, separating the application design from
The application logic, thereby making the code easier to maintain. (Here, you use fxml to create the same logon program user interface, which is separated from the application logic, so as to make the code easy to manage) the login user interface you build in this tutorial is shown in Figure 4-1. (the logon user interface you want to create is shown in Figure 4-1)
Figure 4-1 Login User Interface
The title of the application is "welcome. "(the title of the application is" welcome ") This application has a gray background with a linen-like texture. (The background is linen gray Beijing) from top to bottom, the controls in this application are a textfield with the label "user name," A passwordfield
The label "password," and a button with the label "sign in. "(from start to end, the control is followed by the text input box labeled" User Name ", the Password text labeled" password ", and the button labeled" sign in)
This tutorial uses netbeans ide. Ensure that the version of netbeans ide that you are using supports javafx 2.2. See the system requirements for details.
Set up the project (this tutorial uses the netbeans integrated development environment to ensure that you use netbeans to support javafx2.2 and view detailed system requirements)
Your first task is to set up a javafx fxml project in netbeans ide :( at first, your task is to create a javafx fxml project in netbeans)
From the File menu, choose new project. (select a new project from the file directory)
In the javafx Application category, choose javafx fxml application. Click Next. (In the javafx project category, select the javafx fxml project and click Next)
Name the project fxmlexample and click Finish. (name the project fxmlexample and click Finish)
Netbeans ide opens an fxml project that has des the code for a basic Hello world application. the application implements des three files :( netbeans opens a fxml project, including the code of the basic Hello World project, which contains three files)
Fxmlexample. java. This file takes care of the standard Java code required for an fxml application. (fxmlexample. Java, this file contains the standard Java code required by the fxml Project)
Sample. fxml. This is the fxml source file in which you define the user interface. (Sample. fxml, the source code of this file is where you define the user interface)
Samplecontroller. java. This is the Controller file for handling the mouse and keyboard input. (samplecontroller. Java, This is the control file for processing mouse and keyboard input)
Rename sample. Java to fxmlexamplecontroller. Java so that the name is more meaningful for this application. (rename the sample. Java file to fxmlexamplecontroller. java. This name is more meaningful for this project)
In the projects window, right-click sample. Java and chooserefactor thenrename. (On the project page, right-click sample. Java, choose refactor, and rename)
Enter fxmlexamplecontroller, and click
Refactor. (enter fxmlexamplecontroller and click refactor)
Rename sample. fxml to fxml_example.fxml. (rename the sample. fxml file as fxml_example.fxml)
Right-click sample. fxml and choose
Rename. (right-click sample. fxml and choose rename)
Enter fxml_example and click OK. (enter fxml_example and click OK)
Load the fxml source file (load the fxml source file)The first file you edit is the fxmlexample. java file. (the file you want to edit is fxmlexample. java) This file contains des the code for setting up the application main class and for defining the stage and scene. (This file contains the main class of the project and defines stage and scene.) more specific to fxml, the file
UsesFXMLLoaderClass, which is responsible for loading the fxml source file and returning the resulting object graph. (For fxml, this file uses the fxmlloader class, which is responsible for loading the object graph returned by the fxml source file)
Make the changes shown in bold in
Example 4-1. (modify according to the black characters in the image Example 4-1)
Example 4-1 fxmlexample. Java
@Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml")); stage.setTitle("FXML Welcome"); stage.setScene(new Scene(root, 300, 275)); stage.show(); }
A good practice is to set the height and width of the scene when you create it, in this case 300 by 275; otherwise the scene defaults to the minimum size needed to display its contents. (a good habit is to set the height and width of scene when you create it. In this example, the height is 300 and the width is 275. Otherwise, scene is the minimum length required to display the content by default)
Modify the import statements (modify the Import Statement)
Next, edit the fxml_example.fxml file. This file specifies the user interface that is displayed when the application starts. The first task is to modify the import statements so your code looks likeexample
4-2. (edit the fxml_example.fxml file below. This file specifies the user interface displayed when the application starts. The first task is to modify the import statement like Example 4-2)
<?xml version="1.0" encoding="UTF-8"?><?import javafx.geometry.*?><?import javafx.scene.layout.*?><?import javafx.scene.control.*?><?import javafx.scene.text.*?>
As in Java, class names can be fully qualified (including the package name), (for example, in Java, the class name must be completely specified, including the package name) or they can be imported using the Import Statement, as shown inexample
4-2. (or import the import statement shown in Example 4-2) if you prefer, you can use specific import statements that refer to classes. (If you like it, you can also use a specific import statement pointing to a class)
Create a gridpane layout (create a gridpane layout)The Hello world Application Generated by netbeans usesAnchorPaneLayout. (This Hello World project netbeans is generated using the anchorpane layout) for the login form, you will useGridPaneLayout because it enables you to create a flexible grid of rows and
Columns in which to lay out controls. (For Login forms, you can use the gridpane layout because it allows you to create flexible rows and columns for control placement)
RemoveAnchorPaneLayout and its children and replace it withGridPaneLayout inexample 4-3. (delete the anchorpane layout in Example 4-3 and replace it with gridpane)
In this application,GridPaneLayout is the root element of the fxml document and as such has two attributes. (In this application, the gridpane layout is the root element of fxml and has two attributes in this province)fx:controllerAttribute is required when you specify controller-based event
Handlers in your markup. (FX: controller attribute is required when you specify the controller-based event processor in your tag)xmlns:fxAttribute is always required and specifiesfxNamespace. (xmlns: The FX attribute is required, and it specifies the Command Space)
The remainder of the code controls the alignment and spacing of the grid pane. (the remaining code controls the alignment property changes the default position of the grid from the top left of the scene to the center. (The alignment attribute changes the default position of the grid to the center on the left.)gap
Properties manage the spacing between the rows and columns, whilepaddingProperty manages the space around the edges of the grid pane. (The gap property controls the space between rows and columns, and the padding property controls the controls around the edge of the grid pane)
As the window is resized, the nodes within the grid pane are resized according to their layout constraints. (when the window size is adjusted, the nodes in Grid pane are adjusted according to the layout constraints.) In this example, the grid remains in the center when you grow or shrink the window. (In this example, the grid is kept in the center when you zoom in and out the window.)
Padding properties ensure there is a padding around the grid when you make the window smaller)
Add text and password fields (add text and Password text fields)Looking back
Figure 4-1, you can see that the login form requires the title "welcome" and text and password fields for gathering information from the user. (looking back at Figure 4-1, you can see that the logon form requires the title "welcome" and the text and password fields to collect information from the user.) The Code inexample
4-4 is part ofGridPaneLayout and must be placed above</GridPane>Statement. (the code in Example 4-4 is part of the gridpane layout and must be placed on the </gridpane> statement)
Example 4-4 text, label, textfield, and password field controls
<Text text="Welcome" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="2"/> <Label text="User Name:" GridPane.columnIndex="0" GridPane.rowIndex="1"/> <TextField GridPane.columnIndex="1" GridPane.rowIndex="1"/> <Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="2"/> <PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
The first line createsTextObject and sets its text value
Welcome. (The first line creates a Text object and sets its value to welcome.)GridPane.columnIndexAnd
GridPane.rowIndexAttributes correspond to the placement of
TextControl in the grid. (The gridpane. columnindex and gridpane. rowindex attributes comply with the central value of the text control in the grid) The numbering for rows and columns in the grid starts at zero, and the location of
TextControl is set to (0, 0), meaning it is in the first row of the first column. (the rows and columns in the grid start with 0, and the position of the text control is set to (0, 0), which indicates the first row and the first column.)
GridPane.columnSpanAttribute is set to 2, making the welcome title span two columns in the grid. (gridpane. the columnspan attribute is set to 2, indicating that the welcome title spans two columns in the grid.) You will need this extra width later in the tutorial When you add a style sheet to increase
Font size of the text to 32 points. (In this tutorial, you need to add a style sheet to change the font size of the text to 32 points)
The next lines createLabelObject with textUser NameAt column 1, row 0 and
TextFieldObject To the right of it at column 1, Row 1. (The next row is in column 1, Row 1 creates a tag object, the text of which is the user name, and row 1 on the right creates a text domain object.) Another
LabelAndPasswordFieldObject are created and added to the grid in a similar fashion. (another tag and password domain are created and added in the same style)
When working with a grid layout, you can display the grid lines, which is useful for debugging purposes. In this case, set
gridLinesVisiblePropertytrueUsing the statement
<gridLinesVisible>true</gridLinesVisible>. Then, when you run the application, you see the lines for the grid columns and rows as well as the gap properties, as shown in
Figure 4-2.
Figure 4-2 login form with grid lines