package com.dl.dml.common.voice;
import cn.hutool.core.io.FileUtil;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.List;
/**
* @author liuwenpu
* @version 1.0
* @Description 随机查找文件放入文件夹
* @date 2023/6/26 15:30
*/
public class FrameRandomFileMain {
public static void main(String[] args) {
initJFrame();
}
/**
* 从指定目录下随机找文件合成新的目录
* @param rootPath 指定目录路径
* @param directoryName 新的文件夹名称
* @param outputPath 输出路径
* @param count 输出次数
*/
public static boolean createDirectory(String rootPath,String directoryName,String outputPath,int count) {
for (int i = 0; i < count; i++) {
String tempName = directoryName;
if(i > 0) tempName = directoryName + "-" + i;
String filePath = outputPath + File.separator + tempName;
File file = new File(filePath);
//目录存在直接跳过
if(file.exists()) continue;
file.mkdir();
List<String> filePathList = RecordingSynthesis.randomFindFiles(rootPath);
for (String s : filePathList) {
FileUtil.copy(s,filePath,true);
}
}
return true;
}
public static void initJFrame() {
//定义面板
JPanel jPanel = new JPanel(null); //设置面板为绝对布局(null)
//定义标签
JLabel fileNameLabel = new JLabel("文件夹名称:");
fileNameLabel.setBounds(10, 20, 80, 30); //设置组件坐标和大小,x轴坐标为10(也就是距离窗口左边边框10个像素,距离y轴20(也就是距离窗口上边框20个像素
//文本输入框
JTextField fileNameText = new JTextField(10);
fileNameText.setBounds(90, 20, 300, 30);
//定义标签
JLabel outputLabel = new JLabel("输出路径:");
outputLabel.setBounds(10, 60, 80, 30); //设置组件坐标和大小,x轴坐标为10(也就是距离窗口左边边框10个像素,距离y轴20(也就是距离窗口上边框20个像素
//文本输入框
JTextField outputText = new JTextField(10);
outputText.setBounds(90, 60, 300, 30);
//定义标签
JLabel rootPathLabel = new JLabel("音频目录:");
rootPathLabel.setBounds(10, 100, 80, 30); //设置组件坐标和大小,x轴坐标为10(也就是距离窗口左边边框10个像素,距离y轴20(也就是距离窗口上边框20个像素
//文本输入框
JTextField rootPathText = new JTextField(10);
rootPathText.setBounds(90, 100, 300, 30);
//定义标签
JLabel countLabel = new JLabel("合成次数:");
countLabel.setBounds(10, 140, 80, 30); //设置组件坐标和大小,x轴坐标为10(也就是距离窗口左边边框10个像素,距离y轴20(也就是距离窗口上边框20个像素
//文本输入框
JTextField countText = new JTextField(10);
countText.setBounds(90, 140, 300, 30);
//定义标签
JLabel logLabel = new JLabel("日志输出:");
logLabel.setBounds(10, 180, 80, 30); //设置组件坐标和大小,x轴坐标为10(也就是距离窗口左边边框10个像素,距离y轴20(也就是距离窗口上边框20个像素
// //多行文本输入框
// JTextArea logTextArea = new JTextArea(10,10);
// logTextArea.setBounds(90, 180, 300, 100);
// logTextArea.setLineWrap(true); //激活自动换行功能
// logTextArea.setWrapStyleWord(true); // 激活断行不断字功能
// JScrollPane scrollPane = new JScrollPane(logTextArea);
// //取消显示水平滚动条
// scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// //显示垂直滚动条
// scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//按钮
//设置默认值
fileNameText.setText("hh");
outputText.setText("D:\\tmp\\voice");
rootPathText.setText("D:\\tmp\\voice\\测试音频样本");
countText.setText("3");
JButton submitButton = new JButton("提交生成");
submitButton.setBounds(165, 200, 100, 30);
submitButton.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
String fileText = RecordingSynthesis.FILE_TEXT_PATH;
String fileName = fileNameText.getText();
if(isBlank(fileName)) {
alter("文件名称不能为空!");
return;
}
String output = outputText.getText();
if(isBlank(output)) {
alter("输出路径不能为空!");
return;
}
String rootPath = rootPathText.getText();
if(isBlank(rootPath)) {
alter("音频目录不能为空!");
return;
}
if(isBlank(countText.getText())) {
alter("生成次数不能为空!");
return;
}
Integer count = Integer.valueOf(countText.getText());
// new Thread(() -> {
try {
boolean bool = createDirectory(rootPath,fileName,output,count);
if(bool) {
JOptionPane.showMessageDialog(null, "操作成功!","提示框",JOptionPane.INFORMATION_MESSAGE);
return;
}
} catch (Exception exception) {
exception.printStackTrace();
}
alter("操作失败,请检查参数!");
// }).start();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
//面板添加组件
//文件名称
jPanel.add(fileNameLabel);
jPanel.add(fileNameText);
//输出路径
jPanel.add(outputLabel);
jPanel.add(outputText);
//音频路径
jPanel.add(rootPathLabel);
jPanel.add(rootPathText);
//生成次数
jPanel.add(countLabel);
jPanel.add(countText);
//日志输出
// jPanel.add(logLabel);
//通过JScrollPane实现滚动条
// jPanel.add(logTextArea);
// jPanel.add(scrollPane);
// jPanel.add(new JScrollPane(logTextArea));
//提交按钮
jPanel.add(submitButton);
//窗口定义
JFrame jFrame = new JFrame("音频文件合成"); //窗口标题
jFrame.setSize(460, 300); //设置窗口大小,宽500像素,高300像素
jFrame.setResizable(false); //设置窗口是否可以最大化,false为不可最大化
jFrame.setLocationRelativeTo(null); //设置窗口的父窗口,显示于父窗口中间位置,设置为null,表示以电脑窗口为父窗口,因此此窗口运行后居中显示
jFrame.setVisible(true); //运行程序后显示该窗口,默认为不显示,因此需要设置为true
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //窗口关闭后的操作
jFrame.add(jPanel);
}
/**
* 弹出框提示
* @param msg
*/
private static void alter(String msg) {
JOptionPane.showMessageDialog(null, msg,"提示框",JOptionPane.ERROR_MESSAGE);
}
/**
* 判断是否非空
* @param str
* @return
*/
private static boolean isBlank(String str) {
if(str == null) return true;
return "".equals(str.trim());
}
}