如何绘制swt图像?

u12*_*123 7 java swt

我试图绘制一个swt图像但没有出现:

Display display = new Display();
Shell shell = new Shell(display);
shell.open();

Image image = new Image(display, "C:/sample_image.png");
Rectangle bounds = image.getBounds();

GC gc = new GC(image);
gc.drawImage(image, 100, 100);
// gc.drawLine(0, 0, bounds.width, bounds.height);
// gc.drawLine(0, bounds.height, bounds.width, 0);
// gc.dispose();
// image.dispose();

while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
    display.sleep();
}
display.dispose();
Run Code Online (Sandbox Code Playgroud)

我测试过图像存在并且有内容 - 任何想法?

Mar*_*ato 8

创建标签并在其上设置图像.

Image myImage = new Image( display, "C:/sample_image.png" );
Label myLabel = new Label( shell, SWT.NONE );
myLabel.setImage( myImage );
Run Code Online (Sandbox Code Playgroud)

这对你来说已经足够了.