SWT和Processing的结合

本文提供了一种将SWT与Processing结合的适配器模式方法,通过使用albireo库中的虚拟类SwingControl,实现SWT与Swing组件的无缝集成。具体操作包括在构造函数中重载函数,使用createSwingComponent来返回JComponet组件,以及在getLayoutAncestor中返回SWT组件。示例代码展示了如何在createSwingComponent中创建PApplet并嵌入到JComponent中,最终实现SWT与Swing组件的完美融合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上提供的解决方案不给力啊,通过不断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中。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值