【问题标题】:how to put JavaFX in JPanel?如何将 JavaFX 放入 JPanel?
【发布时间】:2014-06-19 09:40:16
【问题描述】:

我有一个摇摆程序,我正在尝试在我的程序中加入一个 JavaFX 条形图。

如何将其放入JPanel

有没有办法将此代码放在ActionListerner 中?所以我可以在按下按钮后运行它。

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();
}

【问题讨论】:

  • 你如何调用任何方法?
  • YourClass.start(someStageClassInstance);.
  • 使用 classname.methodname()
  • 我不清楚,你在问什么,你想从 getDrawButton 内部调用 start 吗?
  • 是的,这是正确的 skeen

标签: java swing jpanel javafx-2 actionlistener


【解决方案1】:

使用JFXPanel

来自文档 =>

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 甚至有一个关于您所要求的案例的教程:

一般来说,我建议只使用 JavaFX(或仅使用 Swing)而不是混合使用 JavaFX 和 Swing,除非您有充分的理由这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多