SWT相关操作_Util

字符串相关操作

  • 判断字符串是否为空
  • Object对象转字符串

窗口最大化

shell.setMaximized(true);

窗口居中

public static void centerWindow(Shell shell){
	Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
	shell.setLocation((ds.width-shell.getSize().x)/2,(ds.height-shell.getSize().y)/2);
}

弹窗提示

public static void showMessage(Shell shell,String s1,String s2){
	MessageBox mb = new MessageBox(shell,SWT.NONE);
	mb.setText(s1);
	mb.setMessage(s2);
	mb.open();
}

图片自适应

public static void selfAdaption(PaintEvent e,Label label_image){
	Image image = label_image.getImage();
	int h = label_image.getBounds().height;
	int w = label_image.getBounds().width;
	int height = image.getBounds().height;
	int width = image.getBounds().width;
	e.gc.drawImage(image, 0, 0, width, height, 0, 0, w, h);
}
public static void imageSize(Label label){
		//获取 Label组件图片
		Image image=label.getImage();
		if(null==image){
			return ;
		}
		//监控添加重绘监听器
		label.addPaintListener(new PaintListener(){
			public void paintControl(PaintEvent e){
				//获取label组件
				Point size=label.getSize();
				//绘制图片
				e.gc.drawImage(image,0,0,image.getBounds().width,image.getBounds().height,0,0,size.x,size.y);
			}
		});
	}

图片文件file转为byte[]

File file=new File(filepath);//filepath 图片的绝对路径
InputStream in=new FileInputStream(file);//输入file文件
byte[] images=new byte[(int) file.length()];
in.read(images);

上传图片到Lable框中

label_image.addMouseListener(new MouseAdapter() {
	@Override
	public void mouseDoubleClick(MouseEvent e) {
		//创建文件对话框
		FileDialog fd = new FileDialog(shell);
		fd.setFilterPath("E:\\");
		//设置选择文件类型
		fd.setFilterExtensions(new String[]{"*.jpg","*.png","*.gif","*.*"});
		filepath=fd.open();//filepath全局变量,接收返回图片的绝对路径  
		if(null==filepath||"".equals(filepath)){
			/*弹框提示   函数代码在上面哦
			MessageDialog.openError(shell, "警告⚠", "未选中图片");
		    return;*/
		    tishi_image.setText("未选择中图片");
			return ;
		}
		try {
			//将选中图片设置到label中
			File file = new File(filepath);
			InputStream in = new FileInputStream(file);
			
			Image image = new Image(Display.getDefault(),in);
			label_image.setImage(image);
			tishi_image.setText("图片上传成功");
			YcUtil.imageSize(label_image);//图片自适应
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值