创建PieChart,meaby Array声明不起作用

时间:2018-07-19 09:57:40

标签: java arrays javafx pie-chart

我正在尝试制作一个用于生成PieChart的程序。在the first Window 您可以输入名称(X)和百分比(Y)。当您按插入时,此信息将被写入两个数组中,一个用于X(XI),第二个用于Y(YI)。 当您按停止时,程序会将这些信息写入可观察的列表中。 但这是行不通的,我认为问题出在数组的声明上。但是不知道具体的问题有人可以帮助我吗?

MainClass.java:

package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainClass extends Application {
  @Override
  public void start(Stage primaryStage) throws Exception {
    primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("FirstPage.fxml"))));
    primaryStage.show();}}

Controller.java:

package application;
import java.io.IOException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class Controller{

    @FXML
    private TextField XInput;
    @FXML
    private TextField YInput;
    @FXML
    private PieChart PieChart;
    public String[] XI; // Array for X
    public int [] YI; // Array for Y
    public int atm = -1;

    @FXML
    void GetValue(ActionEvent event) { // Write in Arrays
        atm++;
        XI[atm] = XInput.getText();
        YI[atm] = Integer.parseInt(YInput.getText());
    }

    @FXML
    void Stop(ActionEvent event) throws IOException {
        // Load new Stage
        Stage primaryStage = new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader();
        Parent root = fxmlLoader.load(getClass().getResource("SecondPage").openStream());
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();

        // Create ObservableList
        ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
        int i = 0;
        while(i == atm){
            pieChartData.add(new PieChart.Data(XI[i], YI[i]));
            i++;
        }

        // Set PieChart
        PieChart.setData(pieChartData);
    }
}

FirstPage.fxml:

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

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

<AnchorPane id="AnchorPane" prefHeight="410.0" prefWidth="592.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
   <children>
      <Button layoutX="108.0" layoutY="235.0" mnemonicParsing="false" onAction="#GetValue" prefHeight="40.0" prefWidth="376.0" style="-fx-background-color: #FFF; -fx-border-color: #000;" text="Insert" />
      <TextField fx:id="YInput" alignment="CENTER" layoutX="108.0" layoutY="154.0" prefHeight="72.0" prefWidth="376.0" promptText="Y" style="-fx-background-color: #FFF; -fx-border-color: #000;">
         <font>
            <Font size="30.0" />
         </font>
      </TextField>
      <TextField fx:id="XInput" alignment="CENTER" layoutX="108.0" layoutY="74.0" prefHeight="72.0" prefWidth="376.0" promptText="X" style="-fx-background-color: #FFF; -fx-border-color: #000;">
         <font>
            <Font size="30.0" />
         </font>
      </TextField>
      <Button layoutX="108.0" layoutY="282.0" mnemonicParsing="false" onAction="#Stop" prefHeight="40.0" prefWidth="376.0" style="-fx-background-color: #FFF; -fx-border-color: #000;" text="Stop" />
   </children>
</AnchorPane>

第二页:

<?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="571.0" prefWidth="744.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <PieChart fx:id="PieChart" layoutY="7.0" prefHeight="564.0" prefWidth="744.0" />
   </children>
</AnchorPane>

0 个答案:

没有答案