我正在构建一个树遍历程序,它允许用户运行 BFS 和 DFS 遍历,以及添加和删除节点。
我坚持的是从 JComboBox 获取节点并将其传递给appendNode()
. 我想实现这一点:
首先,我添加并连接了一堆节点......addNode()
将节点添加到nodeList
.
然后我将所有节点添加到 JComboBox parents
:
for (Nodes n : nodeList) {
parents.addItem(n.getValue());
}
如上所示,节点已成功添加到 JComboBox。
然后我创建一个新类:
//send in selected parent from combo box
AppendChildren ac = new AppendChildren(child, parents);
this.child.addActionListener(ac);
this.AddButton.addActionListener(ac);
这利用了这个类......
class AppendChildren implements ActionListener {
private TextField child;
private JComboBox parents;
private int index;
public AppendChildren(TextField child, JComboBox parent, int parentIndex) {
this.child = child;
this.parents = parent;
this.index = parentIndex;
}
public void actionPerformed(ActionEvent ae) {
//set max input to 2 characters
if (child.getText().length() <= 0) {
addMoreMessage = "Please name your child...";
}
else {
addMoreMessage = "";
}
if (child.getText().length()>1) {
child.setText(child.getText().substring(0,1));
}
String childName = child.getText();
parents.setSelectedIndex(index);
Nodes newChild = new Nodes(childName, nodeX, nodeY, nodeWidth, nodeHeight);
appendNode(parentNode, newChild);
}
}
它调用appendNode(Nodes parent, Nodes child) {
连接节点并重新创建邻接矩阵。
我的问题是:如何从 JComboBox 中选择节点并将其传递给appendNode()
? 我能够从 TextField 中获取字符串值就好了......
谢谢!