Home > Back-end >  Have a big help to look at the code where is wrong? Why only the first button useful
Have a big help to look at the code where is wrong? Why only the first button useful

Time:09-17

Package test.

Import the Java. The awt. BorderLayout.
Import the Java. The awt. GridLayout;
Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
Import the Java. The awt. The font. LayoutPath;
Import the Java. Util. Enumeration;

The import javax.mail. Swing. JButton;
The import javax.mail. Swing. JFrame;
import javax.swing.JPanel;
The import javax.mail. Swing. JTree;
The import javax.mail. Swing. Tree. DefaultMutableTreeNode;

Public class ErgoticTreeNodeTest extends JFrame {
Private DefaultMutableTreeNode root;
Public static void main (String [] args) {
ErgoticTreeNodeTest frame=new ErgoticTreeNodeTest ();
Frame. SetVisible (true);
}
Public ErgoticTreeNodeTest () {
Super ();
SetTitle (" traverse the root node ");
SetBounds (100100, 290, 260);
SetDefaultCloseOperation (EXIT_ON_CLOSE);
The root=new DefaultMutableTreeNode (" root ");//ROOT ROOT node
DefaultMutableTreeNode nodeFirst=new DefaultMutableTreeNode (" level of child nodes. A ");
NodeFirst. Add (new DefaultMutableTreeNode (" secondary child nodes A1 "));
NodeFirst. Add (new DefaultMutableTreeNode (" secondary child nodes A2 "));
Root. The add (nodeFirst);
DefaultMutableTreeNode nodeSecond=new DefaultMutableTreeNode (" level of child node B ");
NodeSecond. Add (new DefaultMutableTreeNode (" secondary child nodes B1 "));
NodeSecond. Add (new DefaultMutableTreeNode (" secondary child nodes B2 "));
Root. The add (nodeSecond);
DefaultMutableTreeNode nodeThird=new DefaultMutableTreeNode (" level of child nodes C ");
NodeThird. Add (new DefaultMutableTreeNode (" secondary child nodes C1 "));
NodeThird. Add (new DefaultMutableTreeNode (" secondary child nodes C2 "));
Root. The add (nodeThird);
JTree tree=new JTree (root);
GetContentPane (). The add (tree, BorderLayout. CENTER);
Final JPanel panel=new JPanel ();
The panel. SetLayout (new GridLayout (0, 1));
GetContentPane (). The add (panel, BorderLayout. EAST);
Final JButton for=new JButton (" by the preorder traversal ");
For the addActionListener (new ButtonActionListener (" by the preorder traversal "));
A panel. The add (for);
Final JButton button2=new JButton (" according to the following sequence traversal ");
For the addActionListener (new ButtonActionListener (" according to the following sequence traversal "));
A panel. The add (button2);
Final JButton button3=new JButton (" with breadth traversal ");
For the addActionListener (new ButtonActionListener (" with breadth traversal "));
A panel. The add (button3);
Final JButton button4=new JButton (" in depth traversal ");
For the addActionListener (new ButtonActionListener (" in depth traversal "));
A panel. The add (button4);
Final JButton button5=new JButton (" traverse the child node ");
For the addActionListener (new ButtonActionListener (" traverse the child node "));
A panel. The add (button5);
}
Private class ButtonActionListener implements ActionListener {
Private String mode;
Public ButtonActionListener (String mode) {
This. Mode=mode;
}
Public void actionPerformed (an ActionEvent e) {
Enumeration<?> Enumeration;//declare node enumeration object
If (mode. The equals (" by the preorder traversal "))//all the preorder traversal tree node
Enumeration=root. PreorderEnumeration ();
Else if (mode) equals (" according to the following sequence traversal "))
Enumeration=root. PostorderEnumeration ();
Else if (mode) equals (" with breadth traversal "))
Enumeration=root. BreadthFirstEnumeration ();
Else if (mode) equals (" in depth traversal "))
Enumeration=root. DepthFirstEnumeration ();
The else
Enumeration=root. The children ();
While (enumeration. HasMoreElements ()) {//traverse the nodes enumeration object
DefaultMutableTreeNode n;
N=(DefaultMutableTreeNode) enumeration. NextElement ();
For (int l=0; LSystem. The out. Print (" -- ");
}
System. The out. Println (n.g etUserObject ());
}

}

}
}

CodePudding user response:

Left out, it is recommended that the Java UI don't work, the basic no one to use
  • Related