如何显示层次结构(Java)

时间:2012-03-20 22:32:29

标签: java eclipse swing

我有这个项目,它允许用户在其中创建自己的项目并将其保存并做更多事情。我使用Eclipse程序在Java中完成所有这些操作。今天我主要想知道如何展示层次结构?我会更具体一点,当用户创建一个项目时,它会问他们想要在哪里有项目文件夹。让我们说他们选择一个文件夹名称JavaProjects和它在那里桌面(我使用Windows,如果它在Mac和Linux上不一样),并在该文件夹中他们有一个Scripts文件夹和一个艺术文件夹,并在他们有艺术文件夹一个纹理文件夹和一个徽标文件夹(我也在脑海里想出这些文件夹)我怎样才能在JPanelEast中显示一种格式,就像Eclipse中的Package Explorer一样?我能够只扫描他们放入的文件夹,并将所有文件夹和文件整齐地排列出来吗?或者我是否需要根据我的知识做更多的事情?

如果它有帮助,这是我的代码,减去菜单栏和动作监听器

package Engine;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingConstants;

@SuppressWarnings("serial")
public class TestProjectBuilder extends JFrame {

    JPanel jPanelNorth = new JPanel();
    JPanel jPanelSouth = new JPanel();
    JPanel jPanelEast = new JPanel();
    JPanel jPanelCenter = new JPanel();

    JButton jButtonDebug = new JButton("Debug");
    JButton jButtonPause = new JButton("Pause");
    JButton jButtonRun = new JButton("Run");

    JLabel jLabelHeir = new JLabel("");

    GridLayout gridLayout1 = new GridLayout(4,1);

    public TestProjectBuilder() {

        setTitle("Test Project Builder");
        setSize(1400, 800);

        jPanelNorth.setBackground(Color.DARK_GRAY);
        jPanelNorth.setBorder(BorderFactory.createRaisedBevelBorder());
        jPanelNorth.setPreferredSize(new Dimension(14, 40));
        jPanelNorth.setToolTipText("North Panel");

        jPanelNorth.add(jButtonDebug);
        jButtonDebug.setHorizontalAlignment(SwingConstants.CENTER);
        jPanelNorth.add(jButtonPause);
        jButtonPause.setHorizontalAlignment(SwingConstants.CENTER);
        jPanelNorth.add(jButtonRun);
        jButtonRun.setHorizontalAlignment(SwingConstants.CENTER);

        jPanelSouth.setBackground(Color.DARK_GRAY);
        jPanelSouth.setBorder(BorderFactory.createTitledBorder(""));
        jPanelSouth.setPreferredSize(new Dimension(10,200));
        jPanelSouth.setToolTipText("South Panel");

        jPanelEast.setBackground(Color.DARK_GRAY);
        jPanelEast.setBorder(BorderFactory.createEtchedBorder());
        jPanelEast.setPreferredSize(new Dimension(300,10));
        jPanelEast.setToolTipText("East Panel");

        jPanelCenter.setBackground(Color.GRAY);
        jPanelCenter.setBorder(BorderFactory.createEtchedBorder());
        jPanelCenter.setPreferredSize(new Dimension(56,10));
        jPanelCenter.setToolTipText("Center Panel");



        this.getContentPane().add(jPanelNorth, BorderLayout.NORTH);
        this.getContentPane().add(jPanelSouth, BorderLayout.SOUTH);
        this.getContentPane().add(jPanelEast, BorderLayout.EAST);
        this.getContentPane().add(jPanelCenter, BorderLayout.CENTER);

        jPanelCenter.setLayout(gridLayout1);

    }
    public static void main(String[] args) {
        TestProjectBuilder tpb = new TestProjectBuilder();
        tpb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tpb.setVisible(true);
    }
}

提前感谢所有帮助

2 个答案:

答案 0 :(得分:3)

您可以从File Browser GUI的代码开始。

答案 1 :(得分:2)

Java Tutorial中有一节介绍如何使用TreeViews。 http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html

我在这里发布链接,因为这对于一个简单的答案来说有点复杂。