import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; /** * 進(jìn)度條窗體界面設(shè)計 * @author dell * */ public class ProgressFrame extends JFrame { JPanel panel1; JButton button1; GridBagConstraints gridBagConstraints1; ProgressPane progresspane; public ProgressFrame() { super(); setTitle("進(jìn)度條"); setBounds(100, 100, 500, 120); setLayout(new BorderLayout()); printFrame(); this.setVisible(true); this.setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void printFrame(){ progresspane = new ProgressPane(); button1 = new JButton("隱藏"); panel1 = new JPanel(); panel1.setLayout(new GridBagLayout()); gridBagConstraints1 = new GridBagConstraints(); gridBagConstraints1.gridy = 0; gridBagConstraints1.gridx = 0; panel1.add(button1, gridBagConstraints1); getContentPane().add(panel1, BorderLayout.CENTER); getContentPane().add(progresspane, BorderLayout.NORTH);
// 事件監(jiān)聽 button1.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { hide(); } }); } }
import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar;
/** * Java進(jìn)度條 * @author dell * */ public class ProgressPane extends JPanel{
public JLabel label; public JProgressBar progressBar;
public ProgressPane() { super(); setLayout(new GridBagLayout()); setSize(394, 33);
progressBar = new JProgressBar(0,100); progressBar.setStringPainted(true); final GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.weightx = 1.0; gridBagConstraints.ipady = 1; //進(jìn)度條的寬度 gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.weighty = 1.0; gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.ipadx = 75; gridBagConstraints.insets = new Insets(4, 0, 0, 0); add(progressBar, gridBagConstraints);
label = new JLabel(); final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints(); add(label, gridBagConstraints_1); label.setText("進(jìn)度顯示"); gridBagConstraints_1.gridx = 1; gridBagConstraints_1.gridy = 0; gridBagConstraints_1.ipadx = 54; gridBagConstraints_1.insets = new Insets(0, 12, 10, 0); }
}
/** * 提取使用過程 * @author shk * */ public class UseProgress{
ProgressFrame progress; public UseProgress(){ progress=new ProgressFrame(); } public void printProgress(String msg,int num){ progress.progresspane.progressBar.setValue(num); progress.progresspane.progressBar.setString(msg+num+"%"); } }
/** * 測試類 * @author SHK * */ public class Test {
public static void main(String[] args) { new Test().test(); } public void test(){ UseProgress use=new UseProgress(); for(int i=0;i< =100;i++){ use.printProgress("正在上傳文件... ",i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } use.progress.hide();//隱藏進(jìn)度條 } }
|