尝试用AI生成日常方便使用的代码程序
用文心一言生成的可用代码
(1)提示1
假如你是一个java程序员,请用java swing创建一个JFrame,显示一个JTextField显示路径,Jtextfield右侧添加一个JButton,下面添加一个JTextArea,点击JButton后新建一个线程,执行耗时操作,完成工作后,在住线程显示,显示到一个JTextArea上面
(2)提示2
接着刚才这个问题,如果在SwingWorker耗时操作的过程中,显示一下进度,该如何实现呢,在JFrame的下面再增加一个进度条组件,耗时操作的过程中实时更新进度到进度条上面
(3)提示3
现在有一个新需求,如果要在一个磁盘分区中递归遍历文件,然后将每一个遍历的文件路径显示到JFrame的一个JLabel中,或者用其他组件显示路径也可以,遍历时间非常久,这种情况能用SwingWorker实现吗?中途如何显示遍历到的文件路径呢
使用AI助手生成简单的项目代码统计程序
简易版本的统计项目代码行数,统计结果显示到TextArea中
package learn;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
/**
* 简易版本的统计项目代码行数,统计结果显示到TextArea中
*/
public class SwingExampleWithProgress2 {
private JFrame frame;
private JTextField pathField;
private JButton button;
private JTextArea textArea;
private JProgressBar progressBar;
String path = "/home/xxx/github/WarCraft/src";
int totalLineCount = 0;
public SwingExampleWithProgress2() {
prepareGUI();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingExampleWithProgress2();
}
});
}
private void prepareGUI() {
frame = new JFrame("Swing Example with Progress");
frame.setSize(450, 400);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
pathField = new JTextField(20);
pathField.setText("Enter a path here:");
pathField.setText(path);
button = new JButton("Perform Task");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingWorker<Void, String> worker = new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() throws Exception {
File directory = new File(path); // 替换为你的目录路径
try {
Files.walkFileTree(Paths.get(directory.getPath()), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().endsWith(".kt")) {
int lines = countLines(file);
String s = file.toString() + " lines:" + lines;
System.out.println(s);
publish(s);
totalLineCount += lines;
} else if (file.toString().endsWith(".java")) {
int lines = countLines(file);
String s = file.toString() + " lines:" + lines;
System.out.println(s);
publish(s);
totalLineCount += lines;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
String s = "Total line count: " + totalLineCount;
System.out.println(s);
publish(s);
return null;
}
@Override
protected void process(List<String> chunks) {
super.process(chunks);
for (String progress : chunks) {
textArea.append(progress + "\n");
}
}
@Override
protected void done() {
textArea.append("Task Completed!\n");
}
};
worker.execute();
}
});
progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true); // 显示进度条上的文字
panel.add(pathField, BorderLayout.CENTER);
panel.add(button, BorderLayout.EAST);
textArea = new JTextArea(10, 30);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(panel, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(progressBar, BorderLayout.SOUTH); // 将进度条添加到框架的底部
frame.setVisible(true);
}
private List<String> visitFile(String rootPath) {
List<String> list = new ArrayList<>();
File directory = new File(rootPath); // 替换为你的目录路径
try {
Files.walkFileTree(Paths.get(directory.getPath()), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().endsWith(".kt")) {
int lines = countLines(file);
String s = file.toString() + " lines:" + lines;
System.out.println(s);
list.add(s);
totalLineCount += lines;
} else if (file.toString().endsWith(".java")) {
int lines = countLines(file);
String s = file.toString() + " lines:" + lines;
System.out.println(s);
list.add(s);
totalLineCount += lines;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
String s = "Total line count: " + totalLineCount;
System.out.println(s);
list.add(s);
return list;
}
private int countLines(Path file) throws IOException {
return (int) Files.lines(file).count();
}
}