Java GlassPane进度条遮罩

 1 package com.swing.demo;
 2 
 3 import java.awt.BorderLayout;
 4 import java.awt.Dimension;
 5 import java.awt.FlowLayout;
 6 import java.awt.Toolkit;
 7 import java.awt.event.MouseAdapter;
 8 import java.awt.event.MouseEvent;
 9 
10 import javax.swing.ImageIcon;
11 import javax.swing.JButton;
12 import javax.swing.JFrame;
13 import javax.swing.JLabel;
14 import javax.swing.JPanel;
15 
16 public class LoadingGlassPane extends JPanel {
17     private static final String ICON_PATH = System.getProperty("user.dir")
18             + "\\icos\\loading.gif";
19     private static final long serialVersionUID = 1L;
20 
21     public LoadingGlassPane() {
22         setLayout(new BorderLayout());
23         setOpaque(false);
24         add(new JLabel(new ImageIcon(ICON_PATH)));
25         addMouseListener(new MouseAdapter() {
26             @Override
27             public void mousePressed(MouseEvent e) {
28                 super.mousePressed(e);
29             }
30         });
31     }
32 
33     public static void main(String[] args) {
34         JFrame frame = new JFrame("GlassPane");
35         LoadingGlassPane glassPane = new LoadingGlassPane();
36         frame.setGlassPane(glassPane);
37         JButton button = new JButton("查询");
38         button.addMouseListener(new GlassMouseAdapter());
39         frame.getContentPane().setLayout(new FlowLayout());
40         frame.getContentPane().add(button);
41         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
42         // 窗体居中
43         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
44         frame.setSize(400, 320);
45         frame.setLocation((d.width - frame.getSize().width) / 2,
46                 (d.height - frame.getSize().height) / 2);
47         frame.setVisible(true);
48     }
49 }
 1 package com.swing.demo;
 2 
 3 import java.awt.Component;
 4 import java.awt.Container;
 5 import java.awt.event.MouseAdapter;
 6 import java.awt.event.MouseEvent;
 7 
 8 import javax.swing.JButton;
 9 import javax.swing.JDialog;
10 import javax.swing.JFrame;
11 
12 public class GlassMouseAdapter extends MouseAdapter {
13     @Override
14     public void mouseClicked(MouseEvent e) {
15         JButton button = (JButton) e.getSource();
16         Container container = (Container) button.getTopLevelAncestor();
17         System.out.println(container.getClass().getName());
18         Component glassPane = null;
19         if (container instanceof JFrame) {
20             glassPane = ((JFrame) container).getGlassPane();
21         } else if (container instanceof JDialog) {
22             glassPane = ((JDialog) container).getGlassPane();
23         }
24 
25         doClick(button, glassPane);
26     }
27 
28     private void doClick(JButton button, Component glassPane) {
29         if (null == glassPane) {
30             return;
31         }
32         new Thread() {
33             public void run() {
34                 button.setEnabled(false);
35                 glassPane.setVisible(true);
36                 try {
37                     Thread.sleep(2000);
38                 } catch (InterruptedException e) {
39                     e.printStackTrace();
40                 }
41                 glassPane.setVisible(false);
42                 button.setEnabled(true);
43             }
44         }.start();
45     }
46 }

 

posted @ 2015-08-02 22:14  freshier  阅读(1456)  评论(0编辑  收藏  举报