为什么我的javafx阶段不想加载 [英] Why does my javafx stage not want to load

查看:142
本文介绍了为什么我的javafx阶段不想加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是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文件

here is the fxml http://pastebin.com/uzBrMRDV I get a load exception it says Root is already specified. If i remove the 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

推荐答案

删除行

fxmlLoader.setRoot(this);

您的FXML将根定义为 AnchorPane (并且您不能将根设置两次,这是为什么你得到错误)。

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).

由于当前类是 Stage ,而 FXMLLoader 加载 AnchorPane ,你需要将加载的 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. Replace

fxmlLoader.load();

with

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

这篇关于为什么我的javafx阶段不想加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆