자바 SWT로드 및 이미지 크기 조정은보기 또는 편집기 동적으로
버튼의 FileDialog 상자를 열려면 클릭하고에 표시 할 모든 이미지를 선택합니다 특정 라벨.
하여 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"));
경로가 절대 경로입니까? 'SWTResourceManager.getImage (...)'가 이미지 또는 null를 돌려 주는지? – Baz
예. SWTResourceManager.getImage (...)는 Image {140719108178432} – Sana
과 같은 이미지를 반환합니다. CLabel 클래스가 도움이 될 수 있습니다. 그것은 텍스트와 이미지를 설정할 수 있습니다. 몇 년 전에 저는 그것을 프로젝트에서 완벽하게 사용했습니다. – CHmoonKa