javajapplet:按钮树问题
此小程序应获取存储在menuTree中的树,并遵循基于该树的菜单构造 currentNode存储小程序当前所在的菜单,其每个子项都应显示为按钮 单击按钮后,小程序将带您进入一个新菜单,代表所单击的按钮 我很难在单击另一个按钮时更改按钮 我不太确定这棵树是否构造正确,因为它不太容易测试 任何帮助都将不胜感激 多谢各位javajapplet:按钮树问题,java,swing,button,jtree,japplet,Java,Swing,Button,Jtree,Japplet,此小程序应获取存储在menuTree中的树,并遵循基于该树的菜单构造 currentNode存储小程序当前所在的菜单,其每个子项都应显示为按钮 单击按钮后,小程序将带您进入一个新菜单,代表所单击的按钮 我很难在单击另一个按钮时更改按钮 我不太确定这棵树是否构造正确,因为它不太容易测试 任何帮助都将不胜感激 多谢各位 import javax.swing.*; import javax.swing.tree.*; import java.awt.event.*; import java.awt.*
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);
}
}
}
import javax.swing.*;
导入javax.swing.tree.*;
导入java.awt.event.*;
导入java.awt.*;
公共类菜单扩展JApplet实现ActionListener{
私有静态最终长serialVersionUID=2142470002L;
私人JTree menuTree;
私有DefaultMutableTreeNode currentNode;
私人JPanel按钮面板;
公共void init(){
这个.setSize(700550);
buttonPanel=新的JPanel();
按钮面板设置尺寸(300500);
添加(按钮面板);
/**
*将从数组中的第一个条目生成节点,然后从后续条目生成节点
*并使它们成为第一个的子节点。对于数组项,该过程将递归重复。
*树声明的这个想法以及方法中的代码都是令人喜爱的
*被盗自:http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JTree.html
*/
对象[]菜单={“根”,
新对象[]{“经典国际象棋”,
新对象[]{“游戏”,
“哎”,
“热座椅”,
“在线”
},
“挑战”,
新对象[]{“实践”,
“情况”,
“辅导”
},
},
新对象[]{“仙女棋”,
新对象[]{“游戏”,
“哎”,
“热座椅”,
“在线”
},
“挑战”,
新对象[]{“实践”,
“情况”,
“辅导”
},
“创作作品”
}
};
currentNode=processHierarchy(菜单项);
menuTree=新的JTree(currentNode);
初始化按钮(currentNode);
}
/**
*单击其中一个按钮(该按钮应位于currentNode的子节点中)可将您带到树中的该节点
*将currentNode设置为该节点并重做按钮以表示其子节点。
*/
已执行的公共无效行动(行动事件ae){
按钮b=(按钮)ae.getSource();
对于(int i=0;i按照@Andrew的有用大纲,一个TreeSelectionListener
似乎是合适的。有关详细信息,请参阅。一个应用程序似乎更易于调试。使用revalidate()
是更新修订布局的关键
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
/** @see http://stackoverflow.com/questions/7342713 */
public class Menu extends JApplet {
private JTree menuTree;
private JPanel buttonPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setTitle("Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Menu().initContainer(frame);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
@Override
public void init() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
initContainer(Menu.this);
}
});
}
private void initContainer(Container container) {
container.setLayout(new GridLayout(1, 0));
buttonPanel = new JPanel(new GridLayout(0, 1));
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"
}
};
DefaultMutableTreeNode currentNode = processHierarchy(menuNames);
menuTree = new JTree(currentNode);
menuTree.setVisibleRowCount(10);
menuTree.expandRow(2);
initializeButtons(currentNode);
container.add(buttonPanel, BorderLayout.WEST);
container.add(new JScrollPane(menuTree), BorderLayout.EAST);
menuTree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
initializeButtons((DefaultMutableTreeNode)
menuTree.getLastSelectedPathComponent());
}
});
}
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[]) {
child = processHierarchy((Object[]) nodeSpecifier);
} else {
child = new DefaultMutableTreeNode(nodeSpecifier);
}
node.add(child);
}
return (node);
}
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);
buttonPanel.revalidate();
}
}
}
导入java.awt.BorderLayout;
导入java.awt.Button;
导入java.awt.Container;
导入java.awt.EventQueue;
导入java.awt.GridLayout;
导入javax.swing.JApplet;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTree;
导入javax.swing.event.TreeSelectionEvent;
导入javax.swing.event.TreeSelectionListener;
导入javax.swing.tree.DefaultMutableTreeNode;
/**@见http://stackoverflow.com/questions/7342713 */
公共类菜单扩展了JApplet{
私人JTree menuTree;
私人JPanel按钮面板;
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
JFrame=新JFrame();
frame.setTitle(“菜单”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
新建菜单().initContainer(框架);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
@凌驾
公共void init(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
initContainer(Menu.this);
}
});
}
私有void initContainer(容器){
setLayout(新的GridLayout(1,0));
buttonPanel=新的JPanel(新的网格布局(0,1));
对象[]菜单={“根”,
新对象[]{“经典国际象棋”,
新对象[]{“游戏”、“AI”、“Hotseat”、“在线”},
“挑战”,
新对象[]{“练习”、“情境”、“辅导”}
},
新对象[]{“仙女棋”,
新对象[]{“游戏”、“AI”、“Hotseat”、“在线”},
“挑战”,
新对象[]{“练习”、“情境”、“辅导”},
“创作作品”
}
};
DefaultMutableTreeNode currentNode=processHierarchy(菜单项);
menuTree=新的JTree(currentNode);
menuTree.setVisibleRowCount(10);
菜单行(2);
初始化按钮(currentNode);
container.add(buttonPanel,BorderLayout.WEST);
添加(新的JScrollPane(菜单