2009-09-21 79 views
19

如何將圖像添加到GWT按鈕或如何使用Mosaic的按鈕添加圖像。我無法弄清楚如何在我的代碼中使用THIS示例。我需要添加什麼庫。我在我的項目中有馬賽克圖書館,我可以使用它,但他們在那裏的例子並不適合我。GWT按鈕中的圖像

感謝

+0

你試過了什麼代碼,結果是什麼? – CPerkins 2009-09-22 01:11:19

+0

如果你想有圖像和文字,那麼我可能創建了你想要的。我在這個SO問題上寫了一篇文章:http://stackoverflow.com/questions/1853042/creating-custom-button-in-gwt/2449019#2449019 – Juri 2010-03-15 17:25:35

+0

GWT官方樣本: http://gwt.googleusercontent.com/樣品/展示/ Showcase.html#!CwCustomButton – 2013-01-23 05:44:04

回答

26

如果你只是想將圖像添加到一個正常的GWT按鈕,然後PushButton是要走的路:

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

或者,你這個方法。

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

我發現PushButton似乎有正確計算x,y點擊點的問題,正常按鈕沒有。

23

通過按鈕不具有相同的行爲,你有比創建自己的圖像標籤更漂亮的解決方案:

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

只需設置HTML img標籤內按鈕爲:

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

只需在圖像中使用CSS和樣式。像這樣的東西應該使圖像出現在按鈕文字之前:

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