比如java源文件的目录下面有一个文件夹AA,下面有两个文件夹BB和CC,BB中有文件DD.png和EE.png,CC下面有文件FF.png和GG.png,我需要创建一个splitpane,左边是一个树,树的结构就是文件夹AA的结构,右边面板在我点选树中的图片文件时显示图片。
package tmp;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.Icon;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Arrays;
public class TestJTree extends JFrame {
private JPanel contentPane;
private Picture pic=null;
private String rootPath=TestJTree.class.getResource("/").getPath();
private JTree tree;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestJTree frame = new TestJTree();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestJTree() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JSplitPane splitPane = new JSplitPane();
splitPane.setDividerLocation(120);
contentPane.add(splitPane, BorderLayout.CENTER);
MyTreeNode AA = new MyTreeNode(new File(rootPath+"AA"));
tree = new JTree(AA);
tree.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)&& e.getClickCount() == 1) {
MyTreeNode node = (MyTreeNode) tree.getLastSelectedPathComponent();
FileNode fn=(FileNode) node.getUserObject();
File f=fn.getFile();
if(!f.isDirectory()){
pic.loadPic(f.getAbsolutePath());
}
}
}
});
splitPane.setLeftComponent(tree);
pic=new Picture();
splitPane.setRightComponent(pic);
}
}
class MyTreeNode extends DefaultMutableTreeNode{
MyTreeNode(File file){
super(new FileNode(file));
FileNode fn=(FileNode)this.userObject;
File f=fn.getFile();
if(f.isDirectory()){
File[] fs=f.listFiles();
for(File ff:fs){
this.add(new MyTreeNode(ff));
}
}
}
}
class FileNode{
private String name;
private File file;
public FileNode(File file){
this.file=file;
name=file.getName();
}
public String toString(){
return name;
}
public String getName() {
return name;
}
public File getFile() {
return file;
}
}
class Picture extends JPanel {
private String picName;
public Picture(String picName){
this.picName=picName;
}
public Picture() {
}
public void paint (Graphics g) {
g.clearRect(0, 0, this.getWidth(), this.getWidth());
if(this.picName!=null){
Image image = this.getToolkit().getImage(picName);
g.drawImage(image, 0, 0, this);
}
}
public void loadPic(String picName){
this.picName=picName;
repaint();
}
}
//界面编程经验不多,代码可能有诸多不当之处,见谅
追问真是万分感谢您的帮助啊 只是为什么在我的电脑上运行不正常呢 界面是这样的
java源文件的目录下面有一个文件夹AA,下面有两个文件夹BB和CC,BB中有文件DD.png和EE.png,CC下面有文件FF.png和GG.png
你有这样放图片吗?
能不能再麻烦您一下 就是再加个vertical的splitpane 下面弄个label 点选图片时 label显示一句简单的对所选图片的描述
这个 你找到代码中点击选择图片的代码进行添加就可以了
一、jtree用法如下:
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeDemo {
public static void main(String[] args) {
// 创建没有父节点和子节点、但允许有子节点的树节点,并使用指定的用户对象对它进行初始化。
// public DefaultMutableTreeNode(Object userObject)
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("软件部");
node1.add(new DefaultMutableTreeNode(new User("小花")));
node1.add(new DefaultMutableTreeNode(new User("小虎")));
node1.add(new DefaultMutableTreeNode(new User("小龙")));
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("销售部");
node2.add(new DefaultMutableTreeNode(new User("小叶")));
node2.add(new DefaultMutableTreeNode(new User("小雯")));
node2.add(new DefaultMutableTreeNode(new User("小夏")));
DefaultMutableTreeNode top = new DefaultMutableTreeNode("职员管理");
top.add(new DefaultMutableTreeNode(new User("总经理")));
top.add(node1);
top.add(node2);
final JTree tree = new JTree(top);
JFrame f = new JFrame("JTreeDemo");
f.add(tree);
f.setSize(300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加选择事件
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
if (node == null)
return;
Object object = node.getUserObject();
if (node.isLeaf()) {
User user = (User) object;
System.out.println("你选择了:" + user.toString());
}
}
});
}
}
class User {
private String name;
public User(String n) {
name = n;
}
// 重点在toString,节点的显示文本就是toString
public String toString() {
return name;
}
}
二、运行效果如下: