JAVA 如何将JAVAFX 的标签嵌入到swing 中的JPanel 中? [英] JAVA How to embed JAVAFX's label into a JPanel in swing?

查看:158
本文介绍了JAVA 如何将JAVAFX 的标签嵌入到swing 中的JPanel 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将我使用 JAVAFX 创建的自定义标签嵌入到现有 Swing 的 JPanel 中?

例如

自定义 JavaFX 标签:

公共类 CustomJavaFXLabel 扩展标签{公共 CustomJavaFXLabel(){极好的();setFont("胡说八道");setText("胡说八道");/**等等...*/}}

现有的 JPanel 在摇摆中

公共类 SwingApp(){私人JPanel jpanel;公共 SwingApp(){jpanel = 新的 JPanel();jpanel.add(new CustomJavaFXLabel());//这条线不起作用}}

我得到的错误是:

Container类型中的add(Component)方法不适用于参数(CustomJavaFXLabel)

我知道为此,我最好使用 JLabel 来创建自定义标签.但是,由于某些限制,我需要为自定义标签使用 FX.

谢谢.

解决方案

如图

import java.awt.Dimension;导入 java.awt.EventQueue;导入 javafx.application.Platform;导入 javafx.embed.swing.JFXPanel;导入 javafx.geometry.Insets;导入javafx.scene.Scene;导入 javafx.scene.control.Label;导入javafx.scene.layout.Background;导入 javafx.scene.layout.BackgroundFill;导入 javafx.scene.layout.CornerRadii;导入 javafx.scene.layout.StackPane;导入 javafx.scene.paint.Color;导入 javax.swing.JFrame;/*** @参见 https://stackoverflow.com/q/41159015/230513*/公共类标签测试{私人无效显示(){JFrame f = new JFrame("LabelTest");f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JFXPanel jfxPanel = 新 JFXPanel() {@覆盖公共维度 getPreferredSize() {返回新维度(320、240);}};初始化JFXPanel(jfxPanel);f.add(jfxPanel);f.pack();f.setLocationRelativeTo(null);f.setVisible(true);}私人无效initJFXPanel(JFXPanel jfxPanel){Platform.runLater(() -> {标签标签 = 新标签(System.getProperty("os.name") + "v"+ System.getProperty("os.version") + "; Java v"+ System.getProperty("java.version"));label.setBackground(新背景(新背景填充(颜色.ALICEBLUE, CornerRadii.EMPTY, Insets.EMPTY)));StackPane root = new StackPane();root.getChildren().add(标签);场景场景 = 新场景(根);jfxPanel.setScene(场景);});}公共静态无效主要(字符串[]参数){EventQueue.invokeLater(new LabelTest()::display);}}

How can I embed a custom label I created using JAVAFX into an existing swing's JPanel?

E.g.

Custom JavaFX Label:

public class CustomJavaFXLabel extends Label{
    public CustomJavaFXLabel(){
        super();    
        setFont("blah blah blah");
        setText("blah blah blah");
          /*
           *and so on...
           */
    }
}

Existing JPanel in swing

public class SwingApp(){
    private JPanel jpanel;

    public SwingApp(){
        jpanel = new JPanel();
        jpanel.add(new CustomJavaFXLabel()); //this line does not work
    }
}

Error I got is:

The method add(Component) in the type Container is not applicable for argument (CustomJavaFXLabel)

I understand that to for this, I am better off using JLabel to create the custom label. However, due to certain constraints I was required to use FX for the custom Label.

Thanks.

解决方案

As shown in JavaFX: Interoperability, §3 Integrating JavaFX into Swing Applications, add your custom javafx.scene.control.Label to a javafx.embed.swing.JFXPanel. Because a JFXPanel is a java.awt.Container, it can be added to your Swing layout. Several examples may be found here and below.

import java.awt.Dimension;
import java.awt.EventQueue;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javax.swing.JFrame;
/**
 * @see https://stackoverflow.com/q/41159015/230513
 */
public class LabelTest {

    private void display() {
        JFrame f = new JFrame("LabelTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFXPanel jfxPanel = new JFXPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        };
        initJFXPanel(jfxPanel);
        f.add(jfxPanel);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private void initJFXPanel(JFXPanel jfxPanel) {
        Platform.runLater(() -> {
            Label label = new Label(
                System.getProperty("os.name") + " v"
                + System.getProperty("os.version") + "; Java v"
                + System.getProperty("java.version"));
            label.setBackground(new Background(new BackgroundFill(
                Color.ALICEBLUE, CornerRadii.EMPTY, Insets.EMPTY)));
            StackPane root = new StackPane();
            root.getChildren().add(label);
            Scene scene = new Scene(root);
            jfxPanel.setScene(scene);
        });
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new LabelTest()::display);
    }
}

这篇关于JAVA 如何将JAVAFX 的标签嵌入到swing 中的JPanel 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆