官术网_书友最值得收藏!

Bootstrapping the application

As noted in the introduction, this will be a JavaFX-based application, so we'll start by creating the skeleton for the application. This is a Java 9 application, and we intend to make use of the Java Module System. To do that, we need to create the module definition file, module-info.java, which resides in the root of our source tree. This being a Maven-based project, that would be src/main/java:

    module procman.app { 
      requires javafx.controls; 
      requires javafx.fxml; 
    } 

This small file does a couple of different things. First, it defines a new procman.app module. Next, it tells the system that this module requires two JDK modules: javafx.controls and javafx.fxml. If we did not specify these two modules, then our system, which we'll see below, would not compile, as the JDK would not make the required classes and packages available to our application. These modules are part of the standard JDK as of Java 9, so that shouldn't be an issue. However, that may change in future versions of Java, and this module declaration will help prevent runtime failures in our application by forcing the host JVM to provide the module or fail to start. It is also possible to build custom Java runtimes via the J-Link tool, so missing these modules is still a possibility under Java 9. With our module configured, let's turn to the application.

The emerging standard directory layout seems to be something like src/main/java/<module1>, src/main/java/<module2>, and so on. At the time of writing this book, while Maven can be coaxed into such a layout, the plugins themselves, while they do run under Java 9, do not appear to be module-aware enough to allow us to organize our code in such a manner. For that reason, and for the sake of simplicity, we will treat one Maven module as one Java module and maintain the standard source layout for the projects.

The first class we will create is the Application descendant, which NetBeans created for us. It created the Main class, which we renamed to ProcessManager:

    public class ProcessManager extends Application { 
      @Override 
      public void start(Stage stage) throws Exception { 
        Parent root = FXMLLoader 
         .load(getClass().getResource("/fxml/procman.fxml")); 
         
        Scene scene = new Scene(root); 
        scene.getStylesheets().add("/styles/Styles.css"); 
         
        stage.setTitle("Process Manager"); 
        stage.setScene(scene); 
        stage.show(); 
      } 
 
      public static void main(String[] args) { 
        launch(args); 
      } 
    } 

Our ProcessManager class extends the JavaFX base class, Application, which provides a variety of functionality to start and stop the application. We see in the main() method that we simply delegate to Application.launch(String[]), which does the heavy lifting for us in starting our new application.

The more interesting part of this class is the start() method, which is where the JavaFX life cycle calls back into our application, giving us the opportunity to build the user interface, which we'll do next.

主站蜘蛛池模板: 盐池县| 巴东县| 林芝县| 玉林市| 阳信县| 潞城市| 尚义县| 民丰县| 同仁县| 乌鲁木齐市| 荥阳市| 衡南县| 徐水县| 东乡族自治县| 定陶县| 古浪县| 宿州市| 城市| 马尔康县| 桂林市| 正蓝旗| 天等县| 肥乡县| 洛浦县| 奈曼旗| 从化市| 清原| 乌拉特中旗| 湘乡市| 淄博市| 临安市| 长泰县| 方城县| 灵宝市| 寻乌县| 渭南市| 北流市| 宝应县| 隆尧县| 永嘉县| 格尔木市|