为什么我的 javafx 阶段不想加载

Why does my javafx stage not want to load

这是java

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class mains extends Stage {

public static void main(String[] args) {
    new JFXPanel();
    Platform.runLater(new Runnable(){

        @Override
        public void run() {
            new mains();
        }

    });
}
void go(){
    new JFXPanel();
    new mains().show();
}

public mains() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
            "LOL.fxml"));
    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);

    try {
        fxmlLoader.load();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@FXML
private Button button;

@FXML
private Label label;

@FXML
void push(ActionEvent event) {

}

}

这是 fxml http://pastebin.com/uzBrMRDV 我收到加载异常,它说已经指定了 Root。 如果我删除 setRoot(this);它根本不加载 我对 JFX 感到非常沮丧...... 无论如何从控制器本身加载 FXML 文件,如舞台

删除行

fxmlLoader.setRoot(this);

您的 FXML 将根定义为 AnchorPane(并且您不能将根设置两次,这就是您收到错误的原因)。

由于当前class是一个Stage,而FXMLLoader加载了一个AnchorPane,你需要将加载的AnchorPane放在一个Scene并在舞台布景。替换

fxmlLoader.load();

AnchorPane root = fxmlLoader.load();
Scene scene = new Scene(root); // optionally specify dimensions too
this.setScene(scene);