簡體   English   中英

為什么我的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 (並且不能兩次設置根,這就是得到錯誤的原因)。

由於當前類是Stage ,並且FXMLLoader加載AnchorPane ,因此您需要將加載的AnchorPaneScene並在舞台中設置場景。 更換

fxmlLoader.load();

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM