使用扩展Application Class的Start Method调用Fxml文件

时间:2014-12-16 08:26:34

标签: javafx

我想创建一个新的FXML文件,其中包含很少的像Label这样的控件。但是当我运行应用程序时,有些时候来自不显示,有时表单显示标签不显示。

我写了以下代码:

DashBoard.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="603.0" prefWidth="1063.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.DashBoard.DashBoardController">
    <stylesheets>
        <URL value="@dashboard.css" />
    </stylesheets>
   <children>
      <Label fx:id="welcome" layoutX="403.0" layoutY="52.0" text="Label" />
   </children>

</AnchorPane>

DashBoardController.java:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

/**
 * FXML Controller class
 *
 * @author DELL
 */
public class DashBoardController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @FXML
    private Label welcome;
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

DashRun.java:

package com.DashBoard;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class DashRun extends Application{
       @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("DashBoard.fxml"));
        Scene scene = new Scene(root);
          Label a = new Label();
        stage.setTitle("Dash Board");
        stage.setScene(scene);     
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }

}

0 个答案:

没有答案