【问题标题】:JAVA How to embed JAVAFX's label into a JPanel in swing?JAVA 如何将JAVAFX 的标签嵌入到swing 中的JPanel 中?
【发布时间】:2016-12-15 07:52:19
【问题描述】:

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

例如

自定义 JavaFX 标签:

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

现有的 JPanel 在摇摆中

public class SwingApp(){
    private JPanel jpanel;

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

我得到的错误是:

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

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

谢谢。

【问题讨论】:

    标签: java swing javafx jpanel label


    【解决方案1】:

    JavaFX: Interoperability, §3 Integrating JavaFX into Swing Applications 所示,将您的自定义javafx.scene.control.Label 添加到javafx.embed.swing.JFXPanel。因为JFXPanel java.awt.Container,所以它可以添加到您的 Swing 布局中。可以在here 及以下找到几个示例。

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

    【讨论】:

      猜你喜欢
      • 2021-10-08
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多