为什么我的JavaFX阶段不希望加载

问题描述:

这是java为什么我的JavaFX阶段不希望加载

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 我得到一个负载例外,它说的根已经被指定。 如果我删除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); 
+0

我爱你的男人,谢谢! – 2015-02-08 00:55:36

+0

非常感谢! – swapyonubuntu 2015-08-13 16:07:22