package com.vci.client.ui.process; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar; public class QANProcessBar extends JDialog implements Runnable { /** * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) */ private static final long serialVersionUID = 7120183499441604220L; public static JFrame frame = new JFrame(); private Thread thread = null; private String title = null; private String content = null; private JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL,0,100); private JLabel detailLabel = new JLabel(); private JButton startButton = new JButton("开始"); private JButton cancelButton = new JButton("取消"); private ProcessBarInterface pbi = null; private Thread taskThread = null; private boolean enableCancelButton = false; public QANProcessBar(Thread thread, JFrame frame, ProcessBarInterface pbi, boolean enableCancelButton) { super(frame); this.thread = thread; this.title = pbi.getProcessBarTitle(); this.content = pbi.getProcessBarContent(); this.pbi = pbi; this.enableCancelButton = enableCancelButton; init(); startTread(); } public QANProcessBar(Thread thread, JFrame frame, ProcessBarInterface pbi,String title, boolean enableCancelButton) { super(frame); this.thread = thread; this.title = title; this.content = pbi.getProcessBarContent(); this.pbi = pbi; this.enableCancelButton = enableCancelButton; init(); startTread(); } public QANProcessBar(Thread thread, JDialog dialog, ProcessBarInterface pbi, boolean enableCancelButton) { super(dialog); this.thread = thread; this.title = pbi.getProcessBarTitle(); this.content = pbi.getProcessBarContent(); this.pbi = pbi; this.enableCancelButton = enableCancelButton; init(); startTread(); } public void init() { this.setTitle(title); this.setSize(new Dimension(pbi.getProcessBarDialogWidth(),pbi.getProcessBarDialogHeight())); this.setLayout(new BorderLayout()); this.setResizable(false); this.setModal(true); this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-400)/2, (Toolkit.getDefaultToolkit().getScreenSize().height-100)/2); detailLabel.setFont(pbi.getContentFont()); detailLabel.setForeground(pbi.getContentColor()); detailLabel.setPreferredSize(new Dimension(550,25)); detailLabel.setText(content); progressBar.setForeground(Color.green); JPanel topPanel = new JPanel(); topPanel.setPreferredSize(new Dimension(550, 25)); topPanel.setLayout(new BoxLayout(topPanel,BoxLayout.Y_AXIS)); topPanel.add(progressBar); startButton.setVisible(pbi.isShowStartButton()); cancelButton.setEnabled(this.enableCancelButton); cancelButton.setVisible(pbi.isShowCancelButton()); cancelButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { cancelButtonActionListener(); }}); this.setLayout(new GridBagLayout()); this.add(topPanel, new GridBagConstraints(0, 0, 3, 1, 0.0, 1.0 ,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 10, 0, 0), 0, 0)); this.add(detailLabel, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(10, 10, 0, 0), 0, 0)); this.add(startButton, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); this.add(new JLabel(""), new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 10, 0, 0), 0, 0)); this.add(cancelButton, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); this.add(new JLabel(), new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0 ,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(10, 10, 0, 0), 0, 0)); this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); } private void cancelButtonActionListener() { pbi.setProcessBarCancel(true); } public void run() { while(!pbi.getProcessBarCancel()) { if (pbi.isProcessSure()) { progressBar.setValue(pbi.getProcessBarValue()); detailLabel.setText(pbi.getProcessBarContent()); } else { progressBar.setIndeterminate(true); detailLabel.setText(pbi.getProcessBarContent()); } } this.dispose(); } public void startTread() { Thread t = new Thread(this); t.start(); taskThread = new Thread(){ public void run() { try { thread.start(); thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } }; }; taskThread.start(); } }