try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); }
时间: 2024-02-15 14:27:55 浏览: 108
这段代码是用来设置Swing应用程序的外观风格为系统默认的外观风格。其中,UIManager是Swing中管理外观风格的一个类,getSystemLookAndFeelClassName()方法返回系统默认的外观风格的类名。如果设置成功,则Swing中的各种组件(如按钮、标签等)的外观会变为系统默认的外观。如果设置失败,则会打印异常信息。
相关问题
用java编写代码:GUI界面的文件拷贝,8个以上的Swing组件,给至少一个组件添加事件响应,Look and Feel,java,完成二进制类型文件的复制,例如:图片、声音
以下是一个基于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示例代码,可以拓展和修改以满足特定的需求。
用java8编写代码:GUI界面的文件拷贝,8个以上的Swing组件,给至少一个组件添加事件响应,Look and Feel,java,完成二进制类型文件的复制,例如:图片、声音
以下是一个简单的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语句来自动关闭输入输出流,避免了手动关闭输入输出流时出现的异常。
阅读全文