Código fuente de Ztreedemo
¿Importar? javax.swing.joptionpane;
¿Importar? javax.swing.jscrollpane;
¿Importar? javax.swing.jtree;
¿Importar? javax.swing.scrollpaneconstants;
¿Importar? javax .swing administrador de interfaz de usuario;
¿Importar? javax . swing .excepción de lookandfeel no compatible;
¿Importar? javax . swing evento .treeselectionevent;
¿Importar? javax . swing evento .treeselectionlistener;
¿Importar? javax . swing . árbol . defaultmutabletreenode;
¿Importar? javax . swing . árbol predeterminado;
/**
*?
*?@autor? Agua perdida
*
*/
¿Pública? ¿clase? ¿JTreeDemo? ¿extender? JFrame? ¿Utensilios? TreeSelectionListener{
¿Privado? ¿Electricidad estática? finales? ¿dragón? serialVersionUID? =?-6209666185329417356 l;
//Componente del árbol
¿Protegido? ¿Járbol? jÁrbol? ;
¿Público? JTreeDemo(){
init();
}
//Inicialización
¿Privada? ¿Vacío? inicio()? {
jÁrbol? =¿Nuevo? JTree();
//Datos relacionados de los nodos del árbol
DefaultMutableTreeNode? ¿raíz? =?¿Nuevo? DefaultMutableTreeNode("Tres Reinos");
¿DefaultMutableTreeNode? ¿Shu? =?¿Nuevo? DefaultMutableTreeNode("Shuzhou");
¿DefaultMutableTreeNode? ¿Wei? =?¿Nuevo? DefaultMutableTreeNode("Wei Guo");
¿DefaultMutableTreeNode? ¿Wu? =?¿Nuevo? DefaultMutableTreeNode("Wu Guo");
shu.add(New?DefaultMutableTreeNode("Guan Yu"));
shu.add(New?DefaultMutableTreeNode("Zhang Fei") ) ;
shu.add(New?DefaultMutableTreeNode("Zhao Yun"));
shu.add(New?DefaultMutableTreeNode("Ma Chao"));
shu.add(New?DefaultMutableTreeNode("Huang Zhong"));
wei.add(New?DefaultMutableTreeNode("Zhang Liao"));
wei.add (¿Nuevo?DefaultMutableTreeNode ("Dianwei"));
wei.add(new?DefaultMutableTreeNode("Xiahoudun"));
wei.add(new?DefaultMut
ableTreeNode("Chu Xu"));
wei.add(nuevo? DefaultMutableTreeNode("Huang Xu"));
Wu Tian(nuevo? DefaultMutableTreeNode("Taishici") ) ;
Wu Tian(Nuevo?DefaultMutableTreeNode("Gan Ning"));
Wu Tian(Nuevo?DefaultMutableTreeNode("Sun Ce"));
Wu Tian(Nuevo?DefaultMutableTreeNode("Zhou Yu"));
Wu Tian(Nuevo?DefaultMutableTreeNode("Lu Xun");
root.add(Shu); p>
root.add(Wei);
root.add(Wu);
//Modelo de datos de árbol
DefaultTreeModel =?
//Establecer modelo de datos
jTree.setModel(model);
//?Expandir todos los árboles
¿Para qué? (int?i?=?0;?i?<?jtree.getrowcount();?i++)
jtree.expandrow(I);
/ /Agregar evento
jtree. addtreeselectionlistener(this);
//Panel de desplazamiento
JScrollPane? =? jScrollPane.getViewport(). add(jTree);
//Agrega un panel de desplazamiento a la ventana
this.getContentPane().
Resolver. ("ejemplo de evento jtree");
This. pack();
This.
SALIR _ EN _ CERRAR);
}
¿Público? ¿Electricidad estática? ¿Vacío? main(String[]?args){
//Estilo de Windows
//String? ¿mirar y sentir? =?"com . sun . Java . swing . plaf . windows . windows lookandfeel";
//Windows? Estilo clásico
//¿Cuerda? ¿mirar y sentir? =?"com . sun . Java . swing . plaf . windows . windows classiclookandfeel";
//El estilo actual del sistema
¿Cadena? ¿mirar y sentir? =?administrador de interfaz de usuario . getsystemlookandfeelclassname();
¿Probar? {
administrador de interfaz de usuario . setlookandfeel(lookAndFeel);
}?Catch? (¿ClassNotFoundException?e)? {
e . printstacktrace();
}?Captura? (¿Excepción de instanciación? e)? {
e . printstacktrace();
}?Captura? (¿Excepción de acceso ilegal? e)? {
e . printstacktrace();
}?Captura? (¿Excepción LookAndFeel no compatible?e)? {
e . printstacktrace();
}
JTreeDemo? ¿Manifestación? =?¿Nuevo? JTreeDemo();
demo . establecer visible(true);
}
@Override
¿Público? ¿Vacío? ¿Valor cambiado (TreeSelectionEvent? e)? {
//Obtener el nodo seleccionado
¿DefaultMutableTreeNode? ¿nodo? =?(DefaultMutableTreeNode)? jTree
. getLastSelectedPathComponent();
if(node.getLevel()?==?0){
//Mostrar mensaje de aviso
joptionpane .
node.getUserObject()+":?* * * "+node . obtener recuento de niños()+" países ");
}¿Otro? if(node.getLevel()?==?1){
//Mostrar mensaje de aviso
joptionpane . showmessagedialog(null,
node.getUserObject(. )+":?***"+node.getChildCount()+"Celebrity");
}¿Algo más? if(node.getLevel()?==?2){
//Mostrar mensaje de aviso
joptionpane . showmessagedialog(null, Node.getParent()+"Buen nombre:? "+
nodo . obtener objeto de usuario());
}
}
}