java panel中放panel,如何将JavaFX放在JPanel中?

I have a swing program and i am trying to incorporate a JavaFX bar graph in my program.

How can I put it in a JPanel?

or

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 bc =

new BarChart(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();

}

解决方案

From the documentation =>

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 even have a tutorial on exactly the case you are asking:

nTqVM.jpg

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值