2015-04-02 171 views
1

我試圖重寫和簡化圖表的oracle教程,以便您擁有控制器類並使用fxml。 由於某種原因,它無法正常工作。JavaFX中的顯示圖表

這裏是我的代碼: 主類:

public class BarChartSample extends Application { 

    Stage primaryStage; 

    public void start(Stage primaryStage) { 
     this.primaryStage = primaryStage; 
     primaryStage.setTitle("Bar Chart Sample"); 

     showPage(); 
    } 

    public void showPage(){ 
     try { 
      FXMLLoader loader = new FXMLLoader(); 
     loader.setLocation(BarChartSample.class.getResource("ChartPage.fxml")); 
      AnchorPane pane = loader.load(); 

      Scene scene = new Scene(pane); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

控制器:

public class Controller { 
    final static String performance = "Performance:"; 

    @FXML 
    NumberAxis xAxis = new NumberAxis(); 
    @FXML 
    CategoryAxis yAxis = new CategoryAxis(); 

    @FXML 
    BarChart<Number,String> bc; 

    public void initialize(){ 
     xAxis.setLabel("Percent"); 
     xAxis.setTickLabelRotation(90); 
     yAxis.setLabel("Performance"); 

     bc = new BarChart<Number,String>(xAxis,yAxis); 

     XYChart.Series series1 = new XYChart.Series();  
     series1.getData().add(new XYChart.Data(80, performance)); 
     bc.getData().add(series1); 
    } 
} 

FXML:

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

<?import javafx.scene.control.*?> 
<?import javafx.scene.chart.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.layout.AnchorPane?> 

<AnchorPane prefHeight="393.0" prefWidth="419.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller"> 
    <children> 
     <BarChart fx:id="bc" layoutX="-40.0" layoutY="-3.0" prefHeight="205.0" prefWidth="409.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0"> 
     <xAxis> 
      <CategoryAxis fx:id="yAxis" side="BOTTOM" /> 
     </xAxis> 
     <yAxis> 
      <NumberAxis side="LEFT" fx:id="xAxis" /> 
     </yAxis> 
     </BarChart> 
    <TextArea layoutX="103.0" layoutY="14.0" maxHeight="100.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.leftAnchor="21.0"AnchorPane.rightAnchor="21.0" AnchorPane.topAnchor="5.0" /> 
     </children> 
     </AnchorPane> 

將不能正常顯示圖表,它只是畫幾行。爲什麼?謝謝!

回答

2

initialize()

bc = new BarChart<Number,String>(xAxis,yAxis); 

你不應該重新初始化它已經在FXML定義,並已通過@FXML註釋集成在控制器的任何控制刪除此行。

您還需要與

@FXML 
NumberAxis xAxis; 
@FXML 
CategoryAxis yAxis; 

替換下列行的原因說

@FXML 
NumberAxis xAxis = new NumberAxis(); 
@FXML 
CategoryAxis yAxis = new CategoryAxis(); 

同樣,你需要正確定義BARCHART。在FXML中,您宣稱xAxisCategoryAxisyAxisNumberAxis。但是在控制器中你定義了一個BarChart<Number,String>

public class Controller { 
    final static String performance = "Performance:"; 

    @FXML 
    NumberAxis xAxis; 
    @FXML 
    CategoryAxis yAxis; 

    @FXML 
    BarChart<String, Number> bc; 

    public void initialize(){ 
     xAxis.setLabel("Percent"); 
     xAxis.setTickLabelRotation(90); 
     yAxis.setLabel("Performance"); 

     XYChart.Series<String, Number> series1 = new XYChart.Series(); 
     series1.getData().add(new XYChart.Data(performance, 80)); 
     bc.getData().add(series1); 
    } 
} 
+0

我得到一個鑄件例外,但我不明白爲什麼...... java.lang.Integer中不能轉換爲java.lang.String – user3435407 2015-04-02 22:43:34

+0

是的,我明白了。這是因爲在FXML中,你已經聲明xAxis是一個'CategoryAxis'和'yAxis'是一個數字軸。但是在控制器中你已經定義了一個BarChart '。請檢查更新後的答案。 – ItachiUchiha 2015-04-02 22:49:41

+0

pffff ..謝謝,我誤解了這裏的一些行爲.... – user3435407 2015-04-02 23:29:07