• 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 }
  • 相关阅读:
    品尝阿里云容器服务:负载均衡与容器的关系
    基于微服务架构、运行于容器中的.NET Core示例应用eShopOnContainers
    基于VS2017的Docker Support体检ASP.NET Core站点的Docker部署
    用工厂模式解决ASP.NET Core中依赖注入的一个烦恼
    终于知道什么情况下需要实现.NET Core中的IOptions接口
    ASP.NET Core Web API处理HttpResponseMessage类型返回值的问题
    ASP.NET Core奇遇记:无用户访问,CPU却一直100%
    省一行是一行:在if语句中使用C# 7.0的模式匹配
    ASP.NET Core 2.0 Preview 1 中贴心的新特性
    .NET Core类库项目中如何读取appsettings.json中的配置
  • 原文地址:https://www.cnblogs.com/freshier/p/4696891.html
Copyright © 2020-2023  润新知