我正试图让对话保持在它的父母之上。以下代码类似于我对子对话框所做的操作,减去了父对象的传入。我从编写以下代码开始:
public static void main(String [] args)
{
final Display display = new Display();
final Shell shell = new Shell(display, SWT.ON_TOP);
shell.setLayout(new FillLayout());
shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
}
这会导致对话框保持在最顶层,但现在我无法移动它。我尝试将对shell的构造函数的调用更新为:
final Shell shell = new Shell(display, SWT.ON_TOP | SWT.DIALOG_TRIM);
和
final Shell shell = new Shell(display, SWT.ON_TOP | SWT.SHELL_TRIM);
这两个选项都允许我通过单击并拖动窗口周围的边框来更改对话框的大小,但不允许我移动对话框。
我在网上找到的唯一一件事就是为鼠标事件添加一个监听器并自行移动:
Listener l = new Listener()
{
Point origin;
@Override
public void handleEvent(Event pEvent)
{
switch(pEvent.type)
{
case SWT.MouseDown:
origin = new Point(pEvent.x, pEvent.y);
break;
case SWT.MouseUp:
origin = null;
break;
case SWT.MouseMove:
if(origin != null)
{
Point p = display.map(shell, null, pEvent.x, pEvent.y);
shell.setLocation(p.x - origin.x, p.y - origin.y);
}
break;
}
}
};
shell.addListener(SWT.MouseDown, l);
shell.addListener(SWT.MouseUp, l);
shell.addListener(SWT.MouseMove, l);
shell.open(); //Rest of code as above
我在http://jexp.ru/index.php/Java_Tutorial/SWT/Shell
找到了这个建议无论如何,在SWT中创建一个始终位于顶部的对话框,并且具有与默认SWT对话框(带有样式的对话框:SWT.SHELL_TRIM)相同的外观,感觉和交互,而无需编写自己的监听器? / p>
答案 0 :(得分:1)
您需要使用自己的侦听器。下面的代码应该有所帮助: -
public class Demo {
static Boolean blnMouseDown=false;
static int xPos=0;
static int yPos=0;
public static void main(final String[] args) {
Display display=new Display();
final Shell shell = new Shell( Display.getDefault(), SWT.RESIZE);
shell.open();
shell.addMouseListener(new MouseListener() {
@Override
public void mouseUp(MouseEvent arg0) {
// TODO Auto-generated method stub
blnMouseDown=false;
}
@Override
public void mouseDown(MouseEvent e) {
// TODO Auto-generated method stub
blnMouseDown=true;
xPos=e.x;
yPos=e.y;
}
@Override
public void mouseDoubleClick(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
shell.addMouseMoveListener(new MouseMoveListener() {
@Override
public void mouseMove(MouseEvent e) {
// TODO Auto-generated method stub
if(blnMouseDown){
shell.setLocation(shell.getLocation().x+(e.x-xPos),shell.getLocation().y+(e.y-yPos));
}
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.close();
}
}