FXMLLoader.load() 永远不会退出 (JavaFX 8)

erz*_*zr2 6 java javafx java-8 fxml

我正在尝试加载我的应用程序的主屏幕,实际上它从未实际运行并显示屏幕。经过进一步调查(通过 NetBeans 调试器运行),我发现我的代码在 FXMLLoader.load(url); 之后永远不会执行。-- 它停在那里,并且不会抛出任何错误。我确实知道该网址是正确的 - 我检查了它的值,它是正确的。谁知道怎么修它?提前致谢!


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

<?import java.lang.*?>
<?import javafx.scene.text.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-      Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8"    xmlns:fx="http://javafx.com/fxml/1" fx:controller="graphics.MainScreenController">
<children>
<Text fx:id="funds" layoutX="489.0" layoutY="383.0" strokeType="OUTSIDE" strokeWidth="0.0" text="USD 52,356,000.07">
</Text>
</children></AnchorPane>
Run Code Online (Sandbox Code Playgroud)
package graphics;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 */
 public class MainScreenController extends Application implements Initializable {


@FXML
private Text funds;

/**
 * Initializes the controller class.
 */
public void initialize(URL url, ResourceBundle rb) {  
    start(new Stage());

}    

@Override
public void start(Stage primaryStage){
    Parent root = null;
    try {
        URL url = getClass().getResource("MainScreen.fxml");
        root = FXMLLoader.load(url);
    } catch (Exception ex) {
        Logger.getLogger(MainScreenController.class.getName()).log(Level.SEVERE, null, ex);
    }
    Scene scene = new Scene(root,1200,800);
    primaryStage.setTitle("Title");
    primaryStage.setScene(scene);
    primaryStage.show();
}

}
Run Code Online (Sandbox Code Playgroud)

Aar*_*ron 5

您已经使用initialize() 方法创建了一个无限循环。初始化方法由 FXMLLoader 自动调用。

您正在从initialize()方法调用start(),该方法加载MainScreen.fxml文件,该文件创建一个新的MainScreenController实例。FXMLLoader 自动在新实例上调用initialize(),而新实例又调用start(),后者再次加载MainScreen.xml,依此类推。

自 JavaFX 2.2 起,Initialized 接口不再是加载 FXML 后初始化控制器的首选方式。这已更改为使用 @FXML 注释。问题 JavaFX 中的“将位置和资源属性自动注入控制器”是什么?显示了新方法的示例。

另外,我不确定您为什么要在initialize()方法中创建一个新的Stage()。通常要获取舞台,您需要在应用程序上调用 launch() ,并且将自动为您调用舞台的 start() 方法。一个例子在这里:http ://docs.oracle.com/javafx/2/get_started/hello_world.htm