拖动工具栏CoolBar和拖动工具项CoolItem

方法一:每次创建一个拖动工具项,都要创建一个工具栏。麻烦

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.*;
public class E{
	public static void main(String[] args){
		Display display=new Display();
		Shell shell=new Shell(display);
		shell.setText("拖动工具栏CoolBar和拖动工具项CoolItem");
		
		shell.setLayout(new GridLayout());
		
		//创建拖动工具栏coolBar
		CoolBar coolBar=new CoolBar(shell,SWT.FLAT);
	
		
		
		//创建拖动工具项coolItem
		CoolItem coolItem=new CoolItem(coolBar,SWT.None);
		
		//创建工具栏toolBar,在拖动工具栏中创建
		ToolBar toolBar=new ToolBar(coolBar,SWT.FLAT);
		//创建工具项ToolItem
		ToolItem fileToolItem=new ToolItem(toolBar,SWT.None);
		fileToolItem.setText("文件工具项");
		ToolItem createToolItem=new ToolItem(toolBar,SWT.None);
		createToolItem.setText("新建工具项");
		//toolBar.setSize(200,100);
		toolBar.pack();
		
		//获取ToolBar的大小,此处的getSize一定要在setControl的前面,否则容易出错
		Point toolBarPoint=toolBar.getSize();
		
		//将拖动工具项与工具栏绑定
		coolItem.setControl(toolBar);
		
		//根据ToolBar的大小,然后设定CoolItem的大小	
		Point preferredPoint=coolItem.computeSize(toolBarPoint.x+100, toolBarPoint.y);
		coolItem.setSize(preferredPoint);
		

		
		//创建拖动工具项coolItem2
		CoolItem coolItem2=new CoolItem(coolBar,SWT.None);
		
		//创建工具栏toolBar,在拖动工具栏中创建
		ToolBar toolBar2=new ToolBar(coolBar,SWT.FLAT);
		//创建工具项ToolItem
		ToolItem fileToolItem2=new ToolItem(toolBar2,SWT.None);
		fileToolItem2.setText("文件工具项");
		ToolItem createToolItem2=new ToolItem(toolBar2,SWT.None);
		createToolItem2.setText("新建工具项");
		//toolBar2.setSize(200,100);
		toolBar2.pack();
		
		//获取ToolBar的大小,此处的getSize一定要在setControl的前面,否则容易出错
		Point toolBarPoint2=toolBar2.getSize();
		
		//将拖动工具项与工具栏绑定
		coolItem2.setControl(toolBar2);
		
		//根据ToolBar的大小,然后设定CoolItem的大小	
		Point preferredPoint2=coolItem2.computeSize(toolBarPoint2.x+100, toolBarPoint2.y);
		//得到的尺寸是(x,y)//宽为x,高为y
		coolItem2.setSize(preferredPoint2);
			
		
		
		coolBar.pack();

		Text content=new Text(shell,SWT.MULTI);
		content.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		
		//打开窗口,进行窗口的显示
		//shell.setSize(600,400);
		shell.pack();
		shell.open();
		while(!shell.isDisposed()){
			//当窗口没有被释放的时候
			if(!display.readAndDispatch()){
				display.sleep();
			}		
		}
		display.dispose();
	}
}

方法二:将创建工具栏和创建拖动工具项各封装成一个函数。

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.*;
public class F{
	public static void main(String[] args){
		Display display=new Display();
		Shell shell=new Shell(display);
		shell.setText("拖动工具栏CoolBar和拖动工具项CoolItem");
		
		shell.setLayout(new GridLayout());
		
		//创建拖动工具栏CoolBar
		CoolBar coolBar=new CoolBar(shell,SWT.FLAT);
		
		//创建拖动工具项CoolItem多个
		createCoolItem(display,coolBar);
		createCoolItem(display,coolBar);
		createCoolItem(display,coolBar);
		
		coolBar.pack();

		Text content=new Text(shell,SWT.MULTI);
		content.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
	
		
		//打开窗口,进行窗口的显示
		//shell.setSize(400,400);
		shell.pack();
		shell.open();
		while(!shell.isDisposed()){
			//当窗口没有被释放的时候
			if(!display.readAndDispatch()){
				display.sleep();
			}		
		}
		display.dispose();
	}
	
	public static CoolItem  createCoolItem(Display display,CoolBar coolBar){
		//创建工具栏toolBar
		ToolBar toolBar=createToolBar(display,coolBar);
		//获得工具栏的大小,此处的getSize一定要在setControl的前面,否则直接出错
		Point toolBarPoint=toolBar.getSize();
		
		//创建拖动工具项coolItem
		CoolItem coolItem=new CoolItem(coolBar,SWT.None);//SWT.DROP_DOWN带下拉选项的
	
		//将拖动工具项与工具栏绑定
		coolItem.setControl(toolBar);
		
		//根据ToolBar的大小,然后设定CoolItem的大小	
		Point preferredPoint=coolItem.computeSize(toolBarPoint.x+100, toolBarPoint.y);
		coolItem.setSize(preferredPoint);
		
		return coolItem;
	}
	
	public static ToolBar  createToolBar(Display display,CoolBar coolBar){
		//创建工具栏toolBar,在拖动工具栏中创建
		ToolBar toolBar=new ToolBar(coolBar,SWT.FLAT);
		//创建工具项ToolItem
		ToolItem fileToolItem=new ToolItem(toolBar,SWT.NONE);
		fileToolItem.setText("文件工具项");
		ToolItem createToolItem=new ToolItem(toolBar,SWT.NONE);
		createToolItem.setText("新建工具项");
		toolBar.pack();//这句话我总是丢
		return toolBar;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值