我正在寻找一种在单击按钮时在按钮旁边显示小窗口的方法。
使用 Jface
Dialog
不是我想要的,而是一种可以像工具一样精确定位在 Button
旁边的窗口提示。
最佳答案
您可以使用第二个 Shell
并将其放置在 Button
旁边:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
final Shell popup = new Shell(shell, SWT.NONE);
popup.setLayout(new FillLayout());
Text input = new Text(popup, SWT.BORDER);
button.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event event)
{
if(popup != null && !popup.isDisposed())
popup.close();
}
});
shell.pack();
shell.open();
popup.pack();
popup.open();
popup.setLocation(button.toDisplay(button.getBounds().width, 1));
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
看起来像这样:
<小时/>
当然,您需要处理原始 shell 的调整大小和移动事件以将其保持在适当的位置。
关于java - 单击按钮后显示小窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26883687/