- 判断字符串是否为空
- 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();
}
}
});