Java Swing实现简易版项目代码统计

本文介绍了如何利用AI生成的代码示例,展示了如何使用JavaSwing创建界面,包括JFrame、JTextField、JButton和JTextArea,并结合SwingWorker进行文件遍历、进度显示及统计代码行数。
摘要由CSDN通过智能技术生成

尝试用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();
    }
}

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值