2015-02-07 68 views
1

這是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文件,如從控制器本身

回答

5

一個舞臺中刪除線

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

相關問題