Idea Analysis:
Because there must be both an operation panel and a progress bar, two forms that inherit the JFrame class must appear.
First look at the called progress bar form. It does not require manual operation, so you can implement a method inside the class. Because of design file operations, exceptions need to be caught. First create a File object based on the file to be copied, and create a File object based on the saved address of the copied file, then create a FileOutputStream object, then create a FileInputStream object, followed by a ProgressMonitorInputStream object, and then read the file. If the total time takes more than 2 seconds, A progress monitoring window will automatically pop up. Next, define the byte array, then use the while loop to read the file, use the write() method of FileOutputStream class to write data through the stream, then use the close() method of FileOutputStream class to close the output stream, and finally use the close() method of ProgressMonitorInputStream class to close Input stream. It can be seen that this method requires three parameters: its parent window pops up, the file address to be copied, and the folder to be copied to.
The code is as follows:
ProgressMonitorTest.java:
The code copy is as follows:
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); // Create File object based on the file to be copied
File newFile = new File(newPath); // Create File object based on the saved address of the copied file
FileOutputStream fop = new FileOutputStream(newFile); // Create FileOutputStream object
InputStream in = new FileInputStream(file);
// Read the file. If it takes more than 2 seconds in total, a progress monitoring window will automatically pop up.
ProgressMonitorInputStream pm = new ProgressMonitorInputStream(
frame, "File reading, please wait...", in);
int c = 0;
byte[] bytes = new byte[1024]; // Define the byte array
while ((c = pm.read(bytes)) != -1) { // Loop to read the file
fop.write(bytes, 0, c); // Write data through stream
}
fop.close(); // Close the output stream
pm.close(); // Close the input stream
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3. Look at the main form again. Needless to say, JLabel and JTextField are the two buttons, selecting files and selecting folders, are also common. The most important thing is the "Start Copy" button, which will be responsible for popping up this progress bar, which requires opening up a new one. thread, so the main form must not only inherit the JFrame class, but also implement the Runnable interface. His uncle's.
4. In the specific method of starting copy button, first create a Thread object as a new thread, then call the start() method of the object, overload the run() method, create a progress bar object in the method, use The getText() method of the JTextField class gets the file address to be copied and the path to be copied to, and then calls the method in the progress bar class.
The code is as follows:
The code copy is as follows:
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 = 8674569541853793419L;
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("Use progress bar when reading a file");
getContentPane().setLayout(null);
JLabel label = new JLabel("/u6587/u4EF6/u5730/u5740/uFF1A");
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("/u9009/u62E9/u6587/u4EF6");
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("/u590D/u5236/u5730/u5740/uFF1A");
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("/u9009/u62E9/u5730/u5740");
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("/u5F00/u59CB/u590D/u5236");
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);
// Show file opening dialog box
int option = chooser.showOpenDialog(this);
// Make sure the user presses the Open button instead of the Cancel button
if (option != JFileChooser.APPROVE_OPTION)
return;
// Get the file object selected by the user
file = chooser.getSelectedFile();
// Show file information to the text box
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());
}
//Confirm the copy button click event
protected void do_copyButton_actionPerformed(ActionEvent arg0) {
Thread thread = new Thread(this);
thread.start();
}
//Use multi-threading technology to realize read operation
@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()));
}
}