Этот апплет должен взять дерево, сохраненное в menuTree, и следовать построению меню на его основе.
currentNode хранит меню, в котором в данный момент находится апплет, и каждый из его дочерних элементов должен отображаться в виде кнопок.
После нажатия кнопки апплет должен перейти в новое меню, представляющее нажатую кнопку.
У меня возникают проблемы с изменением кнопок при нажатии другой кнопки.
Я не особенно уверен, что дерево даже правильно построено, поскольку его не особенно легко проверить.
Любая помощь будет принята с благодарностью.
Спасибо.
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
import java.awt.*;
public class Menu extends JApplet implements ActionListener{
private static final long serialVersionUID = 2142470002L;
private JTree menuTree;
private DefaultMutableTreeNode currentNode;
private JPanel buttonPanel;
public void init(){
this.setSize(700, 550);
buttonPanel=new JPanel();
buttonPanel.setSize(300, 500);
this.add(buttonPanel);
/**
* will make node out of the first entry in the array, then make nodes out of subsequent entries
* and make them child nodes of the first one. The process is repeated recursively for entries that are arrays.
* this idea of tree declaration as well as the code from the method was lovingly
* stolen from: http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JTree.html
*/
Object [] menuNames = { "ROOT",
new Object[] { "Classic Chess",
new Object[] { "Game",
"AI",
"Hotseat",
"Online"
},
"Challenges",
new Object[]{ "Practice",
"Situations",
"Coaching"
},
},
new Object[] { "Fairy Chess",
new Object[] { "Game",
"AI",
"Hotseat",
"Online"
},
"Challenges",
new Object[]{ "Practice",
"Situations",
"Coaching"
},
"Create Pieces"
}
};
currentNode=processHierarchy(menuNames);
menuTree = new JTree(currentNode);
initializeButtons(currentNode);
}
/**
* Clicking one of the buttons(which should be in the children of the currentNode), takes you to that node in the tree
* setting currentNode to that node and redoing buttons to represent its children.
*/
public void actionPerformed(ActionEvent ae){
Button b=(Button)ae.getSource();
for(int i =0; i<currentNode.getChildCount(); i++){
if(b.getLabel().equals(currentNode.getChildAt(i)+"")){
currentNode=(DefaultMutableTreeNode)currentNode.getChildAt(i);
initializeButtons(currentNode);
}
}
}
/**
* will make node out of the first entry in the array, then make nodes out of subsequent entries
* and make them child nodes of the first one. The process is repeated recursively for entries that are arrays.
* this idea of tree declaration as well as the code from the method was lovingly
* stolen from: http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JTree.html
*/
private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
DefaultMutableTreeNode child;
for (int i = 1; i < hierarchy.length; i++) {
Object nodeSpecifier = hierarchy[i];
if (nodeSpecifier instanceof Object[]) // Ie node with children
child = processHierarchy((Object[]) nodeSpecifier);
else
child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
node.add(child);
}
return (node);
}
/**
* creates buttons for each child of the given node, labels them with their String value, and adds them to the panel.
*/
private void initializeButtons(DefaultMutableTreeNode node){
Button b;
buttonPanel.removeAll();
for(int i =0; i<node.getChildCount(); i++){
b=new Button();
b.setLabel(""+node.getChildAt(i));
buttonPanel.add(b);
}
}
}