2014-05-06 118 views
1

所以我有一個按鈕w /圖像使用SWT。 爲它設計的代碼是在這裏:在Mac上SWT圖像按鈕不工作

final Button button = new Button(shell, SWT.FLAT | SWT.CENTER); button.setBounds(54, 121, 109, 33); button.setImage(SWTResourceManager.getImage(LoginDialog.class, "SignInButton.png"));

我想有圖像佔據整個按鈕沒有邊界就像我可以在Windows。有誰知道如何做到這一點?

這裏是什麼樣子大氣壓的圖像: enter image description here

基本上我只是想白色部分消失了。

+2

請勿使用'setBounds' - 使用[layout](http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html),以便按鈕可以制定出自己的規模。我不認爲可以在Mac按鈕上刪除邊框。 –

回答

2

好吧,Button不是最好的出發點,如果你基本上想一個完全不同類型的按鈕。但是,您可以使用this教程創建您自己的小部件。我寫到這裏的例子:

public class ImageButton extends Composite 
{ 
    private Color textColor; 
    private Image image; 
    private String text; 
    private int  width; 
    private int  height; 

    public ImageButton(Composite parent, int style) 
    { 
     super(parent, style); 

     textColor = Display.getDefault().getSystemColor(SWT.COLOR_WHITE); 

     /* Add dispose listener for the image */ 
     addListener(SWT.Dispose, new Listener() 
     { 
      @Override 
      public void handleEvent(Event arg0) 
      { 
       if (image != null) 
        image.dispose(); 
      } 
     }); 

     /* Add custom paint listener that paints the stars */ 
     addListener(SWT.Paint, new Listener() 
     { 
      @Override 
      public void handleEvent(Event e) 
      { 
       paintControl(e); 
      } 
     }); 

     /* Listen for click events */ 
     addListener(SWT.MouseDown, new Listener() 
     { 
      @Override 
      public void handleEvent(Event arg0) 
      { 
       System.out.println("Click"); 
      } 
     }); 
    } 

    private void paintControl(Event event) 
    { 
     GC gc = event.gc; 

     if (image != null) 
     { 
      gc.drawImage(image, 1, 1); 
      Point textSize = gc.textExtent(text); 
      gc.setForeground(textColor); 
      gc.drawText(text, (width - textSize.x)/2 + 1, (height - textSize.y)/2 + 1, true); 
     } 
    } 

    public void setImage(Image image) 
    { 
     this.image = new Image(Display.getDefault(), image, SWT.IMAGE_COPY); 
     width = image.getBounds().width; 
     height = image.getBounds().height; 
     redraw(); 
    } 

    public void setText(String text) 
    { 
     this.text = text; 
     redraw(); 
    } 

    @Override 
    public Point computeSize(int wHint, int hHint, boolean changed) 
    { 
     int overallWidth = width; 
     int overallHeight = height; 

     /* Consider hints */ 
     if (wHint != SWT.DEFAULT && wHint < overallWidth) 
      overallWidth = wHint; 

     if (hHint != SWT.DEFAULT && hHint < overallHeight) 
      overallHeight = hHint; 

     /* Return computed dimensions plus border */ 
     return new Point(overallWidth + 2, overallHeight + 2); 
    } 

    public static void main(String[] args) 
    { 
     Display display = Display.getDefault(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new GridLayout(1, false)); 

     ImageButton button = new ImageButton(shell, SWT.NONE); 
     button.setImage(new Image(display, "button.png")); 
     button.setText("Button"); 

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

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

是這樣的:

enter image description here


當然你必須要照顧不同的國家像pressedhover等。

+0

哦,我明白了,這對我的項目真的會有所幫助,謝謝。不過,希望swt mac按鈕可以像windows按鈕一樣工作。 = / – Salvite