作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个“气象站”应用程序,并且正在使用 JavaFX 来绘制流图。我想知道是否可以将图形与我希望显示的其他组件一起放置在 JFrame 中,如果可以,如何放置。
我想要在 JFrame
中的类
public class JavaFXApplication6 extends Application
{
private XYChart.Series<Number, Number> hourDataSeries;
private XYChart.Series<Number, Number> minuteDataSeries;
private NumberAxis xAxis;
private Timeline animation;
// more variables handling data manipulation based on time passing
private void init(Stage primaryStage)
{
Group root = new Group();
primaryStage.setScene(new Scene(root));
root.getChildren().add(createChart());
// create timeline to add new data every 60th of second
animation = new Timeline();
animation.getKeyFrames().add(new KeyFrame(Duration.millis(1000 / 60),
new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent actionEvent)
{
// 6 minutes data per frame
for (int count = 0; count < 6; count++)
{
nextTime();
plotTime();
}
}
}));
animation.setCycleCount(Animation.INDEFINITE);
}
protected LineChart<Number, Number> createChart()
{
// Code to setup chart and starting data and return it - substitute
// with:
return new LineChart(new NumberAxis(0, 24, 3), new NumberAxis(0, 100, 10));
}
private void nextTime()
{
// Code to advance time variables
}
private void plotTime() {
// update data series' based on time passing
}
public void play()
{
animation.play();
}
@Override
public void stop()
{
animation.pause();
}
@Override
public void start(Stage primaryStage) throws Exception
{
init(primaryStage);
primaryStage.show();
play();
}
public static void main(String[] args)
{
launch(args);
}
}
GUI 类是标准 swing JFrame
最佳答案
您可以通过将 Scene
放置在 JFXPanel
中来将其包含在 Swing 应用程序中。 This tutorial有更多详细信息。
您不能将 Application
子类直接嵌入到 Swing 应用程序中; Application
子类是启动类;它代表实际的应用程序,而不是 UI 元素。因此,您需要稍微重构您的 JavaFX 代码才能使其正常工作。
具体来说,我会定义一个类,例如:
public class AnimatedChart
{
private XYChart.Series<Number, Number> hourDataSeries;
private XYChart.Series<Number, Number> minuteDataSeries;
private NumberAxis xAxis;
private Timeline animation;
private Group view ;
// more variables handling data manipulation based on time passing
public AnimatedChart()
{
view = new Group();
primaryStage.setScene(new Scene(view));
view.getChildren().add(createChart());
// create timeline to add new data every 60th of second
animation = new Timeline();
animation.getKeyFrames().add(new KeyFrame(Duration.millis(1000 / 60),
new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent actionEvent)
{
// 6 minutes data per frame
for (int count = 0; count < 6; count++)
{
nextTime();
plotTime();
}
}
}));
animation.setCycleCount(Animation.INDEFINITE);
}
public Parent getView() {
return view ;
}
protected LineChart<Number, Number> createChart()
{
// Code to setup chart and starting data and return it - substitute
// with:
return new LineChart(new NumberAxis(0, 24, 3), new NumberAxis(0, 100, 10));
}
private void nextTime()
{
// Code to advance time variables
}
private void plotTime() {
// update data series' based on time passing
}
public void play()
{
animation.play();
}
public void pause() {
animation.pause();
}
}
然后你可以定义一个Application
类:
public void AnimatedChartApp extends Application {
private AnimatedChart animatedChart ;
@Override
public void start(Stage primaryStage) {
animatedChart = new AnimatedChart();
Scene scene = new Scene(animatedChart.getView());
primaryStage.setScene(scene);
animatedChart.play();
primaryStage.show();
}
@Override
public void stop() {
animatedChart.stop();
}
}
或者您可以在 Swing 应用程序中使用它:
JFrame frame = new JFrame();
JFXPanel jfxPanel = new JFXPanel();
frame.add(jfxPanel);
frame.setVisible(true);
Platform.runLater(() -> {
AnimatedChart animatedChart = new AnimatedChart();
Scene scene = new Scene(animatedChart.getView());
jfxPanel.setScene(scene);
animatedChart.play();
});
关于java - 我可以在 JFrame 中嵌入 JavaFX 应用程序类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28900879/
我是一名优秀的程序员,十分优秀!