在eclipse中,用java8编写代码:GUI界面的文件拷贝,8个以上的Swing组件,给至少一个组件添加事件响应,Look and Feel,java,完成二进制类型文件的复制,例如:图片、声音

时间: 2023-10-21 18:05:17 浏览: 38
好的,以下是一个简单的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表达式来简化代码。 注意:在使用此程序时,请记得选择正确的源文件和目标文件,并确保目标文件不存在或为空,否则可能会导致数据丢失。

相关推荐

最新推荐

recommend-type

Java版给爱人表白的玫瑰花程序代码

主要讲解了Java版给爱人表白的玫瑰花程序代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
recommend-type

在eclipse中把java文档打包成jar文件

初学者们经常纳闷,千辛万苦写出的一个个程序,却只是一堆散乱的class文件,于是,将它们打包成为可使用的jar文件成了拦路虎。自制资源,与大家分享。
recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

matlab建立计算力学课程的笔记和文件.zip

matlab建立计算力学课程的笔记和文件.zip
recommend-type

FT-Prog-v3.12.38.643-FTD USB 工作模式设定及eprom读写

FT_Prog_v3.12.38.643--FTD USB 工作模式设定及eprom读写
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。