- 註冊時間
- 2006-3-13
- 最後登錄
- 2025-1-10
- 在線時間
- 673 小時
- 閱讀權限
- 200
- 積分
- 417
- 帖子
- 1107
- 精華
- 0
- UID
- 2
  
|
具有進度條的程式碼
- package test;
- import javax.swing.*;
- import java.awt.*;
- import com.borland.jbcl.layout.*;
- import javax.swing.border.*;
- class ProgressPanel extends JPanel {
- JLabel jTip = new JLabel();
- JProgressBar jProgress = new JProgressBar();
- XYLayout xYLayout1 = new XYLayout();
- TitledBorder titledBorder1;
- public ProgressPanel() {
- try {
- jbInit();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- private void jbInit() throws Exception {
- titledBorder1 = new TitledBorder("");
- jTip.setText("處理中...");
- jTip.setFont(new java.awt.Font("Dialog", 0, 12));
- this.setLayout(xYLayout1);
- jProgress.setOrientation(JProgressBar.HORIZONTAL);
- jProgress.setForeground(new Color(0, 0, 105));
- jProgress.setBorder(BorderFactory.createLoweredBevelBorder());
- jProgress.setStringPainted(true);
- xYLayout1.setWidth(258);
- xYLayout1.setHeight(75); bitsCN.Com
- this.setDebugGraphicsOptions(0);
- this.add(jTip, new XYConstraints(10, 1, -1, -1));
- this.add(jProgress, new XYConstraints(5, 16, 249, 24));
- }
- public static void main(String[] args) {
- ProgressPanel progressPanel1 = new ProgressPanel();
- }
- }
複製代碼- package test;
- import java.awt.Window;
- import java.awt.Dimension;
- import java.awt.Toolkit;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import java.awt.Rectangle;
- import javax.swing.SwingConstants;
- import java.awt.GridBagLayout;
- public class WaitingFrame {
- protected static Window winInc;
- private static WaitingFrame inc;
- private static JLabel jWaitTip;
- private static ProgressPanel proPanel;
- private static final String WAITING_TIP = "系統正在處理中,請稍後...";
- private static final String PROGRESS_TIP = "處理中...";
- private static final int DEFAULT_WIDTH = 260;
- private static final int DEFAULT_HEIGHT = 45;
- private static int DEFAULT_LOCATEX;
- private static int DEFAULT_LOCATEY;
- private static boolean resetFlg = false;
- private static int maxProgress = 100;
- //當前顯示的視窗類型
- private int curType;
- /**
- * 等待視窗
- */
- public static final int WAITING = 0;
- /**
- * 進度條視窗
- */
- public static final int PROGRESS = 1;
- private WaitingFrame() {
- }
- /**
- * 獲取提示視窗實例
- * @param aType:視窗類型
- * @return
- */
- public synchronized static WaitingFrame getInstance(int aType){
- if(aType==PROGRESS){
- if(null==proPanel){
- proPanel = new ProgressPanel();
- }
- }else{
- if(null==jWaitTip){
- jWaitTip = new JLabel();
- jWaitTip.setFont(new java.awt.Font("Dialog", 0, 16));
- jWaitTip.setHorizontalAlignment(SwingConstants.CENTER);
- jWaitTip.setText(WAITING_TIP);
- jWaitTip.setBounds(new Rectangle(0, 0, 280, 98));
- }
- }
- if(null==inc){
- inc = new WaitingFrame();
- JFrame frm = new JFrame();
- frm.getContentPane().setLayout(new GridBagLayout());
- winInc = new Window(frm);
- if(aType==PROGRESS){
- inc.curType = aType;
- proPanel.jProgress.setMaximum(maxProgress);
- proPanel.jProgress.setValue(0);
- winInc.add(proPanel);
- }else{
- inc.curType = WAITING;
- winInc.add(jWaitTip);
- }
- winInc.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
- Dimension sysSize = Toolkit.getDefaultToolkit().getScreenSize();
- DEFAULT_LOCATEX = (sysSize.width-DEFAULT_WIDTH)/2;
- DEFAULT_LOCATEY = (sysSize.height-DEFAULT_HEIGHT)/2;
- }else{
- if (aType == PROGRESS) {
- winInc.removeAll();
- proPanel.jTip.setText(PROGRESS_TIP);
- proPanel.jProgress.setValue(0);
- winInc.add(proPanel);
- }else{
- winInc.removeAll();
- jWaitTip.setText(WAITING_TIP);
- winInc.add(jWaitTip);
- }
- }
- inc.curType = aType;
- inc.resetFlg = false;
- return inc;
- }
- /**
- * 關閉顯示的等待視窗
- */
- public void close(){
- if(null != winInc && winInc.isShowing())
- winInc.dispose();
- }
- /**
- * 關閉顯示的等待視窗
- */
- public void dispose(){
- close();
- }
- /**
- * 設置提示文本
- * @param aTip:提示文本
- */
- public void setTip(String aTip){
- if(this.curType==PROGRESS){
- proPanel.jTip.setText(aTip);
- }else{
- jWaitTip.setText(aTip);
- }
- resetFlg = true;
- winInc.validate();
- }
- /**
- * 設置進度條的最大值
- * @param aValue 最大值
- */
- public void setMaxValue(int aValue){
- if (curType == PROGRESS) {
- if(aValue>0)
- maxProgress = aValue;
- else
- maxProgress = 100;
- proPanel.jProgress.setMaximum(maxProgress);
- }
- else {
- throw new RuntimeException("等待視窗不支持此函數,只有進度條視窗才支持!");
- }
- }
- /**
- * 設置當前進度
- * @param aValue 當前進度值
- */
- public void setProgress(int aValue){
- proPanel.jProgress.setValue(aValue); bitscn.com
- winInc.validate();
- }
- /**
- * 顯示視窗
- * @param aTipText 文本提示
- * @param aLocx 視窗位置x
- * @param aLocy 視窗位置y
- */
- public void show(String aTipText,int aLocx,int aLocy){
- setTip(aTipText);
- winInc.setLocation(aLocx,aLocy);
- winInc.show();
- }
- /**
- * 顯示視窗
- * @param aTipText 文本提示
- */
- public void show(String aTipText){ www_bitscn_com
- show(aTipText,this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
- }
- /**
- * 顯示視窗
- * @param aLocx 視窗位置x
- * @param aLocy 視窗位置y
- */
- public void show(int aLocx,int aLocy){
- if(curType==PROGRESS)
- show(this.PROGRESS_TIP,aLocx,aLocy);
- else
- show(this.WAITING_TIP,aLocx,aLocy);
- }
- /**
- * 顯示視窗
- */
- public void show(){
- if(resetFlg){
- if(curType==PROGRESS)
- show(proPanel.jTip.getText(),this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
- else
- show(jWaitTip.getText(),this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
- }else{
- if(curType==PROGRESS)
- show(this.PROGRESS_TIP,this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
- else
- show(this.WAITING_TIP,this.DEFAULT_LOCATEX,this.DEFAULT_LOCATEY);
- }
- }
- public static void main(String[] args) {
- //進度條測試
- WaitingFrame frm = WaitingFrame.getInstance(WaitingFrame.PROGRESS);
- frm.setMaxValue(100);
- frm.show();
- int i = 0;
- while (i++ < 100) {
- frm.setProgress(i);
- try {
- Thread.sleep(100);
- }
- catch (InterruptedException ex) {
- }
- }
- frm.dispose();
- //等待視窗測試
- frm = WaitingFrame.getInstance(WaitingFrame.WAITING);
- frm.show();
- try {
- Thread.sleep(10000);
- }
- catch (InterruptedException ex1) {
- }
- frm.dispose();
- }
- }
複製代碼 |
|