网上提供的解决方案不给力啊,通过不断Google,自己总结的比较靠谱的方法。
思想:SWT与Processing结合貌似没看到完美的解决方案,SWT-AWT的解决方案貌似也不给力,对Processing支持很不友好。
那就来个折中,美其名曰适配器模式:),将传统的Swing嵌入两者之间,得到想要的效果。
涉及到:
- albireo的库。
解决方案:
使用albireo库中的虚拟类SwingControl,其构造函数为SwingControl(Composite parent, int style),在该类中,重载函数以下两个函数,使用createSwingComponent来返回某一JComponet组件;使用getLayoutAncestor来返回传入的SWT组件(可以为Shell或者普通的Composite);示例代码如下:
SwingControl control = new SwingControl(shell, SWT.NONE) {
@Override
protected JComponent createSwingComponent() {
return createJComponent();
}
@Override
public Composite getLayoutAncestor() {
return shell;
}
};
以上实现了SWT跟Swing组件的结合。
接下来可以在createSwingComponent()函数中,创建PApplet并嵌入到一个JComponent中,最后返回该JComponent组件,如下所示:
private JComponent createJComponent (){
//返回JInternalFrame这个JComponent
JInternalFrame treemapFrame = new JInternalFrame("TreeMap", true, true, true, true);
treemapFrame.setVisible(true);
treemapFrame.setResizable(false);
treemapFrame.setContentPane(pApplet);
//JInternalFrame的样式控制,取消边框,标题栏
((BasicInternalFrameUI)treemapFrame.getUI()).setNorthPane(null);
treemapFrame.setBorder(BorderFactory.createEmptyBorder());
return treemapFrame;
}
这个例子使用的是JInternalFrame,在JInternalFrame中嵌入了PApplet,并将JInternalFrame嵌入到SWT的一个Shell中,另外,设置了JInternalFrame的UI属性,使得JInternalFrame完美嵌入到Shell中。