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

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

需要的图: http://www.blogjava.net/images/blogjava_net/beansoft/20272/o_loading.gif 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.html}
* @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);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值