2011-08-31 88 views
3

我想把圖像(GIF)放在已經設置爲圖像的背景上(shell.setBackgroundImage(image)),我無法弄清楚如何去除按鈕周圍的透明邊框圖片。如果有人能就這個問題給我一些建議,我將不勝感激。在圖像背景上放置圖像按鈕

這裏是我的代碼:

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

public class Main_page { 
     public static void main(String[] args) { 
       Display display = new Display(); 
       Shell shell = new Shell(display); 
       Image image = new Image(display, "bg.gif"); 
       shell.setBackgroundImage(image); 
       shell.setBackgroundMode(SWT.INHERIT_DEFAULT); 
       shell.setFullScreen(true); 

       Button button = new Button(shell, SWT.PUSH); 
       button.setImage(new Image(display, "button.gif")); 

       RowLayout Layout = new RowLayout(); 
       shell.setLayout(Layout); 

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

魔法師,謝謝你的答案,我肯定會考慮this article。也許我會找到自己的方式。到目前爲止,我已經改進了一些代碼。首先,我設法擺脫了灰色的背景噪音。其次,我最終成功地創造了按鈕,正如我之前所看到的那樣。然而,另一個障礙已經出現。當我刪除圖像(按鈕)透明邊框,結果證明按鈕改變了它的模式(從按鈕到複選框)。問題是我來到了我所尋找的東西附近,現在我有些困惑。如果你有一些時間,請看一下我的代碼。

下面是代碼,如果你啓動它,你會看到的問題是什麼(希望你沒有問題,下載圖像):

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

public class Main_page { 
     public static void main(String[] args) { 
       Display display = new Display(); 
       Shell shell = new Shell(display); 
       Image image = new Image(display, "bg.gif"); // Launch on a screen 1280x1024 
       shell.setBackgroundImage(image); 
       shell.setBackgroundMode(SWT.TRANSPARENT); 
       shell.setFullScreen(true); 


       GridLayout gridLayout = new GridLayout(); 
       gridLayout.marginTop = 200; 
       gridLayout.marginLeft = 20; 
       shell.setLayout(gridLayout); 



       // If you replace SWT.PUSH with SWT.COLOR_TITLE_INACTIVE_BACKGROUND 
       // you will see what I am looking for, despite nasty check box 

       Button button = new Button(shell, SWT.PUSH); 
       button.setImage(new Image(display, "moneyfast.gif")); 



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

能否請您發佈鏈接到這兩個GIF圖像? – Sorceror

+0

這裏是他們http://file.karelia.ru/ffdgqn/ http://file.karelia.ru/d6n9dw/ – foma

回答

3

的唯一方法如何繞過擺脫空間按鈕是設置背景Color或背景Image

button.setBackgroundImage(image); 

但是,如果該按鈕將不會在左上角(因爲它是現在),背景圖像將不被正確地定位。所以首先你必須定位按鈕,然後製作背景的適當部分的圖像,然後將其設置爲按鈕作爲背景圖像。

查看文章Taking a look at SWT Images - Transparency瞭解有關Label(以及Button)透明度的更多詳細信息。

+0

我試圖按照你的意見,但不幸的是沒有成功,也許你會告訴我一些代碼樣本,所以我可以澄清的情況。提前致謝 – foma

1

您還可以通過創建 標籤來確保沒有背景出現在任何平臺上,該標籤帶有充當標籤按鈕和鼠標偵聽器的圖片。

0

風格SWT.TRANSPARENT幫助:

Button button = new Button(buttonParent, SWT.TRANSPARENT); 
    InputStream is = getClass().getResourceAsStream("my_image.png"); 
    ImageData imageData = new ImageData(is).scaledTo(16, 16); 
    Image image = new Image (button.getDisplay(), imageData); 
    button.setImage(image);