我有一个我创建的java swing项目,基本上我将使用JavaFX来简化饼图的创建,以便为用户显示一些数据。我已经通过扩展JFXPanel然后启动并将其添加到swing JFrame中来创建我希望用来实现此目的的类
JFXPanel piePanel = new PieChartSample(test);
contentPane.add(piePanel);
piePanel.setVisible(true);
在这种情况下,test是一个整数的任意数组。
我怀疑问题不是在这里,而是在PieChartSample代码中,虽然这很长并且不太清楚。
public class PieChartSample extends JFXPanel{
ArrayList<Integer> vals;
public PieChartSample(ArrayList<Integer> vals) {
this.vals = vals;
}
private static void initAndShowGUI() {
// This method is invoked on the EDT thread
JPanel frame = new JPanel();
final JFXPanel fxPanel = new JFXPanel();
frame.getContentPane().add(fxPanel);
frame.setSize(500, 500);
frame.setVisible(true);
Platform.runLater(new Runnable() {
@Override
public void run() {
initFX(fxPanel);
}
});
}
private static void initFX(JFXPanel fxPanel) {
// This method is invoked on the JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
private static Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root, Color.ALICEBLUE);
Text text = new Text();
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("%0", 22),
new PieChart.Data("%20", 13),
new PieChart.Data("%40", 25),
new PieChart.Data("%60", 10),
new PieChart.Data("%80", 22),
new PieChart.Data("%100", 30));
final PieChart chart = new PieChart(pieChartData);
chart.setTitle("Imported Fruits");
((Group) scene.getRoot()).getChildren().add(chart);
root.getChildren().add(text);
return (scene);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAndShowGUI();
}
});
}
}
基本上,当我启动JFrame时,JFXPanel根本不显示。有什么想法吗?