Java GWT 按钮中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1456889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:27:35  来源:igfitidea点击:

Image in GWT Button

javagwtgwt-mosaic

提问by Maksim

How do I add image to GWT Button or how do I use Mosaic's Buttons to add image. I can't figure out how to use THISexample in my code. What library do I need to add. I have Mosaic Library in my project and I can use it but the example that they have there does not work for me.

如何将图像添加到 GWT 按钮或如何使用 Mosaic 的按钮添加图像。我不知道如何在我的代码中使用这个例子。我需要添加什么库。我的项目中有马赛克库,我可以使用它,但他们在那里的示例对我不起作用。

Thanks

谢谢

采纳答案by Igor Klimer

If you just want to add an image to a normal GWT Button, then PushButtonis the way to go:

如果您只想将图像添加到普通的 GWT 按钮,那么PushButton是您的最佳选择:

PushButton pushButton = new PushButton(new Image("test.png"));

回答by Robin

Alternatively, you this method.

或者,你这个方法。

Button editRow = new Button("Edit Row");
editRow.setHTML(("<img border='0' src='rowEdit.Png' />");

I have found the the PushButton appears to have a problem with calculating its x, y click point correctly, which the normal button does not have.

我发现 PushButton 似乎在正确计算其 x、y 单击点时存在问题,而普通按钮则没有。

回答by Michael

PushButton does not have the same behavior, and you have a prettier solution than creating yourself an image tag :

PushButton 没有相同的行为,您有一个比创建自己的图像标签更漂亮的解决方案:

Image img = new Image("whatever.jpg");
Button button = new Button();
button.getElement().appendChild(img.getElement());

回答by Firas Sabbah

Just set the HTML img tag inside the button as:

只需将按钮内的 HTML img 标记设置为:

Button btn = new Button("<img src='image.png'/>");

回答by David Parish

Just use CSS and style in the image. Something like this should make the image appear before the button text:

只需在图像中使用 CSS 和样式。这样的事情应该使图像出现在按钮文本之前:

background-image: url("/yourimage.png");
background-repeat:  no-repeat;
height: 20px;
width:      20px;