Java 弹出窗口系列之 三

package popupWindows;

/*

 * 该代码执行的效果是,窗口从屏幕右下角弹出,点击关闭按钮后,窗口淡出

 */

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.*;

public class PopupWindows2 {

static JFrame frame;

static int width=320;

static int  height=240;

static int SWidth=Toolkit.getDefaultToolkit().getScreenSize().width;//得到屏幕的宽度

static int SHeight=Toolkit.getDefaultToolkit().getScreenSize().height;//得到屏幕的高度

static int xCoor=SWidth-width;//窗口左上角的x坐标

static int yCoor=SHeight-height;//窗口弹出后最终的y坐标

static int yCoor0=SHeight;

    static float value=1.0f;

static Timer Ti0;

public PopupWindows2(){

frame=new JFrame();

frame.setTitle("弹出窗口2");

frame.setSize(width,height);

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

frame.setResizable(false);

frame.setLocation(xCoor,yCoor0);//窗口的初始位置

frame.setAlwaysOnTop(true);

frame.setVisible(true);

Ti0=new Timer(100,new Tim_00());

Ti0.start();

//关闭窗口后,窗口淡出屏幕

frame.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

Timer Ti1=new Timer(100,new Tim_01());

Ti1.start();

}

});

}

static class Tim_00 implements ActionListener{

public void actionPerformed(ActionEvent e){

if(yCoor0>yCoor){

yCoor0-=4;

frame.setLocation(xCoor,yCoor0);

 }else{

 frame.setLocation(xCoor,yCoor);

 Ti0.stop();

 }

}

}

  static class Tim_01 implements ActionListener{

  public void actionPerformed(ActionEvent e){

  value-=0.02f;

  if(value>=0.02f){

SwingUtilities.invokeLater(new Runnable() {   

   public void run() {    

       com.sun.awt.AWTUtilities.setWindowOpacity(frame,value);   

   }   

});  

}else{

System.exit(0);

}

  }

  }

  

public static void main(String[] args){

new PopupWindows2();

}

}

你可能感兴趣的:(Java 弹出窗口系列之 三)