2017-07-26 77 views
2

這段代碼不會允許在我的窗口中畫線......我在fxml文件中的所有內容都是一個簡單的窗格,其中包含用於測試的hi的fx:id。沒有錯誤,線路根本不會出現。我也用盒子和圓圈試過這個。我真的需要幫助,這是一個重要的項目。pane.getChildren()。addAll();不在場景中工作javafx

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.Pane; 
import javafx.scene.shape.Line; 
import javafx.scene.Scene; 
import javafx.scene.paint.Color; 

public class PlotSceneController implements Initializable { 

    @FXML 
    Pane hi; 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 

    Line line = new Line(0,0,10,110); 
     line.setStroke(Color.BLACK); 
     line.setStrokeWidth(10); 
     hi.getChildren().addAll(line); 

    } 

} 

FXML文件

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.shape.*?> 
<?import java.lang.*?> 
<?import java.net.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 


<Pane fx:id="hi" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- 
Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" 
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> 
<children> 

</children> 
</Pane> 

主類,導致了另一個頁面的按鈕,導致我遇到問題的頁面。

public class Main extends Application { 

Stage firstStage; 
Scene loginButton; 

@Override 
public void start(Stage primaryStage) throws Exception {    

    Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); 
    firstStage = primaryStage;     
    loginButton = new Scene(root, 900, 700);  

    primaryStage.setTitle("Treatment Data");    
    primaryStage.setScene(loginButton);   
    primaryStage.show();       
} 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) {  //Main class 
    launch(args);      //Launches application/window 
} 

}

+0

郵政應用程序的其餘部分(中FXML文件和應用程序類)。當你運行它時,你有任何錯誤嗎? –

+0

@James_D更新它以包含更多關於我的問題和更多代碼的信息。 –

回答

5

你錯過了設置控制器類PlotSceneController.java。以不同的兩種方式設置控制器類,如使用主類setController()方法或在場景構建器屏幕左下方控制器窗格中設置控制器類。

使用主要

FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml")); 
loader.setController(new PlotSceneController()); 
Parent root = (Parent) loader.load(); 

或使用FXML

設置控制器類全包路徑類似下面的方式

enter image description here

+0

這太簡單了,我簡直不敢相信我錯過了。非常感謝! –