用java编写代码:GUI界面的文件拷贝,8个以上的Swing组件,给至少一个组件添加事件响应,Look and Feel,java,完成二进制类型文件的复制,例如:图片、声音
时间: 2023-10-25 18:06:31 浏览: 134
以下是一个基于Java Swing的简单文件拷贝GUI界面代码,包含了8个以上的Swing组件和事件响应,同时支持Look and Feel设置和二进制类型文件的复制(例如图片、声音等)。
```java
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class FileCopyGUI extends JFrame implements ActionListener {
private JButton btnSrc, btnDest, btnCopy;
private JTextField txtSrc, txtDest;
private JProgressBar progressBar;
private JCheckBox chkBinary;
private JLabel lblStatus;
public FileCopyGUI() {
super("File Copy");
// Set Look and Feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
// Create components
btnSrc = new JButton("Source");
btnDest = new JButton("Destination");
btnCopy = new JButton("Copy");
txtSrc = new JTextField(20);
txtDest = new JTextField(20);
progressBar = new JProgressBar();
chkBinary = new JCheckBox("Binary Mode");
lblStatus = new JLabel("");
// Set component properties
progressBar.setStringPainted(true);
// Set component positions
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
add(btnSrc, gbc);
gbc.gridx = 1;
add(txtSrc, gbc);
gbc.gridx = 2;
add(btnDest, gbc);
gbc.gridx = 3;
add(txtDest, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 4;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(progressBar, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 4;
add(chkBinary, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 4;
add(btnCopy, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 4;
add(lblStatus, gbc);
// Add event listeners
btnSrc.addActionListener(this);
btnDest.addActionListener(this);
btnCopy.addActionListener(this);
// Set window properties
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnSrc) {
// Show file chooser dialog for source file
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Binary Files", "jpg", "gif", "png", "wav", "mp3");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
txtSrc.setText(chooser.getSelectedFile().getPath());
}
} else if (e.getSource() == btnDest) {
// Show file chooser dialog for destination file
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
txtDest.setText(chooser.getSelectedFile().getPath());
}
} else if (e.getSource() == btnCopy) {
// Copy the file
String srcPath = txtSrc.getText();
String destPath = txtDest.getText();
boolean binaryMode = chkBinary.isSelected();
if (srcPath.isEmpty() || destPath.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please select source and destination files.");
return;
}
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath, srcFile.getName());
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
if (binaryMode) {
progressBar.setValue((int) (srcFile.length() * 100.0 / srcFile.length()));
} else {
progressBar.setValue((int) (srcFile.length() * 100.0 / destFile.length()));
}
}
fis.close();
fos.close();
lblStatus.setText("File copied successfully.");
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Error copying file: " + ex.getMessage());
}
}
}
public static void main(String[] args) {
new FileCopyGUI();
}
}
```
在本示例中,我们创建了一个名为`FileCopyGUI`的JFrame窗口,其中包含了8个以上的Swing组件,包括JButton、JTextField、JProgressBar、JCheckBox和JLabel等。我们给“Source”和“Destination”按钮以及“Copy”按钮添加了事件响应函数,当用户单击这些按钮时,我们将分别显示文件选择器对话框来选择源文件和目标文件,或者复制源文件到目标文件中。
在文件复制过程中,我们使用`FileInputStream`和`FileOutputStream`类来打开源文件和目标文件,然后使用一个缓冲区来读取和写入文件内容。我们在进度栏中显示文件复制的进度,并在复制过程结束后在标签中显示成功消息或错误消息。在复制过程中,我们可以选择二进制模式或文本模式来复制文件,这可以通过选中或取消“Binary Mode”复选框来实现。
这是一个简单的文件拷贝GUI界面的Java Swing示例代码,可以拓展和修改以满足特定的需求。
阅读全文