Code Example: A Progress Bar of a Read File

xiaoxiao2021-03-06  60

? / ** * @ (#) TestProgressBar.java?*?* Copyright 2004 Opensource Develop Team All rights reserved.?*/// packagepackage com.opensource.progressbar;. // import classesimport java.awt.FlowLayout; import java .awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileInputStream; import java.io.InputStream; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.ProgressMonitorInputStream ; / **? * When a big file is read, you need to display a progress bar prompting that the user has to read and have been read? * How much content is read. ? *? * @AUTHOR ODT? * @Version 1.0 01/05/2004? * @since jdk 1.3? * / public class testprogressbar {??? public static void main (String [] args)?? {??? ?? // Create a Frame???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.? ?????????? F.getContentPane (). Add (b) ;????? F.pack () ;????? ? // set up the file read action?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ????? // Start a thread to read the file when the Button presses.

??? // Because we need an idle thread to update the progress monitor???????????????? ???????????????????????????????????????????????????????????????????????????????? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? // open a file, encapsulated in ProgressMonitorInputStream InputStream in = new FileInputStream????????????????????? ( "D: // Java projects // mail.txt") ;????????????????????? ProgressMonitorInputStream PM = new new???????????????????????? ProgressMonitorInputStream (f, "Reading the big file", in);? // read the file. If you take a long time, Progress Monitor will appear. ????????????????????? Int c ;????????????????????? While ((c = PM.Read ())! = -1)??????????????????? ?????????????????????????????????????? ?? pm.close (); // Can rewrite a better error handling????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????? • EX.PrintStackTrace ();??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ????????? // Show frame???? f. SetDefaultCloseOperation (jframe.exit_on_close);???? f.setvisible (TRUE);??}}

? / ** * @ (#) TestProgressBar.java?*?* Copyright 2004 Opensource Develop Team All rights reserved.?*/// packagepackage com.opensource.progressbar;. // import classesimport java.awt.FlowLayout; import java .awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileInputStream; import java.io.InputStream; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.ProgressMonitorInputStream ; / **? * When a big file is read, you need to display a progress bar prompting that the user has to read and have been read? * How much content is read. ? *? * @AUTHOR ODT? * @Version 1.0 01/05/2004? * @since jdk 1.3? * / public class testprogressbar {??? public static void main (String [] args)?? {??? ?? // Create a Frame???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.? ?????????? F.getContentPane (). Add (b) ;????? F.pack () ;????? ? // set up the file read action?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ????? // Start a thread to read the file when the Button presses.

转载请注明原文地址:https://www.9cbs.com/read-119347.html

New Post(0)