2011-08-31 89 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);