【发布时间】:2015-06-06 02:33:28
【问题描述】:
我想在我的 JavaFX8 应用程序中添加一个java.awt.Panel。不幸的是,当附加到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。
【问题讨论】:
标签: java swing awt javafx-8 hwnd