demo簡略需求
項目背景
很多文件重復存放,除了管理混亂,還會對患有強迫癥用戶的身心造成10000點的傷害...其實就是360云盤當時上傳了有上傳,造成很多重復的圖片+視頻,前陣子360個人云盤“倒閉”,電腦日夜兼程,始終把圖片下了下來,下下來后,作為重度強迫癥患者的我,看著無數張圖片里面n多重復文件,吃不香睡不著....
點擊“載入”,默認載入上次目錄,若是第一次,默認載入文檔圖片目錄,同時彈出“選擇路徑”菜單。選擇需要載入的文件目錄后,如果跟現載入的不一樣,則中斷載入操作,重新載入新路徑。若一樣,則繼續載入;
圖片預覽,默認顯示第一張,如果用戶點擊選擇某一張,則圖片預覽變為用戶選中的;
點擊去重,彈出“重速度”(多線程),“重性能”(單線程)
點擊撤銷,恢復去重時刪除的文件
去重:先判斷文件名(除去“ - 副本”、“(1)”之類結尾的字符)是否重復,再判斷文件大小+文件類型是否一樣,如果都一樣,則計算文件的md5值,通過md5值來判斷。
第一次啟動時,彈出倒計時框,提示去重撤銷需要創建一個隱藏文件,如果不需要撤銷,可以打鉤,不創建隱藏文件,而是直接刪除;
彈出免責聲明,數據無價,請確認再去重(刪除)。
UI樣圖,如下圖
------------------------------------------------------------------------------------------
想用java寫個桌面小demo,就布局都差點寫吐血了,最終還是沒學好,哎,都怪學藝不精
界面實現代碼
package zuiquchong;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
/**
* @author 蘇寶伢 E-mail:by.su@qq.com
* @version 創建時間: 2017年4月5日 下午12:47:32
*/
public class RemovalUI extends JFrame{
JPanel JPanelTitle,JPanelTextArea,JPanelLblPreview,JPanelOperation,JPanelContainer,JPanelLblStatusBar;
JButton btnLoader,btnRemoval,btnCancel,btnHistory;
JTextArea displayContent;
JLabel lblTitle,lblPreviewContent,lblStatusBar;
public RemovalUI(){
super("最去重文件管理器");
initialization();
}
public static void main(String[] args) {
new RemovalUI();
}
//初始化UI窗體
public void initialization(){
buildComponent();
addComponentToPanel();
componentStyle();
uiLayout();
this.setSize(800, 600);
this.setResizable(false); //不能改變大小
this.setVisible(true); //設置窗口可見
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//創建各組件對象
public void buildComponent(){
JPanelTextArea = new JPanel();
JPanelContainer = new JPanel();
JPanelLblPreview = new JPanel();
JPanelOperation = new JPanel();
JPanelTitle = new JPanel();
JPanelLblStatusBar = new JPanel();
displayContent = new JTextArea(30,48);
lblTitle = new JLabel("最去重文件管理器",SwingConstants.CENTER);
lblPreviewContent = new JLabel();
btnLoader = new JButton("載入");
btnRemoval = new JButton("去重");
btnCancel = new JButton("撤銷");
btnHistory = new JButton("歷史記錄");
lblStatusBar = new JLabel("by不最醉不龜歸 | e-mail:by.su@qq.com",SwingConstants.RIGHT);
}
//往容器中添加組件
public void addComponentToPanel(){
this.add(JPanelTitle);
//this.add(JPanelTextArea);
this.add(JPanelContainer);
this.add(JPanelLblStatusBar);
JPanelTitle.add(lblTitle);
JPanelTextArea.add(displayContent);
JPanelLblStatusBar.add(lblStatusBar);
}
//窗體布局
public void uiLayout(){
//==============總體布局================================
this.setLayout(new BorderLayout(10,10));//邊界布局
this.add(JPanelTextArea,BorderLayout.WEST);
this.add(JPanelContainer,BorderLayout.EAST);
this.add(JPanelTitle,BorderLayout.NORTH);
this.add(JPanelLblStatusBar,BorderLayout.SOUTH);
//===============右側布局================================
JPanelContainer.setLayout(new GridLayout(2,1));//網格布局
JPanelContainer.add(lblPreviewContent);
JPanelContainer.add(JPanelOperation);
//================右下布局===============================
JPanelOperation.setLayout(null);//無布局,絕對布局
JPanelOperation.add(btnLoader);
JPanelOperation.add(btnRemoval);
JPanelOperation.add(btnCancel);
JPanelOperation.add(btnHistory);
btnLoader.setBounds(260,30,120,30);
btnRemoval.setBounds(260,80,120,30);
btnCancel.setBounds(260,130,120,30);
btnHistory.setBounds(260,180,120,30);
}
//組件樣式
public void componentStyle(){
lblTitle.setFont(new java.awt.Font("",1,20));//設置lblTitle字體和大小
lblTitle.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));//設置lblTitle邊框,上左下右
lblPreviewContent.setIcon(new ImageIcon("D://DSC_0675.jpg"));
lblPreviewContent.setSize(10,10);
btnLoader.setPreferredSize(new Dimension(30,30));
btnLoader.setBorderPainted(false);
btnRemoval.setBorderPainted(false);
btnCancel.setBorderPainted(false);
btnHistory.setBorderPainted(false);
//lblStatusBar.setBorder(BorderFactory.createEmptyBorder(580, 560, 0, 0));//狀態欄個人信息向右對齊
}
}
-----------未完待續
package zuiquchong;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author 蘇寶伢 E-mail:by.su@qq.com
* @version 創建時間: 2017年4月10日 下午2:25:10
*/
public class FileOperation {
//String strFilePath;
//顯示目錄下所有文件夾和文件,待用遞歸完善
public List displayFile(String strFilePath){
File f = new File(strFilePath);
File[] fileList = f.listFiles();
List fileNameList = new ArrayList<>();
for(File file:fileList){
fileNameList.add(file.getAbsolutePath());
}
return fileNameList;
}
}
package zuiquchong;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
/**
* @author 蘇寶伢 E-mail:by.su@qq.com
* @version 創建時間: 2017年4月5日 下午12:47:32
*/
public class RemovalUI extends JFrame{
JPanel JPanelTitle,JPanelTextArea,JPanelLblPreview,JPanelOperation,JPanelContainer,JPanelLblStatusBar;
JButton btnLoader,btnRemoval,btnRecovery,btnHistory;
JTextArea displayContent;
JScrollPane scroll;
JLabel lblTitle,lblPreviewContent,lblStatusBar;
public RemovalUI(){
super("最去重文件管理器");
initialization();
}
public static void main(String[] args) {
new RemovalUI();
}
//初始化UI窗體
public void initialization(){
buildComponent();
addComponentToPanel();
componentStyle();
uiLayout();
this.setSize(800, 600);
this.setResizable(false); //不能改變大小
this.setVisible(true); //設置窗口可見
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//創建各組件對象、添加監聽對象、注冊監聽
public void buildComponent(){
JPanelTextArea = new JPanel();
JPanelTextArea = new JPanel();
JPanelContainer = new JPanel();
JPanelLblPreview = new JPanel();
JPanelOperation = new JPanel();
JPanelTitle = new JPanel();
JPanelLblStatusBar = new JPanel();
displayContent = new JTextArea(29,48);
scroll = new JScrollPane(displayContent);//添加滾動條
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);//添加垂直方向滾動條
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); //添加水平方向滾動條
lblTitle = new JLabel("最去重文件管理器",SwingConstants.CENTER);
lblPreviewContent = new JLabel();
btnLoader = new JButton("載入");
btnRemoval = new JButton("去重");
btnRecovery = new JButton("撤銷");
btnHistory = new JButton("歷史記錄");
lblStatusBar = new JLabel("by不最醉不龜歸 | e-mail:by.su@qq.com",SwingConstants.RIGHT);
//---創建監聽對象
ButtonListener uiEvent = new ButtonListener();
FocusEventListener focusEvent = new FocusEventListener();
MouseEventListener mouseEvent = new MouseEventListener();
//注冊監聽btnLoader
btnLoader.addActionListener(uiEvent);
//注冊監聽btnRemoval
btnRemoval.addActionListener(uiEvent);
//注冊監聽btnCancel
btnRecovery.addActionListener(uiEvent);
//注冊監聽btnHistory
btnHistory.addActionListener(uiEvent);
//注冊監聽
displayContent.addFocusListener(focusEvent);
displayContent.addMouseListener(mouseEvent);
}
//往容器中添加組件
public void addComponentToPanel(){
this.add(JPanelTitle);
this.add(JPanelContainer);
this.add(JPanelLblStatusBar);
JPanelTitle.add(lblTitle);
JPanelTextArea.add(scroll);
//JPanelTextArea.add(displayContent);
JPanelLblStatusBar.add(lblStatusBar);
}
//窗體布局
public void uiLayout(){
//==============總體布局================================
this.setLayout(new BorderLayout(10,10));//邊界布局
this.add(JPanelTextArea,BorderLayout.WEST);
this.add(JPanelContainer,BorderLayout.EAST);
this.add(JPanelTitle,BorderLayout.NORTH);
this.add(JPanelLblStatusBar,BorderLayout.SOUTH);
//===============右側布局================================
JPanelContainer.setLayout(new GridLayout(2,1));//網格布局
JPanelContainer.add(lblPreviewContent);
JPanelContainer.add(JPanelOperation);
//================右下布局===============================
JPanelOperation.setLayout(null);//無布局,絕對布局
JPanelOperation.add(btnLoader);
JPanelOperation.add(btnRemoval);
JPanelOperation.add(btnRecovery);
JPanelOperation.add(btnHistory);
btnLoader.setBounds(260,30,120,30);
btnRemoval.setBounds(260,80,120,30);
btnRecovery.setBounds(260,130,120,30);
btnHistory.setBounds(260,180,120,30);
}
//組件樣式
public void componentStyle(){
lblTitle.setFont(new java.awt.Font("",1,20));//設置lblTitle字體和大小
lblTitle.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));//設置lblTitle邊框,上左下右
lblPreviewContent.setIcon(new ImageIcon("D://bysu//個人知識拓展//機器學習//PB1636 王者歸來:OpenCV3使用Java開發手冊//PB1636//samples//DSC_0675.jpg"));
lblPreviewContent.setSize(10,10);
btnLoader.setPreferredSize(new Dimension(30,30));
btnLoader.setBorderPainted(false);
btnRemoval.setBorderPainted(false);
btnRecovery.setBorderPainted(false);
btnHistory.setBorderPainted(false);
//lblStatusBar.setBorder(BorderFactory.createEmptyBorder(580, 560, 0, 0));//狀態欄個人信息向右對齊
}
//Button按鈕的內部監聽類
class ButtonListener implements ActionListener{
String strPath = "D:\\Users\\EX-SUBAOYA001\\Documents";
@Override
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == btnLoader){ //點擊載入
displayContent.setText("");
FileOperation f = new FileOperation();
List list = f.displayFile(strPath);
for(Object obj:list){
displayContent.append((String)obj);
displayContent.append("\r\n");
}
//打開一個文件對話框
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
int chooseValue = fc.showOpenDialog(null);
if(chooseValue==JFileChooser.APPROVE_OPTION){
String chooseFilePath = fc.getSelectedFile().getPath();
if(!chooseFilePath.equals(strPath)){
displayContent.setText("");
list = f.displayFile(chooseFilePath);
for(Object obj:list){
displayContent.append((String)obj);
displayContent.append("\r\n");
}
strPath = chooseFilePath;
}else{
System.out.println("選中的文件目錄與現在的一樣");
}
}else{
System.out.println("沒有選中文件夾");
}
}else if(source == btnRemoval){ //點擊去重
System.out.println("btnRemoval");
}else if(source == btnRecovery){ //點擊撤銷
System.out.println("btnCancel");
}else{ //顯示歷史記錄
System.out.println("btnHistory");
}
}
}
//FocusEvent焦點觸發
class FocusEventListener implements FocusListener{
@Override
public void focusGained(FocusEvent e) {
Object source = e.getSource();
if(source == displayContent){
if(displayContent.getText()!=""){
System.out.println("點擊文本編輯框" + displayContent.getRows());
}else{
System.out.println("文本框非空");
}
}
}
@Override
public void focusLost(FocusEvent e) {
}
}
//鼠標監聽類
class MouseEventListener implements MouseListener{
FileOperation f = new FileOperation();
int count =0;
@Override
public void mouseClicked(MouseEvent e) {
Object source = e.getSource();
if(source == displayContent){
System.out.println(getRowText(displayContent.getText()));
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
//JTextArea內容
FileOperation f = new FileOperation();
//顯示JTextArea控件中所在行的內容
public String getRowText(String text){
public int rows = 0;
displayContent.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
try {
int offset = e.getDot() ; //獲得插入符的位置。
//getLineOfOffset(int offset) 將組件文本中的偏移量轉換為行號
int row = displayContent.getLineOfOffset(offset);
//getLineStartOffset(int line) 取得給定行起始處的偏移量。
//getLineEndOffset(int line) 取得給定行結尾處的偏移量。
int column = e.getDot() - displayContent.getLineStartOffset(row);
// 在狀態欄中顯示當前光標所在行號、所在列號
System.out.println("Line: " + (row + 1) + ", Column: " + (column+1));
row = displayContent.getLineStartOffset(row+1);
System.out.println(row +"fffff");
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
String getText="";
String regex = "\n";
Pattern p =Pattern.compile(regex);
String[] r = p.split(text);
if(!((r[rows-1].trim()).equals(""))){
getText = r[rows-1];
}
return getText;
}
}
/*package zuiquchong;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
*//**
* @author 蘇寶伢 E-mail:by.su@qq.com
* @version 創建時間: 2017年4月5日 下午12:47:32
*//*
public class RemovalUI extends JFrame{
JPanel JPanelTitle,JPanelTextArea,JPanelLblPreview,JPanelOperation,JPanelContainer,JPanelLblStatusBar;
JButton btnLoader,btnRemoval,btnCancel,btnHistory;
JTextArea displayContent;
JScrollPane scroll;
JLabel lblTitle,lblPreviewContent,lblStatusBar;
public RemovalUI(){
super("最去重文件管理器");
initialization();
}
public static void main(String[] args) {
new RemovalUI();
}
//初始化UI窗體
public void initialization(){
buildComponent();
addComponentToPanel();
componentStyle();
uiLayout();
this.setSize(800, 600);
this.setResizable(false); //不能改變大小
this.setVisible(true); //設置窗口可見
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//創建各組件對象、添加監聽對象、注冊監聽
public void buildComponent(){
JPanelTextArea = new JPanel();
JPanelTextArea = new JPanel();
JPanelContainer = new JPanel();
JPanelLblPreview = new JPanel();
JPanelOperation = new JPanel();
JPanelTitle = new JPanel();
JPanelLblStatusBar = new JPanel();
displayContent = new JTextArea(30,48);
scroll = new JScrollPane(displayContent);//添加滾動條
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
lblTitle = new JLabel("最去重文件管理器",SwingConstants.CENTER);
lblPreviewContent = new JLabel();
btnLoader = new JButton("載入");
btnRemoval = new JButton("去重");
btnCancel = new JButton("撤銷");
btnHistory = new JButton("歷史記錄");
lblStatusBar = new JLabel("by不最醉不龜歸 | e-mail:by.su@qq.com",SwingConstants.RIGHT);
//---創建監聽對象
ButtonListener uiEvent = new ButtonListener();
//注冊監聽btnLoader
btnLoader.addActionListener(uiEvent);
//注冊監聽btnRemoval
btnRemoval.addActionListener(uiEvent);
//注冊監聽btnCancel
btnCancel.addActionListener(uiEvent);
//注冊監聽btnHistory
btnHistory.addActionListener(uiEvent);
}
//往容器中添加組件
public void addComponentToPanel(){
this.add(JPanelTitle);
this.add(JPanelContainer);
this.add(JPanelLblStatusBar);
JPanelTitle.add(lblTitle);
JPanelTextArea.add(displayContent);
JPanelLblStatusBar.add(lblStatusBar);
}
//窗體布局
public void uiLayout(){
//==============總體布局================================
this.setLayout(new BorderLayout(10,10));//邊界布局
this.add(JPanelTextArea,BorderLayout.WEST);
this.add(JPanelContainer,BorderLayout.EAST);
this.add(JPanelTitle,BorderLayout.NORTH);
this.add(JPanelLblStatusBar,BorderLayout.SOUTH);
//===============右側布局================================
JPanelContainer.setLayout(new GridLayout(2,1));//網格布局
JPanelContainer.add(lblPreviewContent);
JPanelContainer.add(JPanelOperation);
//================右下布局===============================
JPanelOperation.setLayout(null);//無布局,絕對布局
JPanelOperation.add(btnLoader);
JPanelOperation.add(btnRemoval);
JPanelOperation.add(btnCancel);
JPanelOperation.add(btnHistory);
btnLoader.setBounds(260,30,120,30);
btnRemoval.setBounds(260,80,120,30);
btnCancel.setBounds(260,130,120,30);
btnHistory.setBounds(260,180,120,30);
}
//組件樣式
public void componentStyle(){
lblTitle.setFont(new java.awt.Font("",1,20));//設置lblTitle字體和大小
lblTitle.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));//設置lblTitle邊框,上左下右
lblPreviewContent.setIcon(new ImageIcon("D://bysu//個人知識拓展//機器學習//PB1636 王者歸來:OpenCV3使用Java開發手冊//PB1636//samples//DSC_0675.jpg"));
lblPreviewContent.setSize(10,10);
btnLoader.setPreferredSize(new Dimension(30,30));
btnLoader.setBorderPainted(false);
btnRemoval.setBorderPainted(false);
btnCancel.setBorderPainted(false);
btnHistory.setBorderPainted(false);
//lblStatusBar.setBorder(BorderFactory.createEmptyBorder(580, 560, 0, 0));//狀態欄個人信息向右對齊
}
//內部監聽類
class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == btnLoader){
displayContent.setText("");
String strPath = "D:/Users/EX-SUBAOYA001/Documents";
FileOperation f = new FileOperation();
List list = f.displayFile(strPath);
for(Object obj:list){
displayContent.append((String)obj);
displayContent.append("\r\n");
}
//打開一個文件對話框
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
fc.showOpenDialog(null);
String chooseFilePath = fc.getSelectedFile().getPath();
if(chooseFilePath!=strPath){
displayContent.setText("");
list = f.displayFile(chooseFilePath);
for(Object obj:list){
displayContent.append((String)obj);
displayContent.append("\r\n");
}
}else{
}
}else if(source == btnRemoval){
System.out.println("btnRemoval");
}else if(source == btnCancel){
System.out.println("btnCancel");
}else{
System.out.println("btnHistory");
}
}
}
}*/
public String getRowText(String text){
int rows = 0;
class MyCaretListener implements CaretListener{
int tempResult = 0;
@Override
public void caretUpdate(CaretEvent e) {
try {
int offset = e.getDot() ; //獲得插入符的位置。
//getLineOfOffset(int offset) 將組件文本中的偏移量轉換為行號
int row = displayContent.getLineOfOffset(offset);
//getLineStartOffset(int line) 取得給定行起始處的偏移量。
//getLineEndOffset(int line) 取得給定行結尾處的偏移量。
int column = e.getDot() - displayContent.getLineStartOffset(row);
// 在狀態欄中顯示當前光標所在行號、所在列號
System.out.println("Line: " + (row + 1) + ", Column: " + (column+1));
tempResult = displayContent.getLineStartOffset(row+1);
System.out.println(row +"fffff");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public int getResult(){//為了從內部類中取出結果
return tempResult;
}
}
MyCaretListener mycaretListener = new MyCaretListener();
displayContent.addCaretListener(mycaretListener);
rows = mycaretListener.getResult();
String getText="";
String regex = "\n";
Pattern p =Pattern.compile(regex);
String[] r = p.split(text);
if(!((r[rows-1].trim()).equals(""))){
getText = r[rows-1];
}
return getText;
}