SWT 给工具栏加图标。

Mybeautiful 2006-11-24 11:28:45
我使用的是Eclipse3.2 +SWT designer5.0.

现在在窗口中加入了工具条,想给工具条加图片,但是好像不行,请高手指点一二。

代码如下:

final ToolItem newItemToolItem = new ToolItem(toolBar, SWT.PUSH);
newItemToolItem.setText("Run");
newItemToolItem.setImage(new Image(Display.getDefault(),"image/tray1.ico"));
...全文
433 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hexise 2006-12-04
  • 打赏
  • 举报
回复
路径错了,改正方法有两种:
1.换用绝对路径.
2.使用下面的类读取相对路径的图象:
import org.eclipse.swt.graphics.Image;

/**
* ImageUtil contains a method to get image from image file path.
*/

public class ImageUtil {

private static ImageUtil util;

private ImageUtil(){}

private static ImageUtil getInstance(){
if(util == null)
util = new ImageUtil();
return util;
}

/**
* get image from file path
* @param path image file path
* @return image
*/
public static Image getImage(String path){
return new Image(null, getInstance().getClass().getClassLoader().getResourceAsStream(path));
}
}
chiny 2006-11-24
  • 打赏
  • 举报
回复
sorry, i didn't use editor to validate the API.

the correct statement:

item.getParent().getShell().getDisplay()

or

toolBar.getShell().getDisplay()
Mybeautiful 2006-11-24
  • 打赏
  • 举报
回复
Thanks for your suggestion.

It seem that if you want to use image in toolitem, all toolitems must use images, or esle the toolbar will not display.

But I still have the following question,
What's toolItem.getShell().getDisplay() ? ToolItem have no getShell method at all!


Would you like to give me more help?
chiny 2006-11-24
  • 打赏
  • 举报
回复
Remember another rule:
The line
newItemToolItem.setImage(new Image(Display.getDefault(),"image/tray1.ico"));

has serveral problems when it supply formal project.

You should complete it like this:

Image img = new Image(toolitem.getShell().getDisplay,"image/tray1.ico");

.....

img.dispose();

Display.getDefault !== toolItem.getShell().getDisplay();

chiny 2006-11-24
  • 打赏
  • 举报
回复
The toolitem can only exists as text or image.

//newItemToolItem.setText("Run");
newItemToolItem.setImage(xxxxxx);

OR

newItemToolItem.setText("Run");
//newItemToolItem.setImage(xxxxxx);

NOT

newItemToolItem.setText("Run");
newItemToolItem.setImage(xxxxxx);




Mybeautiful 2006-11-24
  • 打赏
  • 举报
回复
有,要是有这句,怎个工具条根本就不出来,并且也不报错。
newItemToolItem.setImage(new Image(Display.getDefault(),"image/tray1.ico"));

要是没有这句,工具条可以出来,就是全文字的。
xiaoxiao130130 2006-11-24
  • 打赏
  • 举报
回复
项目IMAGE目录下有相应的图象没有

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧