Java用GUI实现文件复制,包含八个swing组的源代码
时间: 2024-03-09 20:44:38 浏览: 17
以下是一个包含八个 Swing 组件的 Java GUI 程序,可以实现文件复制功能:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class FileCopyGUI extends JFrame implements ActionListener {
private JTextField sourceField;
private JTextField destField;
private JButton copyButton;
private JProgressBar progressBar;
private JLabel statusLabel;
private JRadioButton manualRadio;
private JRadioButton autoRadio;
private JSpinner speedSpinner;
private Timer timer;
public FileCopyGUI() {
super("文件复制");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel sourceLabel = new JLabel("源文件:");
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.EAST;
panel.add(sourceLabel, c);
sourceField = new JTextField(20);
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 2;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(sourceField, c);
JLabel destLabel = new JLabel("目标文件:");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE;
panel.add(destLabel, c);
destField = new JTextField(20);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 2;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(destField, c);
copyButton = new JButton("复制");
copyButton.addActionListener(this);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE;
panel.add(copyButton, c);
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 2;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(progressBar, c);
statusLabel = new JLabel("就绪");
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 3;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(statusLabel, c);
JLabel modeLabel = new JLabel("模式:");
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE;
panel.add(modeLabel, c);
manualRadio = new JRadioButton("手动");
manualRadio.setSelected(true);
c.gridx = 1;
c.gridy = 4;
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE;
panel.add(manualRadio, c);
autoRadio = new JRadioButton("自动");
c.gridx = 2;
c.gridy = 4;
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE;
panel.add(autoRadio, c);
JLabel speedLabel = new JLabel("速度:");
c.gridx = 0;
c.gridy = 5;
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE;
panel.add(speedLabel, c);
SpinnerNumberModel model = new SpinnerNumberModel(1, 1, 10, 1);
speedSpinner = new JSpinner(model);
c.gridx = 1;
c.gridy = 5;
c.gridwidth = 2;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(speedSpinner, c);
ButtonGroup group = new ButtonGroup();
group.add(manualRadio);
group.add(autoRadio);
setContentPane(panel);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == copyButton) {
String source = sourceField.getText();
String dest = destField.getText();
try {
FileInputStream in = new FileInputStream(source);
FileOutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[4096];
int bytesRead;
int totalBytesRead = 0;
int fileSize = in.available();
int speed = (int) speedSpinner.getValue();
if (autoRadio.isSelected()) {
int delay = 1000 / speed;
timer = new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
bytesRead = in.read(buffer);
if (bytesRead == -1) {
timer.stop();
in.close();
out.close();
progressBar.setValue(100);
statusLabel.setText("复制完成!");
} else {
out.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
int percent = (int) (((double) totalBytesRead / fileSize) * 100);
progressBar.setValue(percent);
statusLabel.setText("复制中... (" + percent + "%)");
}
} catch (IOException ex) {
timer.stop();
in.close();
out.close();
JOptionPane.showMessageDialog(FileCopyGUI.this, "复制失败: " + ex.getMessage());
}
}
});
timer.start();
} else {
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
int percent = (int) (((double) totalBytesRead / fileSize) * 100);
progressBar.setValue(percent);
statusLabel.setText("复制中... (" + percent + "%)");
}
in.close();
out.close();
progressBar.setValue(100);
statusLabel.setText("复制完成!");
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "复制失败: " + ex.getMessage());
}
}
}
public static void main(String[] args) {
new FileCopyGUI();
}
}
```
这个程序包含了以下 Swing 组件:
- JTextField:用于输入文件路径;
- JButton:用于触发复制操作;
- JProgressBar:用于显示复制进度;
- JLabel:用于显示状态信息;
- JRadioButton:用于选择复制模式;
- JSpinner:用于选择复制速度。
程序中使用了 GridBagLayout 布局管理器来布置组件,使它们看起来更加整齐。注意,当用户选择自动复制模式时,程序会使用一个 Timer 对象来定时读取文件并更新进度条和状态标签。