try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); }
时间: 2024-02-15 07:27:55 浏览: 24
这段代码是用来设置Swing应用程序的外观风格为系统默认的外观风格。其中,UIManager是Swing中管理外观风格的一个类,getSystemLookAndFeelClassName()方法返回系统默认的外观风格的类名。如果设置成功,则Swing中的各种组件(如按钮、标签等)的外观会变为系统默认的外观。如果设置失败,则会打印异常信息。
相关问题
在eclipse中,用java8编写代码:GUI界面的文件拷贝,8个以上的Swing组件,给至少一个组件添加事件响应,Look and Feel,java,完成二进制类型文件的复制,例如:图片、声音
好的,以下是一个简单的GUI界面的文件拷贝程序,其中包含8个以上的Swing组件,并给一个组件添加了事件响应。同时,使用了Java 8的新特性lambda表达式来简化事件处理代码:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
public class FileCopyGUI {
private JFrame frame;
private JButton btnSelectSource;
private JButton btnSelectDest;
private JButton btnCopy;
private JLabel lblSource;
private JLabel lblDest;
private JProgressBar progressBar;
private File sourceFile;
private File destFile;
public static void main(String[] args) {
// Set the look and feel to the system default
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
// Start the GUI on the event dispatch thread
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FileCopyGUI window = new FileCopyGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public FileCopyGUI() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("Source File:");
lblNewLabel.setBounds(10, 11, 77, 14);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Destination File:");
lblNewLabel_1.setBounds(10, 36, 96, 14);
panel.add(lblNewLabel_1);
btnSelectSource = new JButton("Select");
btnSelectSource.setBounds(116, 7, 89, 23);
panel.add(btnSelectSource);
btnSelectDest = new JButton("Select");
btnSelectDest.setBounds(116, 32, 89, 23);
panel.add(btnSelectDest);
lblSource = new JLabel("");
lblSource.setBounds(215, 11, 209, 14);
panel.add(lblSource);
lblDest = new JLabel("");
lblDest.setBounds(215, 36, 209, 14);
panel.add(lblDest);
btnCopy = new JButton("Copy");
btnCopy.setBounds(335, 227, 89, 23);
panel.add(btnCopy);
progressBar = new JProgressBar();
progressBar.setBounds(10, 227, 315, 23);
panel.add(progressBar);
// Add event listeners to the buttons
btnSelectSource.addActionListener(e -> {
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
sourceFile = chooser.getSelectedFile();
lblSource.setText(sourceFile.getAbsolutePath());
}
});
btnSelectDest.addActionListener(e -> {
JFileChooser chooser = new JFileChooser();
int result = chooser.showSaveDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
destFile = chooser.getSelectedFile();
lblDest.setText(destFile.getAbsolutePath());
}
});
btnCopy.addActionListener(e -> {
if (sourceFile == null || destFile == null) {
JOptionPane.showMessageDialog(frame, "Please select source and destination files.");
return;
}
try {
FileInputStream in = new FileInputStream(sourceFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int length;
int totalBytesRead = 0;
int fileSize = (int) sourceFile.length();
progressBar.setMaximum(fileSize);
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
totalBytesRead += length;
progressBar.setValue(totalBytesRead);
}
in.close();
out.close();
JOptionPane.showMessageDialog(frame, "File copied successfully.");
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(frame, "Error copying file: " + ex.getMessage());
}
});
}
}
```
代码中,我们使用了`JFileChooser`来选择源文件和目标文件,使用`JProgressBar`来显示复制进度,并在“Copy”按钮上添加了事件监听器,以便在单击按钮时执行文件复制操作。我们还使用了Java 8的lambda表达式来简化代码。
注意:在使用此程序时,请记得选择正确的源文件和目标文件,并确保目标文件不存在或为空,否则可能会导致数据丢失。
用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示例代码,可以拓展和修改以满足特定的需求。