2010-12-15 15 views
5

La mia prova come segue, che non viene in mente nulla:Come mostrare un'immagine con swt in java?

public static void main(String[] args) { 
    Display display = new Display(); 
    Shell shell = new Shell(display); 

    Image image = new Image(display, 
     "D:/topic.png"); 
    GC gc = new GC(image); 
    gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); 
    gc.drawText("I've been drawn on",0,0,true); 
    gc.dispose(); 

    shell.pack(); 
    shell.open(); 

    while (!shell.isDisposed()) { 
     if (!display.readAndDispatch()) { 
      display.sleep(); 
     } 
    } 
    display.dispose(); 
    // TODO Auto-generated method stub 
} 
+0

Non sembra come si sta in realtà la visualizzazione di qualsiasi cosa ... – Robert

+0

Voglio mostrare l'immagine. .. – lex

risposta

5

Vedere la SWT-Snippets per gli esempi. This one utilizza un immagine label

Shell shell = new Shell (display); 
Label label = new Label (shell, SWT.BORDER); 
label.setImage (image); 
+0

Ma voglio mostrare l'immagine in una finestra popup, non come etichetta. – lex

+0

Prova il codice, fa esattamente quello che vuoi. Non farti confondere dall'etichetta :) –

+0

Ho provato il codice, nessuna immagine sta spuntando .. – lex

2

Ti manca una cosa nel codice. Gestore eventi per vernice. Normalmente quando crei un componente genera un evento paint. Tutte le cose relative al disegno dovrebbero essere incluse. Inoltre è necessario non creare il GC esplicitamente .. Viene fornito con l'oggetto evento :)

import org.eclipse.swt.*; 
import org.eclipse.swt.graphics.*; 
import org.eclipse.swt.layout.*; 
import org.eclipse.swt.widgets.*; 

public class ImageX 
{ 
    public static void main (String [] args) 
    { 
     Display display = new Display(); 
     Shell shell = new Shell (display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED); 
     shell.setLayout(new FillLayout()); 
     final Image image = new Image(display, "C:\\temp\\flyimage1.png"); 

     shell.addListener (SWT.Paint, new Listener() 
     { 
      public void handleEvent (Event e) { 
       GC gc = e.gc; 
       int x = 10, y = 10; 
       gc.drawImage (image, x, y); 
       gc.dispose(); 
      } 
     }); 

     shell.setSize (600, 400); 
     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 

     if(image != null && !image.isDisposed()) 
      image.dispose(); 
     display.dispose(); 
    } 

}