java使用swt显示图片示例分享

时间:2021-10-25 05:47:54

代码如下:


import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

 

public class ImagesHelloWorld {

 public static void main(String[] args) {
  Display display = Display.getDefault();
  Shell shell = new Shell();
  Image image = new Image(display, "c://c240b2dcc132c9c6.jpg");
  shell.setText("ImageReader");
  shell.setImage(image);
  Rectangle bounds = image.getBounds();
  shell.setSize(bounds.width + 15, bounds.height +15);
  shell.open();
  GC gc = new GC(shell);
  gc.drawImage(image,5,5);
  shell.layout();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) {
    display.sleep();
   }
  }
  display.dispose();
 }
}