将文件复制功能实现GUI,图形用户界面需要包含8个以上的Swing组件,代码
时间: 2024-02-28 15:53:57 浏览: 70
好的,以下是一个简单的Java Swing GUI,用于实现文件复制功能。它包含8个以上的Swing组件,并使用Java的IO类来实现文件复制功能。请注意,以下代码只是一个示例,您可以根据需要进行修改和定制。
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class FileCopyGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel labelSource;
private JLabel labelTarget;
private JLabel labelProgress;
private JTextField textSource;
private JTextField textTarget;
private JButton buttonSource;
private JButton buttonTarget;
private JButton buttonCopy;
private JProgressBar progressBar;
private JTextArea textLog;
private JCheckBox checkOverwrite;
private JComboBox<String> comboMode;
private JRadioButton radioSourceToTarget;
private JRadioButton radioTargetToSource;
public FileCopyGUI() {
// 设置窗口标题和大小
setTitle("文件复制GUI");
setSize(500, 400);
// 创建Swing组件
labelSource = new JLabel("源文件:");
labelTarget = new JLabel("目标文件:");
labelProgress = new JLabel("复制进度:");
textSource = new JTextField(20);
textTarget = new JTextField(20);
buttonSource = new JButton("选择");
buttonTarget = new JButton("选择");
buttonCopy = new JButton("复制");
progressBar = new JProgressBar(0, 100);
textLog = new JTextArea(10, 30);
checkOverwrite = new JCheckBox("覆盖目标文件");
comboMode = new JComboBox<String>(new String[] { "普通", "快速", "增量" });
radioSourceToTarget = new JRadioButton("从源到目标", true);
radioTargetToSource = new JRadioButton("从目标到源", false);
// 创建按钮组,用于排列两个单选按钮
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioSourceToTarget);
buttonGroup.add(radioTargetToSource);
// 创建面板并添加Swing组件
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
panel.add(labelSource, c);
c.gridx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(textSource, c);
c.gridx = 2;
panel.add(buttonSource, c);
c.gridx = 0;
c.gridy = 1;
panel.add(labelTarget, c);
c.gridx = 1;
panel.add(textTarget, c);
c.gridx = 2;
panel.add(buttonTarget, c);
c.gridx = 0;
c.gridy = 2;
panel.add(checkOverwrite, c);
c.gridx = 1;
panel.add(comboMode, c);
c.gridx = 2;
panel.add(radioSourceToTarget, c);
c.gridx = 3;
panel.add(radioTargetToSource, c);
c.gridx = 0;
c.gridy = 3;
panel.add(buttonCopy, c);
c.gridwidth = 4;
c.gridx = 0;
c.gridy = 4;
panel.add(labelProgress, c);
c.gridy = 5;
panel.add(progressBar, c);
c.gridy = 6;
panel.add(new JScrollPane(textLog), c);
// 添加事件监听器
buttonSource.addActionListener(this);
buttonTarget.addActionListener(this);
buttonCopy.addActionListener(this);
// 将面板添加到窗口中
add(panel);
// 设置窗口可见性和默认关闭操作
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonSource) {
// 选择源文件
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
textSource.setText(chooser.getSelectedFile().getAbsolutePath());
}
} else if (e.getSource() == buttonTarget) {
// 选择目标文件
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
textTarget.setText(chooser.getSelectedFile().getAbsolutePath());
}
} else if (e.getSource() == buttonCopy) {
// 复制文件
String source = textSource.getText();
String target = textTarget.getText();
boolean overwrite = checkOverwrite.isSelected();
String mode = (String) comboMode.getSelectedItem();
boolean sourceToTarget = radioSourceToTarget.isSelected();
copyFile(source, target, overwrite, mode, sourceToTarget);
}
}
private void copyFile(String source, String target, boolean overwrite, String mode, boolean sourceToTarget) {
InputStream in = null;
OutputStream out = null;
try {
// 打开输入和输出文件流
File sourceFile = new File(source);
File targetFile = new File(target);
if (sourceToTarget) {
in = new FileInputStream(sourceFile);
out = new FileOutputStream(targetFile);
} else {
in = new FileInputStream(targetFile);
out = new FileOutputStream(sourceFile);
}
// 复制文件
byte[] buffer = new byte[1024];
long totalBytes = sourceFile.length();
long copiedBytes = 0;
int bytes;
while ((bytes = in.read(buffer)) > 0) {
out.write(buffer, 0, bytes);
copiedBytes += bytes;
int progress = (int) (copiedBytes * 100 / totalBytes);
progressBar.setValue(progress);
textLog.append(String.format("%d%% (%d/%d bytes) copied.\n", progress, copiedBytes, totalBytes));
}
// 关闭输入和输出文件流
in.close();
out.close();
progressBar.setValue(100);
textLog.append("文件复制已完成。\n");
// 如果选择了覆盖目标文件,则删除源文件
if (overwrite) {
sourceFile.delete();
textLog.append("源文件已被删除。\n");
}
} catch (IOException e) {
e.printStackTrace();
textLog.append("复制文件时出错:" + e.getMessage() + "\n");
}
}
public static void main(String[] args) {
new FileCopyGUI();
}
}
```
阅读全文