Java 创建PieChart、meaby数组声明don';行不通
我正在尝试制作一个程序来生成一个饼图。在 你可以写一个名字(X)和百分比(Y)。按Insert键时,此信息将写入两个数组,一个用于X(XI),另一个用于Y(YI)。 当您按下停止键时,程序会将此信息写入可观察列表中。 但它不起作用,我认为问题在于数组的声明。但是不知道具体的问题谁能帮我 MainClass.java:Java 创建PieChart、meaby数组声明don';行不通,java,arrays,javafx,pie-chart,Java,Arrays,Javafx,Pie Chart,我正在尝试制作一个程序来生成一个饼图。在 你可以写一个名字(X)和百分比(Y)。按Insert键时,此信息将写入两个数组,一个用于X(XI),另一个用于Y(YI)。 当您按下停止键时,程序会将此信息写入可观察列表中。 但它不起作用,我认为问题在于数组的声明。但是不知道具体的问题谁能帮我 MainClass.java: package application; import javafx.application.Application; import javafx.fxml.FXMLLoader;
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);
}
}
包应用;
导入java.io.IOException;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.fxml.fxml;
导入javafx.fxml.fxmloader;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.chart.PieChart;
导入javafx.scene.control.TextField;
导入javafx.stage.stage;
公共类控制器{
@FXML
私有文本字段XInput;
@FXML
私有文本字段输入;
@FXML
私人海图;
公共字符串[]席;//数组为X
public int[]YI;//Y的数组
公共int atm=-1;
@FXML
void GetValue(ActionEvent事件){//写入数组
atm++;
XI[atm]=XInput.getText();
YI[atm]=Integer.parseInt(YInput.getText());
}
@FXML
无效停止(ActionEvent事件)引发IOException{
//加载新阶段
阶段primaryStage=新阶段();
FXMLLoader FXMLLoader=新的FXMLLoader();
父根=fxmlLoader.load(getClass().getResource(“SecondPage”).openStream());
场景=新场景(根);
初级阶段。场景(场景);
primaryStage.show();
//创建可观察列表
ObservableList pieChartData=FXCollections.observableArrayList();
int i=0;
而(i==atm){
添加(新的PieChart.Data(XI[i],YI[i]);
i++;
}
//集合图表
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>
>“不工作”是有帮助的,就像我们用“修复它”那样回答。无关:请学习java命名约定并坚持它们<代码>公共字符串[]席;X public int[]YI的数组;//Y的数组
我看不到您在哪里初始化数组
。您可能应该将数组
替换为列表
。