Как написать простой JTree с несколькими папками и несколькими папками в этих папках? - PullRequest
0 голосов
/ 31 октября 2019

Я хочу показать университетскую систему от JTree, но я не могу поместить папки в папки. Так что это проблема!
Как я могу решить эту проблему?

    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.JTree;
    import javax.swing.SwingUtilities;
    import javax.swing.tree.DefaultMutableTreeNode;

    public class MyClass extends JFrame
    {
        JFrame frame = new JFrame("JTree Frame");
        private JTree tree;

        public MyClass()
        {
            DefaultMutableTreeNode Universities = new DefaultMutableTreeNode("Universities");

            /* Universities */
            DefaultMutableTreeNode European = new DefaultMutableTreeNode("European University");
            DefaultMutableTreeNode French = new DefaultMutableTreeNode("French University");
            DefaultMutableTreeNode American = new DefaultMutableTreeNode("American University");
            DefaultMutableTreeNode Bryusov = new DefaultMutableTreeNode("Bryusov University");

            /* Departments */
            DefaultMutableTreeNode Business_Management = new DefaultMutableTreeNode("Business_Management");
            DefaultMutableTreeNode Economics_and_Management = new DefaultMutableTreeNode("Economics_and_Management");
            DefaultMutableTreeNode Information_technology = new DefaultMutableTreeNode("Information_technology");
            DefaultMutableTreeNode International_Relation = new DefaultMutableTreeNode("International_Relation");
            DefaultMutableTreeNode Linguistics = new DefaultMutableTreeNode("Linguistics");
            DefaultMutableTreeNode Tourism = new DefaultMutableTreeNode("Tourism");
            DefaultMutableTreeNode European_Criminal_Law_Section = new DefaultMutableTreeNode("European_Criminal_Law_Section");

            /* Subjects */
            DefaultMutableTreeNode Computer_Science_and_Information_Systems = new DefaultMutableTreeNode("Computer_Science_and_Information_Systems");
            DefaultMutableTreeNode Chemical_Engineering = new DefaultMutableTreeNode("Chemical_Engineering");
            DefaultMutableTreeNode Civil_and_Structural_Engineering = new DefaultMutableTreeNode("Civil_and_Structural_Engineering");
            DefaultMutableTreeNode Electrical_and_Electronic_Engineering = new DefaultMutableTreeNode("Electrical_and_Electronic_Engineering");
            DefaultMutableTreeNode Mechanical_Engineering = new DefaultMutableTreeNode("Mechanical_Engineering");
            DefaultMutableTreeNode Mineral_and_Mining_Engineering = new DefaultMutableTreeNode("Mineral_and_Mining_Engineering");
            DefaultMutableTreeNode Accounting_and_Finance = new DefaultMutableTreeNode("Accounting_and_Finance");
            DefaultMutableTreeNode Anthropology = new DefaultMutableTreeNode("Anthropology");
            DefaultMutableTreeNode Development_Studies = new DefaultMutableTreeNode("Development_Studies");

            /* Students */
            String Students[] = {"Ruben", "John", "Rita", "Monica", "Russell", "Logan", "Jane", "Mace", "Shawn", "Debora"};

            Universities.add(European); Universities.add(French);
            Universities.add(American); Universities.add(Bryusov);

            European.add(Business_Management);  European.add(Economics_and_Management);  European.add(Information_technology);  European.add(International_Relation);
            European.add(Linguistics);  European.add(Tourism);  European.add(European_Criminal_Law_Section);
            French.add(Business_Management);  French.add(Economics_and_Management);   French.add(Information_technology);  French.add(International_Relation);
            French.add(Linguistics);  French.add(Tourism);  French.add(European_Criminal_Law_Section);
            American.add(Business_Management);  American.add(Economics_and_Management);   American.add(Information_technology);  American.add(International_Relation);
            American.add(Linguistics);  American.add(Tourism);  American.add(European_Criminal_Law_Section);

Bryusov.add(Business_Management); Bryusov.add(Economics_and_Management);  
Bryusov.add(Information_technology); Bryusov.add(International_Relation);
            Bryusov.add(Linguistics);  Bryusov.add(Tourism);  Bryusov.add(European_Criminal_Law_Section);

            Business_Management.add(Computer_Science_and_Information_Systems);  Business_Management.add(Chemical_Engineering);
            Business_Management.add(Civil_and_Structural_Engineering);  Business_Management.add(Electrical_and_Electronic_Engineering);
            Business_Management.add(Mechanical_Engineering);  Business_Management.add(Mineral_and_Mining_Engineering);
            Business_Management.add(Accounting_and_Finance);  Business_Management.add(Anthropology);
            Business_Management.add(Development_Studies);

            Economics_and_Management.add(Computer_Science_and_Information_Systems);  Economics_and_Management.add(Chemical_Engineering);
            Economics_and_Management.add(Civil_and_Structural_Engineering);  Economics_and_Management.add(Electrical_and_Electronic_Engineering);
            Economics_and_Management.add(Mechanical_Engineering);  Economics_and_Management.add(Mineral_and_Mining_Engineering);
            Economics_and_Management.add(Accounting_and_Finance);  Economics_and_Management.add(Anthropology);
            Economics_and_Management.add(Development_Studies);

            Information_technology.add(Computer_Science_and_Information_Systems);  Information_technology.add(Chemical_Engineering);
            Information_technology.add(Civil_and_Structural_Engineering);  Information_technology.add(Electrical_and_Electronic_Engineering);
            Information_technology.add(Mechanical_Engineering);  Information_technology.add(Mineral_and_Mining_Engineering);
            Information_technology.add(Accounting_and_Finance);  Information_technology.add(Anthropology);
            Information_technology.add(Development_Studies);

            International_Relation.add(Computer_Science_and_Information_Systems);  International_Relation.add(Chemical_Engineering);
            International_Relation.add(Civil_and_Structural_Engineering);  International_Relation.add(Electrical_and_Electronic_Engineering);
            International_Relation.add(Mechanical_Engineering);  International_Relation.add(Mineral_and_Mining_Engineering);
            International_Relation.add(Accounting_and_Finance);  International_Relation.add(Anthropology);
            International_Relation.add(Development_Studies);

            Linguistics.add(Computer_Science_and_Information_Systems);  Linguistics.add(Chemical_Engineering);
            Linguistics.add(Civil_and_Structural_Engineering);  Linguistics.add(Electrical_and_Electronic_Engineering);
            Linguistics.add(Mechanical_Engineering);  Linguistics.add(Mineral_and_Mining_Engineering);
            Linguistics.add(Accounting_and_Finance);  Linguistics.add(Anthropology);
            Linguistics.add(Development_Studies);

            Tourism.add(Computer_Science_and_Information_Systems);  Tourism.add(Chemical_Engineering);
            Tourism.add(Civil_and_Structural_Engineering);  Tourism.add(Electrical_and_Electronic_Engineering);
            Tourism.add(Mechanical_Engineering);  Tourism.add(Mineral_and_Mining_Engineering);
            Tourism.add(Accounting_and_Finance);  Tourism.add(Anthropology);
            Tourism.add(Development_Studies);

            European_Criminal_Law_Section.add(Computer_Science_and_Information_Systems);  European_Criminal_Law_Section.add(Chemical_Engineering);
            European_Criminal_Law_Section.add(Civil_and_Structural_Engineering);  European_Criminal_Law_Section.add(Electrical_and_Electronic_Engineering);
            European_Criminal_Law_Section.add(Mechanical_Engineering);  European_Criminal_Law_Section.add(Mineral_and_Mining_Engineering);
            European_Criminal_Law_Section.add(Accounting_and_Finance);  European_Criminal_Law_Section.add(Anthropology);
            European_Criminal_Law_Section.add(Development_Studies);

            for( int i = 0; i < Students.length; i++ )
            {
                Computer_Science_and_Information_Systems.add(new DefaultMutableTreeNode(Students[i]));
                Chemical_Engineering.add(new DefaultMutableTreeNode(Students[i]));
                Civil_and_Structural_Engineering.add(new DefaultMutableTreeNode(Students[i]));
                Electrical_and_Electronic_Engineering.add(new DefaultMutableTreeNode(Students[i]));
                Mechanical_Engineering.add(new DefaultMutableTreeNode(Students[i]));
                Mineral_and_Mining_Engineering.add(new DefaultMutableTreeNode(Students[i]));
                Accounting_and_Finance.add(new DefaultMutableTreeNode(Students[i]));
                Anthropology.add(new DefaultMutableTreeNode(Students[i]));
                Development_Studies.add(new DefaultMutableTreeNode(Students[i]));
            }

            tree = new JTree(Universities);
            frame.add(tree);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new JScrollPane(tree));
            frame.setSize(300, 300);
            this.pack();
            frame.setVisible(true);
        }

        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run()
                {
                    new MyClass();
                }
            });
        }
    }
...