Java Mac上的SWT图像按钮不工作
所以我有一个按钮,带有使用SWT的图像。 其设计代码如下:Java Mac上的SWT图像按钮不工作,java,eclipse,image,button,swt,Java,Eclipse,Image,Button,Swt,所以我有一个按钮,带有使用SWT的图像。 其设计代码如下: final Button Button=新按钮(外壳,SWT.FLAT | SWT.CENTER); 按钮。立根(54、121、109、33); setImage(SWTResourceManager.getImage(LoginDialog.class、, “SignInButton.png”) 我想让图像占据整个按钮,没有边框,就像在windows中一样。有人知道如何做到这一点吗 下面是一张它看起来像atm的图像: 基本上,我只想
final Button Button=新按钮(外壳,SWT.FLAT | SWT.CENTER);
按钮。立根(54、121、109、33);
setImage(SWTResourceManager.getImage(LoginDialog.class、,
“SignInButton.png”)代码>
我想让图像占据整个按钮,没有边框,就像在windows中一样。有人知道如何做到这一点吗
下面是一张它看起来像atm的图像:
基本上,我只想去掉白色部分。好吧,按钮
不是最好的起点,如果你想画一个完全不同类型的按钮。不过,您可以使用教程创建自己的小部件。我在这里写了一个例子:
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();
}
}
公共类ImageButton扩展了复合
{
私人色彩;
私有图像;
私有字符串文本;
私有整数宽度;
私人内部高度;
公共图像按钮(复合父级,int样式)
{
超级(父母,风格);
textColor=Display.getDefault().getSystemColor(SWT.COLOR\u WHITE);
/*为映像添加dispose侦听器*/
addListener(SWT.Dispose,newlistener())
{
@凌驾
公共无效handleEvent(事件arg0)
{
如果(图像!=null)
image.dispose();
}
});
/*添加绘制星星的自定义绘制侦听器*/
addListener(SWT.Paint,new Listener())
{
@凌驾
公共无效handleEvent(事件e)
{
油漆控制(e);
}
});
/*侦听单击事件*/
addListener(SWT.MouseDown,newlistener())
{
@凌驾
公共无效handleEvent(事件arg0)
{
System.out.println(“单击”);
}
});
}
私有控件(事件)
{
GC=event.GC;
如果(图像!=null)
{
gc.drawImage(图像,1,1);
点textSize=gc.textExtent(文本);
gc.set前台(textColor);
gc.drawText(text,(宽度-textSize.x)/2+1,(高度-textSize.y)/2+1,true);
}
}
公共void setImage(图像)
{
this.image=新图像(Display.getDefault(),image,SWT.image\u COPY);
宽度=image.getBounds().width;
高度=image.getBounds().height;
重画();
}
公共void setText(字符串文本)
{
this.text=文本;
重画();
}
@凌驾
公共点计算(int-wHint、int-hHint、boolean-changed)
{
int-overallWidth=宽度;
int总高度=高度;
*考虑提示*
if(wHint!=SWT.DEFAULT&&wHint
看起来像这样:
当然,您必须注意不同的状态,如按下按钮、悬停按钮等。不要使用设置框-使用按钮可以计算出按钮的大小。我不认为Mac按钮上的边框可以删除。哦,我明白了,这对我的项目真的很有帮助。谢谢。有点希望swt mac按钮能像windows按钮一样工作=/