问题1:我用swt做了一个始终在所有程序最前的窗口,现在在另外一个用swing做的窗口,点击按钮怎么能swt的那个窗口隐藏呢?
问题2:我还想swt的窗口不允许随便关闭,当用户不小心点了关闭按钮后,要求给出提示信息,当确定以后,才允许真正关闭swt窗口。
现附一个始终在所有窗口最前的swt的简单代码:
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;
import org.eclipse.swt.internal.win32.*;public class Swt {
  public Swt() {
      Display display = new Display(); 
      Shell shell = new Shell(display); 
      shell.setText("Hello world");
      shell.setBounds(0, 700, 1000, 200);
        //窗口始终最前
      OS.SetWindowPos(shell.handle, OS.HWND_TOPMOST, 500, 111, 1000,200,
                        SWT.NULL);      Text helloText = new Text(shell, SWT.CENTER); 
      helloText.setText("Hello,World!"); 
      helloText.pack(); 
      shell.pack(); 
      shell.open(); 
      while (!shell.isDisposed()) { 
        if (!display.readAndDispatch()) { 
            display.sleep(); 
      } 
     } 
      display.dispose(); 
   }
   public static void main(String[] args) {
      Swt t = new Swt();      
   }
}

解决方案 »

  1.   

    第一个问题调用dispose();不行吗?
      

  2.   

    回复楼上,我试过了,不管是对display进行dispose也好,对shell进行dispose也好,都会抛出异常。
      

  3.   

    目前解决了第二个问题,在swt的窗口程序中,设置Shell的风格为SWT.TITLE类型的,这样窗口就变成没有最大化,最小化和关闭的工具条。只要在shell内添加一个button,在button事件中,来对关闭事件进行响应,因为不熟悉swt的dialog是怎么做的,我采用swing的confirmDialog来做提示对话框。
    第一个问题继续关注中,期待大家提出好建议。以前可能是没有代码的原因,现在附一个简单的代码文件:
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.*;
    import org.eclipse.swt.internal.win32.*;public class Swt {
      Display display = new Display();
    public Swt() {
        display = new Display();
        Shell shell = new Shell(display,SWT.TITLE);
        shell.setText("Hello world");
        shell.setBounds(0, 700, 100, 200);
        //窗口始终最前
        OS.SetWindowPos(shell.handle, OS.HWND_TOPMOST, 500, 111, 100,200,
                    SWT.NULL);    Text helloText = new Text(shell, SWT.CENTER);
        Button closeButton = new Button(shell, SWT.PUSH | SWT.CENTER);
        closeButton.setText("关闭");
        closeButton.addMouseListener(new MouseAdapter() {            
               public void mouseDown(MouseEvent m) {
                   int result = javax.swing.JOptionPane.showConfirmDialog(null,"确定关闭对话框?","提示",JOptionPane.OK_CANCEL_OPTION);
                   if(result=0)
                          display.dispose();                    
               }            
           });
        helloText.setText("Hello,World!");
        helloText.pack();
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
      }    display.dispose();
      }
      public static void main(String[] args) {
        Swt t = new Swt();
      }
    }