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

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

问题描述

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



例如



自定义JavaFX标签:

 公共类CustomJavaFXLabel扩展Label {
public CustomJavaFXLabel(){
super( );
setFont(blah blah blah);
setText(blah blah blah);
/ *
*依此类推...
* /
}
}

摇摆中的现有JPanel

 公共类SwingApp(){
私人JPanel jpanel;

public SwingApp(){
jpanel = new JPanel();
jpanel.add(new CustomJavaFXLabel()); //此行不起作用
}
}

我得到的错误是:

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

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



谢谢。

解决方案

  import java.awt 。尺寸; 
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
* /
公共类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);
场景场景=新场景(根);
jfxPanel.setScene(场景);
});
}

public static void main(String [] args){
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的标签嵌入到JPanel中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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