我正在尝试创建一个具有下拉菜单且可单击的按钮。 在开发 eclipse 插件时我该怎么做? 如果可能的话,我正在寻找特定于 Eclipse 插件开发的答案。最好没有 xml,只有 java 代码。
搜索并检查了 eclipse 文档,但没有找到执行这两项操作的按钮的可用答案。希望有一个在线示例的链接(猜测它存在)。
我希望该按钮的行为类似于 Debug\Run\New 图标按钮:可通过侧面的下拉菜单进行点击。
最佳答案
最后我自己找到了一个迂回的解决方案。 这是一个例子:
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.SWT;
....
//These 3 lines are the solution for style, turns out to be simple.
Image image = <<Your image for deselected>>;
ToolItem item = = new ToolItem(toolBar, SWT.BEGINNING | SWT.DROP_DOWN);
item.setImage(image);
//Menu creation for arrow
Menu menu = new Menu(item.getParent().getShell());
new MenuItem(menu, SWT.PUSH).setText("Menu item example 1");
//Indication for state of button press (to give it checkbox behavior - does not have to be atomic)
AtomicBoolean recording = new AtomicBoolean(false);
//This is where the logic is for opening the menu and enabling a checkbox behavior
//is done. ((width * 3) was added because tool bar is on the side for my specific use case)
item.addListener(SWT.Selection, e -> {
if (e.detail == SWT.ARROW) {
Rectangle rect = item.getBounds();
Point pt = new Point(rect.x - (rect.width * 3), rect.y + rect.height);
pt = toolBar.toDisplay(pt);
menu.setLocation(pt.x, pt.y);
menu.setVisible(true);
} else {
//logic for pressing icon. You can swap the icon
//image according to needed with item.setImage()
}
});
关于java - 带下拉菜单的可点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56018931/