C/S中进度条使用

使用下面这个类就可以使用进度条,j方便在C/S端中调用

package com.zhenghan;

import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
/*
 * 进度条的使用
 */
public class ProgressCustomDialog implements ActionListener {

	private static final String DEFAULT_STATUS = "Please Waiting";

	private static JDialog dialog;

	private static JProgressBar progressBar;

	public static JLabel lbStatus;

	private static JButton btnCancel;

	private static Thread thread; // 处理业务的线程,如果没有线程,则这条不需要

	private static String statusInfo;

	public static String resultInfo;

	public static String cancelInfo;

	private static Dialog parentDlg;
	private static  JPanel mainPane;

	String message = "";

	public static void show(Dialog parentDlg) {

		new ProgressCustomDialog(DEFAULT_STATUS, null, null, parentDlg);
	}

	public static void show(String statusInfo, Dialog parentDlg) {
		new ProgressCustomDialog(statusInfo, null, null, parentDlg);
	}

	public static void show(String statusInfo, String resultInfo, String cancelInfo, Dialog parentDlg) {
		new ProgressCustomDialog(statusInfo, resultInfo, cancelInfo, parentDlg);
	}

	private ProgressCustomDialog(String statusInfo, String resultInfo, String cancelInfo, Dialog parentDlg) {

		this.statusInfo = statusInfo;// Status
		this.resultInfo = resultInfo;// 处理完毕
		this.cancelInfo = cancelInfo;// Cancel
		this.parentDlg = parentDlg;

	}

	
	public ProgressCustomDialog() {
		
		System.out.println("123");
	}

	
	
	//加载进度条
	public void initUI() {
		if (parentDlg == null) {
			dialog = new JDialog();
		} else {
			dialog = new JDialog(parentDlg);
		}
		mainPane = new JPanel(null);
		progressBar = new JProgressBar();
		lbStatus = new JLabel();
		lbStatus.setText("正在更新 ....");
		btnCancel = new JButton("Cancel");
		progressBar.setIndeterminate(true);
		btnCancel.addActionListener(this);

		mainPane.add(progressBar);
		mainPane.add(lbStatus);
		// mainPane.add(btnCancel);
		dialog.getContentPane().add(mainPane);
		// dialog.setUndecorated(true); // 除去title
		dialog.setResizable(true);
		dialog.setAlwaysOnTop(true);
		dialog.setSize(590, 100);
		dialog.setLocationRelativeTo(null); // 设置此窗口相对于指定组件的位置
		dialog.setTitle("进度信息");
		dialog.requestFocusInWindow();
		dialog.validate();
		dialog.addWindowListener(new WindowAdapter() {
			public void windowClosed(WindowEvent e) {
			}

			public void windowClosing(WindowEvent e) {
				//     MessageBox.post("提醒",
				//       "关闭该窗口后报表将无法生成。", MessageBox.INFORMATION);
				dialog.dispose();
				// thread.stop();
			}
		});
		mainPane.addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
				layout(mainPane.getWidth(), mainPane.getHeight());
			}
		});
		
		dialog.setVisible(true);
		
		
	}

	//进度条完成后,出现提示
	void startThread() {
		dialog.setVisible(false);//关闭进度条
		String title = "进度信息";
		JOptionPane.showMessageDialog(new Dialog(dialog), "系统更新成功", title, JOptionPane.INFORMATION_MESSAGE);

	}

	private static void layout(int width, int height) {
		progressBar.setBounds(20, 20, 550, 15);
		lbStatus.setBounds(20, 40, 550, 25);
		btnCancel.setBounds(width - 85, height - 31, 75, 21);
	}

	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e) {
		resultInfo = cancelInfo;
		thread.stop();
	}

	public static void StopStatus() {
		if (dialog != null)
			dialog.dispose();
	}

	// test
	// public static void main(String[] args) throws Exception {
	// ProgressCustomDialog p = new ProgressCustomDialog();
	// }

	public Dialog getParentDlg() {
		return parentDlg;
	}

	public void setParentDlg(Dialog parentDlg) {
		this.parentDlg = parentDlg;
	}

	public static JDialog getDialog() {
		return dialog;
	}

	public static void setDialog(JDialog dialog) {
		ProgressCustomDialog.dialog = dialog;
	}

}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值