点击按钮打开 FileDialog Box ,然后选择要在特定标签上显示的任何图片。
我尝试使用Label控件中的所选图像设置绝对路径或相对路径 但没有适当的动态工作。
所以请帮我解决问题。
答案 0 :(得分:3)
Java SWT加载并调整图像以动态查看或编辑
单击按钮打开FileDialog Box并选择要在特定标签上显示的任何图像。
ImageLoader 类用于从文件或流中加载图像并将图像保存到文件或流中
ImageData 类是与设备无关的图像描述
SWT的图像类可用于在GUI中显示图像
package rcp_demo.Editor;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
public class ProductEditor extends EditorPart {
public static final String ID="rcp_demo.Editor.product";
private Text text;
private CLabel lbl_image_text;
private static final String[] FILTER_NAMES = {
"Images(*.jpg)","Images(*.jpeg)","Images(*.png)","All Files (*.*)"};
// These filter extensions are used to filter which files are displayed.
private static final String[] FILTER_EXTS = { "*.jpg", "*.jpeg", "*.png", "*.*"};
public void createPartControl(final Composite parent) {
parent.setLayout(null);
//Layout with absolute positioning components.
text = new Text(parent, SWT.BORDER);
text.setBounds(25, 57, 169, 19);
Button btnOpen = new Button(parent, SWT.NONE);
btnOpen.setText("open");
btnOpen.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN);
dialog.setFilterNames(FILTER_NAMES);
dialog.setFilterExtensions(FILTER_EXTS);
String result = dialog.open();
if(result!=null)
{
text.setText(result);
Image image=SWTResourceManager.getImage(result);
ImageData imgData = image.getImageData();
imgData=imgData.scaledTo(200, 200);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] {imgData};
imageLoader.save(result, SWT.IMAGE_COPY);
System.out.println(imgData.width+"....."+imgData.height);
lbl_image_text.setBounds(25,88,imgData.width+10,imgData.height+10);
//Image size set to Label
//lbl_image_text.setBounds(25,88,image.getBounds().width+10,image.getBounds().height+10);
lbl_image_text.setImage(SWTResourceManager.getImage(result));
}
}
});
btnOpen.setText("open");
lbl_image_text = new CLabel(parent, SWT.Resize);
}
}
CLabel 类提供了Label类的一些高级功能。 该类可以同时显示其文本标签和图像标签。
lbl_image_text.setText("Welcome");
lbl_image_text.setImage(SWTResourceManager.getImage("Image Path"));