快速业务通道

Swing中用GlassPane显示一个透明的正在操作框 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-15

Swing中用GlassPane显示一个透明的正在操作框

时间:2011-09-14

这个组件可以让用户看到界面里的显示但是无法操作.

需要的图Swing中用GlassPane显示一个透明的正在操作框 - 编程入门网 loading.gif

代码(main() 方法里是demo代码):

运行效果截图:

import javax.swing.*; import java.awt.BorderLayout; import java.awt.Cursor; import java.awt.FlowLayout; import java.awt.event.*; /** * We have to provide our own glass pane so that it can paint a loading * dialog and then the user can see the progress but can''t operation * the GUI, it''s a transparent pane so the below contents is visible. * * Also please refer to articles by Sun - How to Use Root Panes: * {@link http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.ht ml} * @author Jacky Liu * @version 1.0 2006-08 */ public class LoadingGlassPane extends JPanel { private static final long serialVersionUID = 1L; /** * A label displays status text and loading icon. */ private JLabel statusLabel = new JLabel ("Reading data, please wait..."); public LoadingGlassPane() { try { statusLabel.setIcon(new ImageIcon(getClass ().getResource( "loading.gif"))); } catch (RuntimeException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } statusLabel.setHorizontalAlignment(JLabel.CENTER); // Must add a mouse listener, otherwise the event will not be // captured this.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(MouseEvent e) { } }); this.setLayout(new BorderLayout()); this.add (statusLabel); // Transparent setOpaque(false); } /** * Set the text to be displayed on the glass pane. * * @param text */ public void setStatusText(final String text) { SwingUtilities.invokeLater(new Runnable() { public void run() { statusLabel.setText(text); } }); } /** * Install this to the jframe as glass pane. * @param frame */ public void installAsGlassPane(JFrame frame) { frame.setGlassPane (this); } /** * A small demo code of how to use this glasspane. * @param args */ public static void main(String[] args) { JFrame frame = new JFrame("Test GlassPane"); final LoadingGlassPane glassPane = new LoadingGlassPane (); glassPane.installAsGlassPane(frame); JButton button = new JButton("Test Query"); button.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { // Call in new thread to allow the UI to update Thread th = new Thread() { public void run() { glassPane.setVisible (true); glassPane.setCursor(new Cursor(Cursor.WAIT_CURSOR)); // TODO Long time operation here try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } glassPane.setCursor(new Cursor (Cursor.DEFAULT_CURSOR)); glassPane.setVisible(false); } }; th.start(); } }); frame.getContentPane().setLayout (new FlowLayout()); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (button); frame.setSize(200, 200); frame.setVisible(true); } }

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号