簡體   English   中英

有沒有辦法使SWT外殼在頂部移動?

[英]Is there a way to make a SWT shell moveable and on top?

我試圖使對話框停留在其父級之上。 以下代碼類似於我對子對話框所做的操作,減去了父級的傳入。 我首先編寫以下代碼:

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的對話框)相同的外觀,感覺和交互的對話框,而無需編寫我自己的偵聽器?

您需要使用自己的偵聽器。 下面的代碼應該有幫助:-

  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();
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM