SWT工具栏菜单的实现

本文介绍如何在SWT环境中创建带有下拉菜单的工具栏按钮。通过设置SWT.POP_UP和SWT.DROP_DOWN,实现弹出菜单的功能。详细讲解了事件监听、菜单定位和显示的方法,提供了具体的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

弹出菜单要定义为SWT.POP_UP
带有下拉按钮的工具栏按钮要设置为SWT.DROP_DOWN
判断是否是单击下拉按钮事件的方法代码:
if(event.detail == SWT.ARROW)
显示菜单时,要先计算出菜单所要出现的位置,然后通过setLocation(x,y)方法定位菜单,最后使用setVisible(true)方法将菜单显示出来。

package com.layotech.www.toolbar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.layotech.www.facory.ImageFactory;

public class ToolBarSample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("ToolBar");
shell.setLayout(new GridLayout());
Composite tool = new Composite(shell, SWT.NONE);
tool.setLayoutData(new GridData(SWT.LEFT, SWT.TOP,true,false));
//创建工具栏对象
final ToolBar toolBar = new ToolBar(tool, SWT.FLAT);
//创建打开按钮
ToolItem openItem = new ToolItem(toolBar, SWT.NONE);
openItem.setImage(ImageFactory.loadImage(display, ImageFactory.OPEN_IMAGE));
openItem.setText("打开");
openItem.setToolTipText("打开");
final ToolItem helpItem = new ToolItem(toolBar, SWT.DROP_DOWN);
helpItem.setImage(ImageFactory.loadImage(display, ImageFactory.PREVIEW_IMAGE));
helpItem.setText("帮助");
helpItem.setToolTipText("帮助");
final Menu helpMenu = new Menu(shell,SWT.POP_UP);
MenuItem welcomeItem = new MenuItem(helpMenu, SWT.PUSH);
welcomeItem.setText("欢迎");
new MenuItem(helpMenu, SWT.SEPARATOR);
MenuItem updateItem = new MenuItem(helpMenu, SWT.PUSH);
updateItem.setText("在线更新");
MenuItem aboutItem = new MenuItem(helpMenu, SWT.PUSH);
aboutItem.setText("关于我们");
helpItem.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
if(event.detail == SWT.ARROW){
//获得帮助按钮所在的坐标位置和大小
Rectangle rec = helpItem.getBounds();
//计算出菜单出现的起始位置
Point point = new Point(rec.x, rec.y+rec.height);
//将该店转化为与屏幕相对的点
point = toolBar.toDisplay(point);
//设置菜单的位置
helpMenu.setLocation(point.x,point.y);
helpMenu.setVisible(true);
}
}
});
toolBar.pack();
Text content = new Text(shell, SWT.MULTI);
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
shell.setSize(200,150);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
ImageFactory.dispose();
display.dispose();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值