在桌面右下角弹出提醒框(JAVA)
下面为java类,右键运行就OK了;
1 package com.renqiu.ggzy.util; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.Cursor; 6 import java.awt.Dimension; 7 import java.awt.FlowLayout; 8 import java.awt.Font; 9 import java.awt.Point; 10 import java.awt.event.MouseAdapter; 11 import java.awt.event.MouseEvent; 12 import java.awt.event.MouseMotionAdapter; 13 import java.text.SimpleDateFormat; 14 import java.util.Date; 15 import java.util.HashMap; 16 import java.util.Map; 17 import javax.swing.BorderFactory; 18 import javax.swing.ImageIcon; 19 import javax.swing.JLabel; 20 import javax.swing.JOptionPane; 21 import javax.swing.JPanel; 22 import javax.swing.JScrollPane; 23 import javax.swing.JTextArea; 24 import javax.swing.SwingConstants; 25 import java.awt.Insets; 26 import java.awt.Toolkit; 27 import javax.swing.JDialog; 28 29 public class RemindUtil { 30 private Map<String, String> feaMap = null; 31 private Point oldP;// 上一次坐标,拖动窗口时用 32 private TipWindow tw = null;// 提示框 33 private ImageIcon img = null;// 图像组件 34 private JLabel imgLabel = null; // 背景图片标签 35 private JPanel headPan = null; 36 private JPanel feaPan = null; 37 private JPanel btnPan = null; 38 private JLabel title = null; 39 private JLabel head = null; 40 private JLabel close = null;// 关闭按钮 41 private JTextArea feature = null; 42 private JScrollPane jfeaPan = null; 43 private JLabel releaseLabel = null; 44 private JLabel sure = null; 45 private SimpleDateFormat sdf = null; 46 47 { 48 sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 49 feaMap = new HashMap<String, String>(); 50 feaMap.put("name", "企业注册四证联办监管系统"); 51 feaMap.put("release", sdf.format(new Date())); 52 feaMap.put( 53 "feature", 54 "1.开发环境:windows\n2.开发语言:java\n3.开发工具:Eclipse4.3\n4.数据库类型:MySql\n5.开发公司:北京云天\n6.联系方式:0317-2598400"); 55 } 56 57 public void VersionUtil() { 58 init(); 59 handle(); 60 tw.setAlwaysOnTop(true); 61 tw.setUndecorated(true); 62 tw.setResizable(false); 63 tw.setVisible(true); 64 tw.run(); 65 } 66 67 public void init() { 68 // 新建300x220的消息提示框 69 tw = new TipWindow(300, 220); 70 img = new ImageIcon("WebContent/WEB-INF/views/background.gif"); 71 imgLabel = new JLabel(img); 72 // 设置各个面板的布局以及面板中控件的边界 73 headPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); 74 feaPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); 75 btnPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); 76 title = new JLabel("流程到期提醒"); 77 head = new JLabel(feaMap.get("name")); 78 close = new JLabel(" x");// 关闭按钮 79 feature = new JTextArea(feaMap.get("feature")); 80 jfeaPan = new JScrollPane(feature); 81 releaseLabel = new JLabel("登录 " + feaMap.get("release")); 82 sure = new JLabel("确定"); 83 sure.setHorizontalAlignment(SwingConstants.CENTER); 84 85 // 将各个面板设置为透明,否则看不到背景图片 86 ((JPanel) tw.getContentPane()).setOpaque(false); 87 headPan.setOpaque(false); 88 feaPan.setOpaque(false); 89 btnPan.setOpaque(false); 90 91 // 设置JDialog的整个背景图片 92 tw.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE)); 93 imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight()); 94 headPan.setPreferredSize(new Dimension(300, 60)); 95 96 // 设置提示框的边框,宽度和颜色 97 tw.getRootPane().setBorder( 98 BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray)); 99 100 title.setPreferredSize(new Dimension(260, 26)); 101 title.setVerticalTextPosition(JLabel.CENTER); 102 title.setHorizontalTextPosition(JLabel.CENTER); 103 title.setFont(new Font("宋体", Font.PLAIN, 12)); 104 title.setForeground(Color.black); 105 106 close.setFont(new Font("Arial", Font.BOLD, 15)); 107 close.setPreferredSize(new Dimension(20, 20)); 108 close.setVerticalTextPosition(JLabel.CENTER); 109 close.setHorizontalTextPosition(JLabel.CENTER); 110 close.setCursor(new Cursor(12)); 111 close.setToolTipText("关闭"); 112 113 head.setPreferredSize(new Dimension(250, 35)); 114 head.setVerticalTextPosition(JLabel.CENTER); 115 head.setHorizontalTextPosition(JLabel.CENTER); 116 head.setFont(new Font("宋体", Font.PLAIN, 12)); 117 head.setForeground(Color.blue); 118 119 feature.setEditable(false); 120 feature.setForeground(Color.red); 121 feature.setFont(new Font("宋体", Font.PLAIN, 13)); 122 feature.setBackground(new Color(184, 230, 172)); 123 // 设置文本域自动换行 124 feature.setLineWrap(true); 125 126 jfeaPan.setPreferredSize(new Dimension(250, 80)); 127 jfeaPan.setBorder(null); 128 jfeaPan.setBackground(Color.black); 129 130 releaseLabel.setForeground(Color.DARK_GRAY); 131 releaseLabel.setFont(new Font("宋体", Font.PLAIN, 12)); 132 133 // 为了隐藏文本域,加个空的JLabel将他挤到下面去 134 JLabel jsp = new JLabel(); 135 jsp.setPreferredSize(new Dimension(300, 25)); 136 137 sure.setPreferredSize(new Dimension(110, 30)); 138 // 设置标签鼠标手形 139 sure.setCursor(new Cursor(12)); 140 141 headPan.add(title); 142 headPan.add(close); 143 headPan.add(head); 144 145 feaPan.add(jsp); 146 feaPan.add(jfeaPan); 147 feaPan.add(releaseLabel); 148 149 btnPan.add(sure); 150 151 tw.add(headPan, BorderLayout.NORTH); 152 tw.add(feaPan, BorderLayout.CENTER); 153 tw.add(btnPan, BorderLayout.SOUTH); 154 } 155 156 public void handle() { 157 // 为更新按钮增加相应的事件 158 sure.addMouseListener(new MouseAdapter() { 159 public void mouseClicked(MouseEvent e) { 160 JOptionPane.showMessageDialog(tw, "谢谢,再见"); 161 tw.close(); 162 } 163 164 public void mouseEntered(MouseEvent e) { 165 sure.setBorder(BorderFactory.createLineBorder(Color.gray)); 166 } 167 168 public void mouseExited(MouseEvent e) { 169 sure.setBorder(null); 170 } 171 }); 172 // 增加鼠标拖动事件 173 title.addMouseMotionListener(new MouseMotionAdapter() { 174 public void mouseDragged(MouseEvent e) { 175 // TODO Auto-generated method stub 176 Point newP = new Point(e.getXOnScreen(), e.getYOnScreen()); 177 int x = tw.getX() + (newP.x - oldP.x); 178 int y = tw.getY() + (newP.y - oldP.y); 179 tw.setLocation(x, y); 180 oldP = newP; 181 182 } 183 }); 184 // 鼠标按下时初始坐标,供拖动时计算用 185 title.addMouseListener(new MouseAdapter() { 186 public void mousePressed(MouseEvent e) { 187 oldP = new Point(e.getXOnScreen(), e.getYOnScreen()); 188 } 189 }); 190 191 // 右上角关闭按钮事件 192 close.addMouseListener(new MouseAdapter() { 193 public void mouseClicked(MouseEvent e) { 194 tw.close(); 195 } 196 197 public void mouseEntered(MouseEvent e) { 198 close.setBorder(BorderFactory.createLineBorder(Color.gray)); 199 } 200 201 public void mouseExited(MouseEvent e) { 202 close.setBorder(null); 203 } 204 }); 205 206 } 207 208 public static void main(String args[]) { 209 // new Test(); 210 RemindUtil reminUtil = new RemindUtil(); 211 reminUtil.VersionUtil(); 212 } 213 } 214 215 class TipWindow extends JDialog { 216 private static final long serialVersionUID = 8541659783234673950L; 217 private static Dimension dim; 218 private int x, y; 219 private int width, height; 220 private static Insets screenInsets; 221 222 public TipWindow(int width, int height) { 223 this.width = width; 224 this.height = height; 225 dim = Toolkit.getDefaultToolkit().getScreenSize(); 226 screenInsets = Toolkit.getDefaultToolkit().getScreenInsets( 227 this.getGraphicsConfiguration()); 228 x = (int) (dim.getWidth() - width - 3); 229 y = (int) (dim.getHeight() - screenInsets.bottom - 3); 230 initComponents(); 231 } 232 233 public void run() { 234 for (int i = 0; i <= height; i += 10) { 235 try { 236 this.setLocation(x, y - i); 237 Thread.sleep(5); 238 } catch (InterruptedException ex) { 239 } 240 } 241 // 此处代码用来实现让消息提示框5秒后自动消失 242 // try { 243 // Thread.sleep(5000); 244 // } catch (InterruptedException e) { 245 // // TODO Auto-generated catch block 246 // e.printStackTrace(); 247 // } 248 // close(); 249 } 250 251 private void initComponents() { 252 this.setSize(width, height); 253 this.setLocation(x, y); 254 this.setBackground(Color.black); 255 } 256 257 public void close() { 258 x = this.getX(); 259 y = this.getY(); 260 int ybottom = (int) dim.getHeight() - screenInsets.bottom; 261 for (int i = 0; i <= ybottom - y; i += 10) { 262 try { 263 setLocation(x, y + i); 264 Thread.sleep(5); 265 } catch (InterruptedException ex) { 266 } 267 } 268 dispose(); 269 } 270 271 }
下面为用到的背景图片
本文为转载——编辑后的文章。
--别碰!这只是一只小尾巴! →_→ 会掉的!!!