如何将JavaFX放在JPanel中? [英] how to put JavaFX in JPanel?
本文介绍了如何将JavaFX放在JPanel中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个swing程序,并且我正在尝试将JavaFX条形图合并到我的程序中.
I have a swing program and i am trying to incorporate a JavaFX bar graph in my program.
如何将其放在JPanel
中?
或
有没有办法将此代码放在ActionListerner
中?这样我就可以在按下按钮后运行它.
Is there a way to put this code in a ActionListerner
? So I can run it after pressing a button.
public static void start(Stage stage) {
String judge1 = "Judge 1";
String judge2 = "Judge 2";
String judge3 = "Judge 3";
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String,Number> bc =
new BarChart<String,Number>(xAxis,yAxis);
xAxis.setLabel("Judges");
yAxis.setLabel("Run");
XYChart.Series series1 = new XYChart.Series();
series1.setName("Run 1");
series1.getData().add(new XYChart.Data(judge1, 1));
series1.getData().add(new XYChart.Data(judge2, 3));
series1.getData().add(new XYChart.Data(judge3, 2));
XYChart.Series series2 = new XYChart.Series();
series2.setName("Run 2");
series2.getData().add(new XYChart.Data(judge1, 5));
series2.getData().add(new XYChart.Data(judge2, 4);
series2.getData().add(new XYChart.Data(judge3, 4));
Scene scene = new Scene(bc,800,600);
bc.getData().addAll(series1, series2);
stage.setScene(scene);
stage.show();
}
推荐答案
使用从文档=>
public class Test {
private static void initAndShowGUI() {
// This method is invoked on Swing thread
JFrame frame = new JFrame("FX");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setVisible(true);
Platform.runLater(new Runnable() {
@Override
public void run() {
initFX(fxPanel);
}
});
}
private static void initFX(JFXPanel fxPanel) {
// This method is invoked on JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAndShowGUI();
}
});
}
}
Oracle甚至还提供了有关您所要求的具体情况的教程:
Oracle even have a tutorial on exactly the case you are asking:
通常,我建议您仅使用JavaFX(或仅使用Swing),而不建议混合使用JavaFX和Swing,除非您确实有充分的理由这样做.
In general, I'd advise just using JavaFX only (or Swing only) and not mixing JavaFX and Swing unless you have really good reasons to do so.
这篇关于如何将JavaFX放在JPanel中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文