简体   繁体   English

JavaFX:为什么带有JComponent参数的SwingNode的setContent方法不能接受JPanel?

[英]JavaFX: Why doesn't the setContent method of SwingNode with a JComponent parameter accept a JPanel?

I have the following code: 我有以下代码:

private void launchSingleRecordWindow() {
    SwingNode swingNode = new SwingNode();
    MainStageController.setContent(new AnchorPane(swingNode));
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JPanel webcamPanel = new WebcamPanel(selectedWebcam);
            swingNode.setContent(webcamPanel);
        }
    });
}

But to my surprise the string swingNode.setContent(webcamPanel); 但令我惊讶的是,字符串swingNode.setContent(webcamPanel); isn't correct. 是不正确的。

IntelliJ IDEA says: IntelliJ IDEA说:

java: incompatible types: javax.swing.JPanel cannot be converted to javax.swing.JComponent . java: incompatible types: javax.swing.JPanel cannot be converted to javax.swing.JComponent

I've tried to make separate class, but it works only when it is a subclass of JComponent , but not for its subclasses, such as JFrame or JPanel . 我试图创建单独的类,但仅当它是JComponent的子类时才起作用,但不适用于其子类,例如JFrameJPanel What is wrong? 怎么了?

It's telling you that a JFrame is not a JComponent . 告诉您JFrame不是JComponent Guess what? 你猜怎么了? It's right. 这是正确的。

See for yourself: JFrame API 自己看看: JFrame API

JFrame不是JComponent子类,它是Component子类,因此无法将其转换为JComponent

The error is strange: 错误很奇怪:

java: incompatible types: javax.swing.JPanel cannot be converted to javax.swing.JComponent

Because ... A JPanel is a JComponent: 因为... JPanel是JComponent:

Class JPanel
java.lang.Object
   java.awt.Component
      java.awt.Container
         javax.swing.JComponent
            javax.swing.JPanel

And a SwingNode does accept a JComponent: 并且SwingNode确实接受JComponent:

public void setContent(javax.swing.JComponent content)

So ... it should work to pass a JPanel to a SwingNode::setContent 所以...它将JPanel传递给SwingNode :: setContent应该起作用

Check your code or compile in verbose mode and see if the error is what you think it is! 检查您的代码或以详细模式进行编译,看看错误是否与您想像的一样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM