本文主要是介绍java 图行界面创建文件目录树 编程_控控控-上ke控_新浪博客,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码实现:
package audition_1_100;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
import javax.swing.event.*;
public class JTree2 extends JFrame
{
JPanel cp=new JPanel();
JTree jtree;
DefaultMutableTreeNode root;
public JTree2()
{
this.setSize(300,300);
this.setTitle("try to use tree");
cp=(JPanel)this.getContentPane();
cp.setLayout(new BorderLayout());
root=new DefaultMutableTreeNode("school");
createTree(root);
jtree=new JTree(root);
cp.add(jtree,BorderLayout.CENTER);
}
public static void main(String[] args)
{
JTree2 JTree2 = new JTree2();
JTree2.setVisible(true);
}
private void createTree(DefaultMutableTreeNode root)
{
DefaultMutableTreeNode classroom=null;
DefaultMutableTreeNode number=null;
classroom=new DefaultMutableTreeNode("classroom");
root.add(classroom);
for(int i=1;i<=8;i++)
{
number=new DefaultMutableTreeNode("No."+String.valueOf(i));
if(i==4)
{
for(int j=1;j<=5;j++)
{
number.add(new DefaultMutableTreeNode("seat"+String.valueOf(j)));
}
}
classroom.add(number);
}
}
protected void processWindowEvent(WindowEvent e)
{
if(e.getID()==WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
}
效果图:
这篇关于java 图行界面创建文件目录树 编程_控控控-上ke控_新浪博客的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!