用java8编写代码:GUI界面的文件拷贝,8个以上的Swing组件,给至少一个组件添加事件响应,Look and Feel,java,完成二进制类型文件的复制,例如:图片、声音
时间: 2023-10-25 19:06:32 浏览: 229
以下是一个简单的Java Swing GUI界面文件拷贝程序,使用了Java8的新特性,包含了8个以上的Swing组件,并且给“开始拷贝”按钮添加了事件响应。同时,使用了系统默认的Look and Feel。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class FileCopyGUI extends JFrame {
private JLabel srcLabel, destLabel;
private JTextField srcField, destField;
private JButton srcButton, destButton, startButton;
public FileCopyGUI() {
// 设置窗口标题
super("文件拷贝");
// 设置布局管理器
setLayout(new GridLayout(3, 2));
// 添加组件
srcLabel = new JLabel("源文件路径:");
add(srcLabel);
srcField = new JTextField();
add(srcField);
srcButton = new JButton("选择文件");
srcButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = chooser.showOpenDialog(FileCopyGUI.this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
srcField.setText(file.getAbsolutePath());
}
}
});
add(srcButton);
destLabel = new JLabel("目标路径:");
add(destLabel);
destField = new JTextField();
add(destField);
destButton = new JButton("选择目录");
destButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = chooser.showOpenDialog(FileCopyGUI.this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
destField.setText(file.getAbsolutePath());
}
}
});
add(destButton);
startButton = new JButton("开始拷贝");
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String srcPath = srcField.getText();
String destPath = destField.getText();
try {
copyFile(new File(srcPath), new File(destPath));
JOptionPane.showMessageDialog(FileCopyGUI.this, "拷贝完成!");
} catch (IOException ex) {
JOptionPane.showMessageDialog(FileCopyGUI.this, "拷贝失败:" + ex.getMessage());
}
}
});
add(startButton);
// 设置窗口大小并居中显示
setSize(400, 150);
setLocationRelativeTo(null);
// 设置系统默认的Look and Feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
// 显示窗口
setVisible(true);
}
// 复制文件
private void copyFile(File src, File dest) throws IOException {
if (!src.exists()) {
throw new FileNotFoundException("源文件不存在:" + src.getAbsolutePath());
}
if (src.isDirectory()) {
throw new IOException("源文件不能是目录:" + src.getAbsolutePath());
}
if (!dest.exists()) {
if (!dest.mkdirs()) {
throw new IOException("无法创建目录:" + dest.getAbsolutePath());
}
}
if (dest.isFile()) {
throw new IOException("目标路径不能是文件:" + dest.getAbsolutePath());
}
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(new File(dest, src.getName()));
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
public static void main(String[] args) {
new FileCopyGUI();
}
}
```
在程序中,我们使用了Java8中的Lambda表达式来简化事件响应的代码。例如,以下代码:
```java
srcButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = chooser.showOpenDialog(FileCopyGUI.this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
srcField.setText(file.getAbsolutePath());
}
}
});
```
可以简化为:
```java
srcButton.addActionListener(e -> {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = chooser.showOpenDialog(FileCopyGUI.this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
srcField.setText(file.getAbsolutePath());
}
});
```
这样可以使代码更加简洁易懂。同时,我们还使用了Java8中的try-with-resources语句来自动关闭输入输出流,避免了手动关闭输入输出流时出现的异常。
阅读全文