进度条小伙伴们应该都见过吧,这是为了让我们知道当前的加载进度而制作的,这次我们就来看看要如何用java语言编写一个进度条。
示例:复制文件时显示进度
ProgressMonitorTest.java
package cn.edu.xidian.crytoll; import java.io.FileInputStream; import java.io.*; import javax.swing.JFrame; import javax.swing.ProgressMonitorInputStream; public class ProgressMonitorTest { public void useProgressMonitor(JFrame frame, String copyPath, String newPath) { try { File file = new File(copyPath); // 根据要复制的文件创建File对象 File newFile = new File(newPath); // 根据复制后文件的保存地址创建File对象 FileOutputStream fop = new FileOutputStream(newFile); // 创建FileOutputStream对象 InputStream in = new FileInputStream(file); // 读取文件,如果总耗时超过2秒,将会自动弹出一个进度监视窗口。 ProgressMonitorInputStream pm = new ProgressMonitorInputStream( frame, "文件读取中,请稍后...", in ); int c = 0; byte[] bytes = new byte[1024]; // 定义byte数组 while ((c = pm.read(bytes)) != -1) { // 循环读取文件 fop.write(bytes, 0, c); // 通过流写数据 } fop.close(); // 关闭输出流 pm.close(); // 关闭输入流 } catch (Exception ex) { ex.printStackTrace(); } } }
在复制按钮的具体方法里,需要创建一个Thread对象作为新的线程,再调用该对象的start()方法,重载执行run()方法,在该方法中创建一个进度条对象,使用JTextField类的getText()方法获取要复制的文件地址和要复制到的路径,然后调用进度条类里的方法。
代码:
package cn.edu.xidian.crytoll; import java.awt.BorderLayout; import java.awt.Desktop; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import javax.swing.filechooser.FileNameExtensionFilter; public class UserMonitorFrame extends JFrame implements Runnable { /** * */ private static final long serialVersionUID = 8674569541853793419 L; private JPanel contentPane; private JTextField fileField; private JTextField searchTextField; private JTextField replaceTextField; private File file; private JTextField textField; private JTextField textField_1; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { UserMonitorFrame frame = new UserMonitorFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public UserMonitorFrame() { setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 501, 184); setTitle("在读取文件时使用进度条"); getContentPane() .setLayout(null); JLabel label = new JLabel("文件地址:"); label.setBounds(10, 10, 70, 15); getContentPane() .add(label); textField = new JTextField(); textField.setBounds(90, 7, 300, 21); getContentPane() .add(textField); textField.setColumns(10); JButton button = new JButton("选择文件"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { do_button_actionPerformed(e); } }); button.setBounds(400, 6, 93, 23); getContentPane() .add(button); JLabel label_1 = new JLabel("复制地址:"); label_1.setBounds(10, 40, 70, 15); getContentPane() .add(label_1); textField_1 = new JTextField(); textField_1.setBounds(90, 38, 300, 21); getContentPane() .add(textField_1); textField_1.setColumns(10); JButton button_1 = new JButton("选择地址"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { do_button_1_actionPerformed(e); } }); button_1.setBounds(400, 39, 93, 23); getContentPane() .add(button_1); JButton button_2 = new JButton("开始复制"); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { do_copyButton_actionPerformed(e); } }); button_2.setBounds(182, 69, 93, 23); getContentPane() .add(button_2); } protected void do_button_actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); // 显示文件打开对话框 int option = chooser.showOpenDialog(this); // 确定用户按下打开按钮,而非取消按钮 if (option != JFileChooser.APPROVE_OPTION) return; // 获取用户选择的文件对象 file = chooser.getSelectedFile(); // 显示文件信息到文本框 textField.setText(file.toString()); } protected void do_button_1_actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int option = chooser.showOpenDialog(this); if (option != JFileChooser.APPROVE_OPTION) return; file = chooser.getSelectedFile(); textField_1.setText(file.toString()); } //确定复制按钮单击事件 protected void do_copyButton_actionPerformed(ActionEvent arg0) { Thread thread = new Thread(this); thread.start(); } //应用多线程技术实现读取操作 @Override public void run() { ProgressMonitorTest test = new ProgressMonitorTest(); String path = textField.getText(); String save = textField_1.getText(); test.useProgressMonitor(this, path, save + path.substring(path.lastIndexOf("."), path.length())); } }
以上就是本篇文章的所有内容,相信对进度条小伙伴们已经非常了解了,对其他相关java程序代码例子还有需要的话可以关注我们了解详情。
推荐阅读: