在下面给出的代码中,我想在点击提交按钮时显示饼图。它没有显示任何错误,但甚至没有显示饼图。我已经静态地为我的fxml代码添加了一个条形图,并在点击按钮后在饼图中显示详细信息。
这是我的java代码
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.Pane;
import javax.naming.ldap.InitialLdapContext;
public class Operations implements Initializable{
public Button buttonsubmit;
public PieChart piechart;
public Pane pane1,pane2;
public BarChart barchart;
public ComboBox combobox;
Connection con;
public void combo(ActionEvent e)
{
}
@FXML
private void handleOnPieChartAction()
{
try
{
System.out.println("You clicked the Pie Chart!");
piechart=new PieChart();
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Negative", 45),
new PieChart.Data("Positive", 55));
piechart.setData(pieChartData);
System.out.println("You clicked the Pie Chart2!");
piechart.setVisible(true);
}
catch (Exception e1)
{
// TODO: handle exception
}
}
@Override
public void initialize(URL location, ResourceBundle resources)
{
// TODO Auto-generated method stub
buttonsubmit.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event) {
// TODO Auto-generated method stub
handleOnPieChartAction();
}
});
}
}
我的FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.BarChart?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.chart.PieChart?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<VBox prefHeight="900.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Operations">
<children>
<MenuBar VBox.vgrow="NEVER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New" />
<MenuItem mnemonicParsing="false" text="Open…" />
<Menu mnemonicParsing="false" text="Open Recent" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" text="Save" />
<MenuItem mnemonicParsing="false" text="Save As…" />
<MenuItem mnemonicParsing="false" text="Revert" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Preferences…" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Undo" />
<MenuItem mnemonicParsing="false" text="Redo" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Cut" />
<MenuItem mnemonicParsing="false" text="Copy" />
<MenuItem mnemonicParsing="false" text="Paste" />
<MenuItem mnemonicParsing="false" text="Delete" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Select All" />
<MenuItem mnemonicParsing="false" text="Unselect All" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About MyHelloApp" />
</items>
</Menu>
</menus>
</MenuBar>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<Pane layoutY="4.0" prefHeight="227.0" prefWidth="200.0">
<children>
<ComboBox fx:id="combobox" layoutX="23.0" layoutY="23.0" onAction="#combo" prefWidth="150.0" />
</children>
</Pane>
<Pane layoutY="205.0" prefHeight="200.0" prefWidth="200.0">
<children>
<Button fx:id="buttonsubmit" layoutX="58.0" layoutY="100.0" mnemonicParsing="false" text="submit" />
<TextField layoutX="15.0" layoutY="44.0" />
</children>
</Pane>
<Pane fx:id="pane1" layoutX="345.0" layoutY="22.0" prefHeight="302.0" prefWidth="342.0">
<children>
<BarChart fx:id="barchart" layoutX="2.0" layoutY="-1.0" prefHeight="315.0" prefWidth="342.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</BarChart>
</children></Pane>
<Pane fx:id="pane2" layoutX="354.0" layoutY="373.0" prefHeight="315.0" prefWidth="342.0">
<children>
<PieChart fx:id="piechart" layoutX="34.0" layoutY="29.0" prefHeight="221.0" prefWidth="259.0" />
</children></Pane>
</children>
</AnchorPane>
</children>
</VBox>
饼图显示什么.. 知道为什么它不起作用......