请问各位如何点关闭按钮实现最小化的功能?我不想隐藏它,只想最小化在任务栏或显示一个托盘,不知可实现不?//重写窗口关闭事件
shell.addShellListener(new ShellAdapter(){
public void shellClosed(ShellEvent arg){
//shell.setMinimized(true);
}
});

解决方案 »

  1.   

    final Tray tray = Display.getDefault().getSystemTray();
    trayItem = new TrayItem(tray, SWT.NONE);
    //trayItem.setImage(null)//设置图片
    trayItem.setToolTipText(title);
    trayItem.setVisible(false);trayItem.addListener(SWT.Selection, new Listener()
    {
    public void handleEvent(Event event)
    {
    if (!shell.isVisible())
    shell.setVisible(true);
    shell.setActive();
    trayItem.setVisible(false);
    }
    });

    shell.addShellListener(new ShellAdapter()
    {
    public void shellIconified(ShellEvent e) //最小化
    {
    shell.setVisible(false);
    trayItem.setVisible(true);
    }
    }
      

  2.   

    有点错误,改正如下:
    final Tray tray = Display.getDefault().getSystemTray();
    trayItem = new TrayItem(tray, SWT.NONE);
    //trayItem.setImage(null)//设置图片
    trayItem.setToolTipText(title);
    trayItem.setVisible(false);trayItem.addListener(SWT.Selection, new Listener()
    {
    public void handleEvent(Event event)
    {
    if (!shell.isVisible())
                       {
    shell.setVisible(true);
    shell.setActive();
    trayItem.setVisible(false);
    }
              }
    });

    shell.addShellListener(new ShellAdapter()
    {
    public void shellIconified(ShellEvent e) //最小化
    {
    shell.setVisible(false);
    trayItem.setVisible(true);
    }
    });
      

  3.   

    刚才用JB2006拖了一个:import java.awt.*;
    import javax.swing.*;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class Frame1
        extends JFrame
    {
        public Frame1()
        {
            try
            {
                jbInit();
            }
            catch(Exception exception)
            {
                exception.printStackTrace();
            }
        }    private void jbInit() throws Exception
        {
            getContentPane().setLayout(null);
            this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            this.addWindowListener(new Frame1_this_windowAdapter(this));
            this.setBounds(100,50,600,400);
            jButton1.setBounds(new Rectangle(145,178,122,33));
            jButton1.setMnemonic('Q');
            jButton1.setText("退出(Q)");
            jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
            this.getContentPane().add(jButton1);
            this.setVisible(true);
        }    public static void main(String[] args)
        {
            Frame1 frame1=new Frame1();
        }    public void this_windowClosing(WindowEvent e)
        {
            this.setState(this.ICONIFIED);
        }    JButton jButton1=new JButton();
        public void jButton1_actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }
    class Frame1_jButton1_actionAdapter
        implements ActionListener
    {
        private Frame1 adaptee;
        Frame1_jButton1_actionAdapter(Frame1 adaptee)
        {
            this.adaptee=adaptee;
        }    public void actionPerformed(ActionEvent e)
        {
            adaptee.jButton1_actionPerformed(e);
        }
    }
    class Frame1_this_windowAdapter
        extends WindowAdapter
    {
        private Frame1 adaptee;
        Frame1_this_windowAdapter(Frame1 adaptee)
        {
            this.adaptee=adaptee;
        }    public void windowClosing(WindowEvent e)
        {
            adaptee.this_windowClosing(e);
        }
    }
    自己研究
      

  4.   

    楼主啊,zhuokai() 写得比较明白了,这些代码放在main()中或createContents()中