我有一个扩展javax.swing.JFrame的swing应用程序。 我使用NetBeanse GUI构建器构建了它。 我想使用JavaFX为这个程序添加一个条形图,所以我从Oracle获得了一个示例代码,并根据我的要求更改了它,Oracle示例(http://docs.oracle.com/javafx/2/swing/swing-fx-interoperability.htm#CHDIEEJE)。 问题是,当我运行这个类时,它会显示框架并且jPanel在那里,但由于某种原因它没有显示JavaFX面板。
我的代码:
public class Guage extends javax.swing.JFrame {
/**
* Creates new form Guage
*/
public Guage() {
initComponents();
}
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);
XYChart.Series TotallyPositive = new XYChart.Series();
XYChart.Series TotallyNegative = new XYChart.Series();
XYChart.Series MixedPositive = new XYChart.Series();
XYChart.Series MixedNegative = new XYChart.Series();
XYChart.Series MixedNeutral = new XYChart.Series();
XYChart.Series Neutral = new XYChart.Series();
allSeries.add(TotallyPositive);
allSeries.add(TotallyNegative);
allSeries.add(MixedPositive);
allSeries.add(MixedNegative);
allSeries.add(MixedNeutral);
allSeries.add(Neutral);
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String,Number> bc =
new BarChart<>(xAxis,yAxis);
bc.setTitle("Summary");
xAxis.setLabel("Tweets");
yAxis.setLabel("Value");
bc.setAnimated(false);
TotallyPositive.setName("Totally Positive");
TotallyNegative.setName("Totally Negative");
MixedPositive.setName("Mixed Positive");
MixedNegative.setName("Mixed Negative");
MixedNeutral.setName("Mixed Neutral");
Neutral.setName("Neutral");
TotallyPositive.getData().add(new XYChart.Data("Totally Positive",
KacstV2.TP));
TotallyNegative.getData().add(new XYChart.Data("Totally Negative",
KacstV2.TN));
MixedPositive.getData().add(new XYChart.Data("Mixed Positive",
KacstV2.MP));
MixedNegative.getData().add(new XYChart.Data("Mixed Negative",
KacstV2.MN));
MixedNeutral.getData().add(new XYChart.Data("Mixed Neutral",
KacstV2.MNeutral));
Neutral.getData().add(new XYChart.Data("Neutral", KacstV2.N));
bc.getData().addAll(TotallyPositive,TotallyNegative,MixedPositive,MixedNegative,MixedNeutral,Neutral);
root.getChildren().add(bc);
return (scene);
}
private static void initAndShowGUI() {
// This method is invoked on the EDT thread
final JFXPanel fxPanel = new JFXPanel();
new Guage().jPanel1.add(fxPanel);
Platform.runLater(new Runnable() {
@Override
public void run() {
initFX(fxPanel);
}
});
}
以下是我在main方法中运行代码的方法:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Guage().setVisible(true);
initAndShowGUI();
}
});
任何帮助将不胜感激。