Java程序:一个窗体已经创建了一个文件树,使用的是边界布局,树传入了一个文件的路径,现在我想要的是创建一个鼠标监听器,点击文件树上的文件后,如果是txt文本文件能够在右边窗体上显示出来
时间: 2024-02-17 22:04:00 浏览: 68
好的,您可以通过以下步骤实现:
1. 创建一个鼠标监听器类,实现 MouseListener 接口,重写 mousePressed 方法。
2. 在 mousePressed 方法中获取鼠标点击的树节点,判断是否为文件节点,如果是txt文件,读取文件内容并显示在右边窗体上。
示例代码如下:
```java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
public class FileTreeDemo extends JFrame {
private static final long serialVersionUID = 1L;
private JTree fileTree;
private JPanel rightPanel;
private JTextArea contentArea;
public FileTreeDemo(String filePath) {
init(filePath);
addListener();
}
private void init(String filePath) {
setTitle("File Tree Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(600, 400));
setLayout(new BorderLayout());
// 创建文件树
DefaultMutableTreeNode root = new DefaultMutableTreeNode(filePath);
File file = new File(filePath);
createFileTree(root, file);
fileTree = new JTree(root);
JScrollPane scrollPane = new JScrollPane(fileTree);
scrollPane.setPreferredSize(new Dimension(200, 0));
add(scrollPane, BorderLayout.WEST);
// 创建右边的窗体
rightPanel = new JPanel(new BorderLayout());
contentArea = new JTextArea();
contentArea.setEditable(false);
JScrollPane contentScrollPane = new JScrollPane(contentArea);
rightPanel.add(contentScrollPane, BorderLayout.CENTER);
add(rightPanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void createFileTree(DefaultMutableTreeNode root, File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(f.getName());
root.add(node);
createFileTree(node, f);
}
}
}
private void addListener() {
fileTree.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
// 获取鼠标点击的树节点
TreePath path = fileTree.getPathForLocation(e.getX(), e.getY());
if (path != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
String fileName = node.toString();
if (fileName.endsWith(".txt")) {
// 读取txt文件内容并显示在右边窗体上
String filePath = getFilePath(node);
String content = readFile(filePath);
contentArea.setText(content);
}
}
}
private String getFilePath(DefaultMutableTreeNode node) {
StringBuilder sb = new StringBuilder();
sb.append(node.toString());
while (node.getParent() != null) {
node = (DefaultMutableTreeNode) node.getParent();
sb.insert(0, node.toString() + File.separator);
}
return sb.toString();
}
private String readFile(String filePath) {
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
});
}
public static void main(String[] args) {
new FileTreeDemo("C:\\");
}
}
```
在上面的代码中,我们创建了一个文件树,当鼠标点击文件节点时,如果是txt文件,则会读取文件内容并显示在右边窗体上。注意,需要将文件路径作为参数传入构造方法中,这里以C盘作为例子。
阅读全文