简体   繁体   English

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

[英]Why does my javafx stage not want to load

This is the java 这是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) {

}

}

here is the fxml http://pastebin.com/uzBrMRDV I get a load exception it says Root is already specified. 这是fxml http://pastebin.com/uzBrMRDV我得到一个加载异常,它说已经指定了Root。 If i remove the setRoot(this); 如果我删除setRoot(this); it doesnt load at all I am getting really frustrated with JFX... Is there anyway to load FXML files like a Stage from the controller itself 它根本不会加载我对JFX感到非常沮丧...无论如何,是否有从控制器本身加载FXML文件(如舞台)的信息

Remove the line 删除线

fxmlLoader.setRoot(this);

Your FXML defines the root to be an AnchorPane (and you can't set the root twice, which is why you are getting the error). 您的FXML将根定义为AnchorPane (并且不能两次设置根,这就是得到错误的原因)。

Since the current class is a Stage , and the FXMLLoader loads an AnchorPane , you need to put the loaded AnchorPane in a Scene and set the scene in the stage. 由于当前类是Stage ,并且FXMLLoader加载AnchorPane ,因此您需要将加载的AnchorPaneScene并在舞台中设置场景。 Replace 更换

fxmlLoader.load();

with

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