0

ユーザーが 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 から文字列値をうまく取得できます...

ありがとう!

4

1 に答える 1