程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Swing中用GlassPane顯示一個透明的正在操作框

Swing中用GlassPane顯示一個透明的正在操作框

編輯:關於JAVA

這個組件可以讓用戶看到界面裡的顯示但是無法操作.

需要的圖 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);
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved