gpt4 book ai didi

java - AWT 面板未在 JFX 中渲染

转载 作者:行者123 更新时间:2023-12-01 22:22:50 26 4
gpt4 key购买 nike

我想将 java.awt.Panel 添加到我的 JavaFX8 应用程序中。不幸的是,当附加到 SwingNode 时,面板似乎不会被渲染。

我有一个简单的测试应用程序:

import java.awt.Dimension;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class AWTInJFX extends Application {

@Override
public void start(Stage stage) {

final AwtInitializerTask awtInitializerTask = new AwtInitializerTask(() -> {
AWTPanel panel = new AWTPanel();
return panel;
});

SwingNode swingNode = new SwingNode();

SwingUtilities.invokeLater(awtInitializerTask);
try {
swingNode.setContent(awtInitializerTask.get());
} catch (InterruptedException | ExecutionException ex) {
Logger.getLogger(AWTInJFX.class.getName()).log(Level.SEVERE, null, ex);
}

stage.setScene(new Scene(new Group(swingNode), 600, 600));
stage.setResizable(false);
stage.show();

SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
frame.setSize(new Dimension(600, 400));
frame.add(new AWTPanel());
frame.setVisible(true);
});
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

private class AwtInitializerTask extends FutureTask<JPanel> {
public AwtInitializerTask(Callable<JPanel> callable) {
super(callable);
}
}

}

我的 JPanel 包含 java.awt.Panel

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Panel;
import javax.swing.JPanel;

public class AWTPanel extends JPanel{
public AWTPanel()
{
Dimension d = new Dimension(600, 400);
setPreferredSize(d);
Panel p = new Panel();
p.setSize(d);
p.setPreferredSize(d);
p.setBackground(Color.red);
this.add(p);
this.setBackground(Color.green);
}
}

当我将 AWTPanel 添加到 SwingNode 时,其他 AWT 组件也不会显示。

有人可以解释一下为什么这不起作用吗?

我需要一个 AWT 面板来获取在附加 C++ 库中使用的 hWnd。

最佳答案

来自 SwingNode Javadocs :

The hierarchy of components contained in the JComponent instance should not contain any heavyweight components, otherwise SwingNode may fail to paint it.

据我所知,没有办法在JavaFX中嵌入重量级组件,例如AWT组件。

根据您的要求,您可以考虑扭转局面,以便将 Swing/AWT 框架作为主窗口,并将应用程序的 JavaFX 部分嵌入 JFXPanel 中。 .

关于java - AWT 面板未在 JFX 中渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29390902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com