我想点击按钮后显示一张已经存在的JPG图片,请问要怎么做?

解决方案 »

  1.   

    SWT不适合图像处理,用Swing吧。
      

  2.   


    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.PaintEvent;
    import org.eclipse.swt.events.PaintListener;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Canvas;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.FileDialog;
    import org.eclipse.swt.widgets.Shell;public class MyImage { private String fileName = null;

    private Image image = null;

    private Canvas canvas;

    public static void main(String[] args) {
    try {
    MyImage window = new MyImage();
    window.open();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public void open() {
    final Display display = Display.getDefault();
    final Shell shell = new Shell();
    shell.setSize(309, 314);
    shell.setText("SWT Application");
    shell.open();

    Button button_Scan = new Button(shell, SWT.NONE);
    button_Scan.setText("浏览");
    button_Scan.setBounds(3, 3, 55, 25);
    button_Scan.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(final SelectionEvent e) {
    FileDialog dlg =new FileDialog(shell, SWT.OPEN);
    dlg.setText("Open");
    dlg.setFilterNames(new String[]{"图片文件(*.jpg)","图片文件(*.gif)"});
    dlg.setFilterExtensions(new String[]{"*.jpg", "*.gif"});
    fileName = dlg.open();//取得打开图片的物理地址
    if (fileName!=null){
    image = new Image(null, fileName);
    canvas.redraw();
    }
    }
    }); canvas = new Canvas(shell, SWT.BORDER);
    canvas.setBounds(0, 29, 300, 256);
    canvas.addPaintListener(new PaintListener(){
    public void paintControl(PaintEvent arg0) {
    if (image != null) {
    arg0.gc.drawImage(image, 0, 0);
    }
    }
    });

    shell.layout();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    }
    }
      

  3.   

    楼上正解,建议楼主碰到问题多查查api
    swt也有的