import javax.swing.*;import javax.swing.tree.DefaultMutableTreeNode;import java.io.File;public class FolderTree extends JFrame { public FolderTree() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("根目录"); JTree tree = new JTree(root); String folderPath = "C:\\Users\\xxx\\Documents\\test"; File folder = new File(folderPath); addNodes(root, folder); add(new JScrollPane(tree)); setSize(400, 600); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void addNodes(DefaultMutableTreeNode parentNode, File file) { DefaultMutableTreeNode node = new DefaultMutableTreeNode(file.getName()); parentNode.add(node); if (file.isDirectory()) { for (File subFile : file.listFiles()) { addNodes(node, subFile); } } } public static void main(String[] args) { new FolderTree(); }}
时间: 2024-01-22 12:01:50 浏览: 65
jar.zip解决The import javax.crypto cannot be resolved
5星 · 资源好评率100%
好的,下面是使用 React 框架编写的组件代码示例:
```
import React from 'react';
import { JTree, DefaultMutableTreeNode } from 'jtree';
class FolderTree extends React.Component {
constructor(props) {
super(props);
this.state = {
root: new DefaultMutableTreeNode("根目录"),
};
}
componentDidMount() {
const folderPath = "C:\\Users\\xxx\\Documents\\test";
const folder = new File(folderPath);
this.addNodes(this.state.root, folder);
}
addNodes(parentNode, file) {
const node = new DefaultMutableTreeNode(file.getName());
parentNode.add(node);
if (file.isDirectory()) {
for (const subFile of file.listFiles()) {
this.addNodes(node, subFile);
}
}
}
render() {
return (
<div style={{ height: '600px', width: '400px' }}>
<JTree root={this.state.root} />
</div>
);
}
}
export default FolderTree;
```
需要注意的是,由于 React 框架是基于组件的,因此我们需要将原本的 `JFrame` 组件拆分成更小的组件,并使用 JSX 语法来描述组件的结构和样式。在这个示例中,我们使用了 `JTree` 组件和 `DefaultMutableTreeNode` 类来实现文件夹树形结构。
阅读全文