现在有这样一个需求,用java在屏幕右下角弹出一个窗口。就像进入CSDN时那个广告窗口一样。然后如果在一定时间内都不点击关闭按钮,那这个窗口就自动关闭。请问如何处理“然后如果在一定时间内都不点击关闭按钮,那这个窗口就自动关闭”这个问题?大家给个思路就行。谢谢!

解决方案 »

  1.   

    弹出窗口可以用javax.swing.JFrame来实现,【在一定时间内都不点击关闭按钮,那这个窗口就自动关闭】这个需求可以用javax.swing.Timer来实现(setInitialDelay方法),比如设置5秒种后关闭窗口,先提示到这里,楼主你好好考虑一下。
      

  2.   


    package 回答;import java.awt.Color;
    import java.awt.Frame; 
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;import javax.swing.Timer;public class CloseWindowIn5 extends Frame implements Runnable{ public void showWindow(){
    this.setSize(300, 400);
    this.setLocation(100, 100);
    this.setBackground(Color.gray);
    this.setVisible(true); }

    public static void main(String[] args) {
    CloseWindowIn5 clos = new CloseWindowIn5();
    new Thread(clos).start();
    clos.showWindow();

    } @Override
    public void run() {
    try {
    Thread.sleep(5000);
    setVisible(false);  //关闭窗口
    System.exit(0);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    }

    }
      

  3.   

    package 回答;import java.awt.Color;
    import java.awt.Frame; 
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;import javax.swing.Timer;public class CloseWindowIn5 extends Frame implements Runnable{ public void showWindow(){
    this.setSize(300, 400);
    this.setLocation(100, 100);
    this.setBackground(Color.gray);
    this.setVisible(true); }

    public static void main(String[] args) {
    CloseWindowIn5 clos = new CloseWindowIn5();
    new Thread(clos).start();
    clos.showWindow();

    } @Override
    public void run() {
    try {
    Thread.sleep(5000);
    setVisible(false);
    System.exit(0);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    }

    }
      

  4.   

    public class NewJDialog extends JDialog implements ActionListener
    {
        private Timer time;
        public NewJDialog()
        {
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width - 416) / 2, (screenSize.height - 338) / 2,
                    416, 338);
            time = new Timer(5000, this);
            time.start();
            addMouseListener(new MouseAdapter()
            {
                @Override
                public void mouseEntered(MouseEvent e)
                {
                    time.stop();
                }            @Override
                public void mouseExited(MouseEvent e)
                {
                    time.start();
                }
            });
        }
        public static void main(String args[])
        {
            new NewJDialog().setVisible(true);
        }
        @Override
        public void actionPerformed(ActionEvent e)
        {
            this.dispose();
        }
    }这样写就能达到你的要求
      

  5.   

    如果不是一定使用java来实现的话,那可以利用第三方插件,简单易用,路过
      

  6.   

    如果用现成的话,不是在main函数里调用的话很容易造成frame的内容不显示,求解 ..