java实现gui界面的文件复制

代码中含有注释
如果自己要写文件复制的代码,建议流程
1、写一个简单的程序完成文件复制
2、利用JProgressBar完成进度条的设置,以及进度条的刷新展示
3、gui界面的TtextArea以及button十分简单,不解释!你懂得!
4、将上述步骤串在一起就可,这里注意thread running不能一直跑,要留给程序一定的时间去检测copy,也就是thread running per 1000ms,时间太短或者说不设置的话会出问题的,自己去理解,不做过多解释

package homework;

import java.lang.*;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

//@author=BUPT:huanglin
public class File_Copy extends Frame  implements ActionListener{
   

    public int rate = 0;
    String old_filepath;
    String new_filepath;
    private JProgressBar pro = new JProgressBar();   
    JPanel frame;
    Button copy = new Button("COPY");
    JTextArea F
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以帮你解答这个问题。 首先,我们需要使用Java的Swing库创建GUI界面。在GUI中,我们需要使用以下至少8个Swing组件: 1. JLabel:用于显示文本或图像。 2. JTextField:用于输入文本。 3. JButton:用于触发复制操作。 4. JFileChooser:用于选择要复制文件。 5. JProgressBar:用于显示复制进度。 6. JTextArea:用于显示复制日志。 7. JCheckBox:用于选择是否覆盖目标文件。 8. JComboBox:用于选择复制模式(例如:单个文件复制文件复制等)。 下面是一个简单的Java代码示例,用于实现GUI文件复制功能: ```java import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class FileCopyGUI extends JFrame implements ActionListener { private JLabel sourceLabel, targetLabel, modeLabel; private JTextField sourceField, targetField; private JButton copyButton; private JFileChooser fileChooser; private JProgressBar progressBar; private JTextArea logArea; private JCheckBox overwriteCheckBox; private JComboBox<String> modeComboBox; public FileCopyGUI() { super("文件复制工具"); // 创建GUI界面 sourceLabel = new JLabel("源文件:"); targetLabel = new JLabel("目标文件:"); modeLabel = new JLabel("复制模式:"); sourceField = new JTextField(20); targetField = new JTextField(20); copyButton = new JButton("复制"); fileChooser = new JFileChooser(); progressBar = new JProgressBar(); logArea = new JTextArea(10, 30); overwriteCheckBox = new JCheckBox("覆盖目标文件"); modeComboBox = new JComboBox<String>(new String[] { "单个文件复制", "文件复制" }); // 设置GUI布局 JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.LINE_END; panel.add(sourceLabel, c); c.gridx = 1; c.anchor = GridBagConstraints.LINE_START; panel.add(sourceField, c); c.gridx = 0; c.gridy = 1; c.anchor = GridBagConstraints.LINE_END; panel.add(targetLabel, c); c.gridx = 1; c.anchor = GridBagConstraints.LINE_START; panel.add(targetField, c); c.gridx = 0; c.gridy = 2; c.anchor = GridBagConstraints.LINE_END; panel.add(modeLabel, c); c.gridx = 1; c.anchor = GridBagConstraints.LINE_START; panel.add(modeComboBox, c); c.gridx = 0; c.gridy = 3; c.anchor = GridBagConstraints.LINE_END; panel.add(overwriteCheckBox, c); c.gridx = 1; c.anchor = GridBagConstraints.LINE_START; panel.add(copyButton, c); c.gridx = 0; c.gridy = 4; c.gridwidth = 2; c.fill = GridBagConstraints.HORIZONTAL; panel.add(progressBar, c); c.gridx = 0; c.gridy = 5; c.gridwidth = 2; c.fill = GridBagConstraints.BOTH; panel.add(new JScrollPane(logArea), c); setContentPane(panel); pack(); // 添加事件监听器 copyButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == copyButton) { copyFiles(); } } private void copyFiles() { // 获取复制模式和源文件路径 String mode = (String) modeComboBox.getSelectedItem(); String sourcePath = sourceField.getText(); // 根据复制模式选择目标文件路径 String targetPath; if (mode.equals("单个文件复制")) { targetPath = targetField.getText(); } else { targetPath = targetField.getText() + "/" + new File(sourcePath).getName(); } // 创建文件复制线程 FileCopyThread thread = new FileCopyThread(sourcePath, targetPath, overwriteCheckBox.isSelected(), progressBar, logArea); // 开始复制文件 thread.start(); } public static void main(String[] args) { FileCopyGUI gui = new FileCopyGUI(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setVisible(true); } } class FileCopyThread extends Thread { private String sourcePath, targetPath; private boolean overwrite; private JProgressBar progressBar; private JTextArea logArea; public FileCopyThread(String sourcePath, String targetPath, boolean overwrite, JProgressBar progressBar, JTextArea logArea) { this.sourcePath = sourcePath; this.targetPath = targetPath; this.overwrite = overwrite; this.progressBar = progressBar; this.logArea = logArea; } public void run() { try { File sourceFile = new File(sourcePath); File targetFile = new File(targetPath); // 检查目标文件是否存在 if (targetFile.exists() && !overwrite) { logArea.append("目标文件已存在,复制操作被取消。\n"); return; } // 创建目标文件的父文件夹 if (!targetFile.getParentFile().exists()) { targetFile.getParentFile().mkdirs(); } // 复制文件 FileInputStream in = new FileInputStream(sourceFile); FileOutputStream out = new FileOutputStream(targetFile); byte[] buffer = new byte[1024]; int length; long copied = 0; long total = sourceFile.length(); while ((length = in.read(buffer)) != -1) { out.write(buffer, 0, length); copied += length; progressBar.setValue((int) (copied * 100 / total)); } in.close(); out.close(); logArea.append("文件已成功复制到 " + targetPath + "。\n"); } catch (Exception e) { logArea.append("文件复制失败:" + e.getMessage() + "\n"); } } } ``` 这个程序实现了一个简单的文件复制工具,可以进行单个文件复制文件复制GUI界面包含了8个以上的Swing组件,包括JLabel、JTextField、JButton、JFileChooser、JProgressBar、JTextArea、JCheckBox和JComboBox。当用户点击复制按钮时,程序会在后台启动一个线程复制文件,并在GUI界面上显示复制进度和日志。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值