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 PopupWindows1 { static JFrame frame; static int width=320; static int height=240; static int xCoor=Toolkit.getDefaultToolkit().getScreenSize().width-width;//一直不变的x坐标 static int SHeight=Toolkit.getDefaultToolkit().getScreenSize().height;//获取当前系统的屏幕高度 static int yCoor=SHeight-height;//窗口停止的位置 static int yCoor0=SHeight+1;//一直变化的y坐标 static Timer T0; public PopupWindows1(){ frame=new JFrame(); frame.setTitle("弹出窗口1"); frame.setSize(width,height); frame.setResizable(false); frame.setLocation(xCoor,yCoor0);//窗口最初的位置,后面的设置会覆盖掉前面的坐标 frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.setAlwaysOnTop(true);//设置窗口一直显示屏幕的在最前端 frame.setVisible(true); T0=new Timer(100,new Tim_0()); T0.start(); //点击关闭按钮,窗口退出屏幕,在这里是重写窗口的关闭方法,如果要重写方法成功的话,则,必须写frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE) frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent arg0) { Timer T1=new Timer(100,new Tim_1()); T1.start(); } }); } static class Tim_0 implements ActionListener{ public void actionPerformed(ActionEvent e){ if(yCoor0>yCoor){ yCoor0-=4; frame.setLocation(xCoor,yCoor0); }else{ frame.setLocation(xCoor,yCoor); } } } static class Tim_1 implements ActionListener{ public void actionPerformed(ActionEvent e){ if(yCoor0>=yCoor-4){ yCoor0+=4; frame.setLocation(xCoor,yCoor0); T0.stop(); if(yCoor0>SHeight){ System.exit(0); } } } } public static void main(String[] args){ new PopupWindows1(); } }