If I create a set of actions to be used in a JFace application and I assign images to those actions, those images show up in both the toolbar (where I want them) and in the menus (where I don't want them).
如果我创建了一组要在JFace应用程序中使用的动作,并且我将图像分配给这些动作,那些图像将显示在工具栏(我想要它们)和菜单(我不想要它们)中。
Other than supplying two completely separate sets of actions (which eliminates part of the point of actions in the first place), how can I arrange to have those images displayed ONLY in the toolbar, and have the menus display only text?
除了提供两组完全独立的动作(首先消除了部分操作点)之外,我如何安排将这些图像仅显示在工具栏中,并让菜单只显示文本?
3 个解决方案
#1
I ran into this problem as well (except I wanted different text for the toolbar and menu.) I ended up using the same action, but two different instances of it.
我也遇到了这个问题(除了我想要工具栏和菜单的不同文本。)我最终使用相同的操作,但它有两个不同的实例。
// Use this one in the menu
Action a = new Action();
// Use this one in the toolbar
Action a2 = new Action();
a2.setImageDescriptor(img);
Alternately, you could store the image descriptor in the action for the toolbar version and set it to null for the menu version.
或者,您可以将图像描述符存储在工具栏版本的操作中,并将其设置为null以用于菜单版本。
#2
I haven't checked this myself, but give this method a looksee:
我自己没有检查过这个,但是请给出这个方法:
public int getStyle() { ... }
It's defined in the Action class, and it appears to return the type of interface element that the Action is graphically represented as. So then you could override the getImageDescriptor() method:
它在Action类中定义,它似乎返回Action以图形方式表示的interface元素的类型。那么你可以覆盖getImageDescriptor()方法:
public ImageDescriptor getImageDescriptor() {
if (getStyle() == AS_DROP_DOWN_MENU)
return null; // No icon in a menu
return someImageDescriptor; // Actual icon
}
#3
I ended up essentially duplicating the actions, making pairs, one with an image, one without, thereby eliminating the benefit of actions in the first place (sigh). On the other hand, since each action does just invoke a single method in the controller to perform the work, it's not too insidious and grouping the pairs together makes it reasonably clear what's going on.
我最终基本上复制了动作,制作了对,一个带有图像,一个没有,从而首先消除了动作的好处(叹息)。另一方面,由于每个动作只是在控制器中调用一个方法来执行工作,所以它并不太阴险,并且将这些对分组在一起使得它相当清楚发生了什么。
#1
I ran into this problem as well (except I wanted different text for the toolbar and menu.) I ended up using the same action, but two different instances of it.
我也遇到了这个问题(除了我想要工具栏和菜单的不同文本。)我最终使用相同的操作,但它有两个不同的实例。
// Use this one in the menu
Action a = new Action();
// Use this one in the toolbar
Action a2 = new Action();
a2.setImageDescriptor(img);
Alternately, you could store the image descriptor in the action for the toolbar version and set it to null for the menu version.
或者,您可以将图像描述符存储在工具栏版本的操作中,并将其设置为null以用于菜单版本。
#2
I haven't checked this myself, but give this method a looksee:
我自己没有检查过这个,但是请给出这个方法:
public int getStyle() { ... }
It's defined in the Action class, and it appears to return the type of interface element that the Action is graphically represented as. So then you could override the getImageDescriptor() method:
它在Action类中定义,它似乎返回Action以图形方式表示的interface元素的类型。那么你可以覆盖getImageDescriptor()方法:
public ImageDescriptor getImageDescriptor() {
if (getStyle() == AS_DROP_DOWN_MENU)
return null; // No icon in a menu
return someImageDescriptor; // Actual icon
}
#3
I ended up essentially duplicating the actions, making pairs, one with an image, one without, thereby eliminating the benefit of actions in the first place (sigh). On the other hand, since each action does just invoke a single method in the controller to perform the work, it's not too insidious and grouping the pairs together makes it reasonably clear what's going on.
我最终基本上复制了动作,制作了对,一个带有图像,一个没有,从而首先消除了动作的好处(叹息)。另一方面,由于每个动作只是在控制器中调用一个方法来执行工作,所以它并不太阴险,并且将这些对分组在一起使得它相当清楚发生了什么。