无法在swing应用程序中获取JavaFX帧
我创建了一个JavaSwing项目,基本上我将使用JavaFX来简化创建饼图的过程,以便为用户显示一些数据。我已经创建了一个类,我希望通过扩展JFXPanel,然后像这样启动并将其添加到swing JFrame中来实现这一点无法在swing应用程序中获取JavaFX帧,java,swing,javafx,Java,Swing,Javafx,我创建了一个JavaSwing项目,基本上我将使用JavaFX来简化创建饼图的过程,以便为用户显示一些数据。我已经创建了一个类,我希望通过扩展JFXPanel,然后像这样启动并将其添加到swing JFrame中来实现这一点 JFXPanel piePanel = new PieChartSample(test); contentPane.add(piePanel); piePanel.setVisible(true); 在本例中,test是int的任意数组 我怀疑问题不在这里,而是在Piec
JFXPanel piePanel = new PieChartSample(test);
contentPane.add(piePanel);
piePanel.setVisible(true);
在本例中,test是int的任意数组
我怀疑问题不在这里,而是在Piechart示例代码中,尽管这很长而且不太清楚
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();
}
});
}
}
public类PieChartSample扩展JFXPanel{
ArrayList VAL;
公共图表样本(ArrayList VAL){
this.vals=vals;
}
私有静态void initAndShowGUI(){
//此方法在EDT线程上调用
JPanel frame=新的JPanel();
最终JFXPanel fxPanel=新JFXPanel();
frame.getContentPane().add(fxPanel);
框架。设置尺寸(500500);
frame.setVisible(true);
Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
initFX(fxPanel);
}
});
}
私有静态void initFX(JFXPanel fxPanel){
//此方法在JavaFX线程上调用
Scene-Scene=createScene();
fxPanel.setScene(场景);
}
私有静态场景CreateSecene(){
组根=新组();
场景=新场景(根,Color.ALICEBLUE);
Text Text=新文本();
可观测数据=
FXCollections.observableArrayList(
新的图表数据(“%0”,22),
新的图表数据(“%20”,13),
新图表数据(“%40”,25),
新的图表数据(“%60”,10),
新的图表数据(“%80”,22),
新的图表数据(“%100”,30));
最终PieChart图表=新PieChart(PieChart数据);
图表.setTitle(“进口水果”);
((组)scene.getRoot()).getChildren().add(图表);
root.getChildren().add(文本);
返回(现场);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
initAndShowGUI();
}
});
}
}
基本上,当我启动JFrame时,JFXPanel根本不显示。有什么想法吗?我在你的代码中看到的唯一问题是
JPanel frame=newjpanel()代码>应该是JFrame=newjframe()代码>我在代码中看到的唯一问题是JPanel frame=newjpanel()代码>应该是JFrame=newjframe()代码>