gpt4 book ai didi

java - 如何在 JTextArea 列表中浏览此代码的结果?

转载 作者:行者123 更新时间:2023-12-02 03:43:13 24 4
gpt4 key购买 nike

我有这个用于搜索重复文件的代码,但我不知道如何在列表等文本区域中显示结果。

public static void find(Map<String, List<String>> lists, File dir) {
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
find(lists, f);
} else {
String hash = f.getName() + f.length();
List<String> list = lists.get(hash);
if (list == null) {

// Create a linked list and add duplicate entries
list = new LinkedList<String>();
lists.put(hash, list);
}
list.add(f.getAbsolutePath());
}
}
}

我尝试过,但没有成功

logScrollPane.add(list1); 文件 file = fc.getSelectedFile();

            Map<String, List<String>> lists = new HashMap<String, List<String>>();
find(lists, file);

//This is where a real application would open the file.
listModel.addElement("Looking for Duplicates in Folder Name: " + file.getName() + " at location " + file.getParent());
listModel.addElement("Double Click on File to open the Folder");

for (List<String> list : lists.values()) {
if (list.size() > 1) {
listModel.addElement("--");
listModel.addElement("Duplicates Found");
for (String file1 : list) {
listModel.addElement(file1);
}
}
}
listModel.addElement("--");

最佳答案

试试这个...

这是一个独立的演示,会将重复项显示为 JListJTextArea。拿走你需要的。

我不确定您在哪里遇到了麻烦,因此请查看下面的 doFind() 实现,以查看两个不同的部分,一个填充列表,另一个填充文本面积。

其余大部分代码只是 Swing 内容,用于制作独立示例。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ListDemo extends JFrame {

DefaultListModel listModel = new DefaultListModel();
JList list = new JList(listModel);
JScrollPane listScrollPane = new JScrollPane(list);
JPanel buttonPanel = new JPanel(new FlowLayout());
JPanel centerPanel = new JPanel(new GridLayout(1, 0));
JTextArea textArea = new JTextArea();
JScrollPane textAreaScrollPane = new JScrollPane(textArea);
JTextField pathTextField = new JTextField();
JButton findButton = new JButton("Find");

/**
* Original implementation of "find" from question.
*
* @param lists
* @param dir
*/
public static void find(Map<String, List<String>> lists, File dir) {
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
find(lists, f);
} else {
String hash = f.getName() + f.length();
List<String> list = lists.get(hash);
if (list == null) {

// Create a linked listScrollPane and add duplicate entries
list = new LinkedList<String>();
lists.put(hash, list);
}
list.add(f.getAbsolutePath());
}
}
}

public void doFind() {
String path = pathTextField.getText();
if (path == null || path.isEmpty()) {
return;
}
File file = new File(path);
if (!(file.exists() && file.isDirectory())) {
JOptionPane.showMessageDialog(this, "Not a valid path.");
return;
}

Map<String, List<String>> lists = new HashMap<String, List<String>>();

// Populate list model
listModel.clear();
find(lists, file);
for (List<String> list : lists.values()) {
if (list.size() > 1) {
listModel.addElement("--");
listModel.addElement("Duplicates Found");
for (String file1 : list) {
listModel.addElement(file1);
}
}
}
listModel.addElement("--");

// Populate text area
StringBuilder sb = new StringBuilder();
find(lists, file);
for (List<String> list : lists.values()) {
if (list.size() > 1) {
sb.append("--\n");
sb.append("Duplicates Found\n");
for (String file1 : list) {
sb.append(file1);
sb.append("\n");
}
}
}
sb.append("--\n");
textArea.setText(sb.toString());
}

public ListDemo() {
setTitle("List Demo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(new Dimension(500, 500));
centerPanel.add(listScrollPane);
centerPanel.add(textAreaScrollPane);
add(centerPanel, BorderLayout.CENTER);
pathTextField.setColumns(30);
buttonPanel.add(pathTextField);
buttonPanel.add(findButton);
add(buttonPanel, BorderLayout.SOUTH);
findButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doFind();
}
});
}

public static final void main(String[] args) {
// Run in GUI thread
Runnable runnable = new Runnable() {
@Override
public void run() {
ListDemo frame = new ListDemo();
// Center in screen and show
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
frame.setVisible(true);
}
};
SwingUtilities.invokeLater(runnable);
}
}

请注意,这不是最好的方法,因为文件搜索是在 GUI 线程上完成的,并且结果不会增量显示,但它会在紧要关头起作用。

关于java - 如何在 JTextArea 列表中浏览此代码的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56823412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com