ช่วยทำหน้าMainให้หน่อยได้ไหมครับคือผมไม่ค่อยเข้าใจเท่าไร
public class cartoonShop { public class TreeSearch extends JFrame { private static final long serialVersionUID = 1L; private DefaultTreeModel model; private DefaultMutableTreeNode root; private JTree tree; private static final int DEFAULT_WIDTH = 800; private static final int DEFAULT_HEIGHT = 500; Container contentPane = getContentPane(); JTextField textSearch; public TreeSearch(){ setTitle("HomeCatoon"); setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); root=makeSampleTree(); model = new DefaultTreeModel(root); tree = new JTree(model); JScrollPane scrollPane = new JScrollPane(tree); add(scrollPane,BorderLayout.CENTER); //ฟังก์ชั้นในการเพิ่มข้อมูล tree.setEditable(true); //ฟังก์ชั่นในการแก้ไขชื่อของข้อมูล makeButtons(); } public DefaultMutableTreeNode makeSampleTree(){ DefaultMutableTreeNode root = new DefaultMutableTreeNode("คลังเก็บข้อมูล"); DefaultMutableTreeNode style = new DefaultMutableTreeNode("Book"); root.add(style); DefaultMutableTreeNode type = new DefaultMutableTreeNode("Cartoon"); style.add(type); DefaultMutableTreeNode title = new DefaultMutableTreeNode("DORAEMON"); type.add(title); title = new DefaultMutableTreeNode("Reborn"); type.add(title); type = new DefaultMutableTreeNode("บากิ"); style.add(type); title = new DefaultMutableTreeNode("Shaman King"); type.add(title); type = new DefaultMutableTreeNode("Bleach"); style.add(type); title = new DefaultMutableTreeNode("Hunter x Hunter"); type.add(title); title = new DefaultMutableTreeNode("ก้าวแรกสู่สังเวียน"); type.add(title); title = new DefaultMutableTreeNode("Air Gear"); type.add(title); type = new DefaultMutableTreeNode("One Piece"); style.add(type); title = new DefaultMutableTreeNode("Fairy Tail"); type.add(title); title = new DefaultMutableTreeNode("Fullmetal Alchemist"); type.add(title); type = new DefaultMutableTreeNode("Negima"); style.add(type); title = new DefaultMutableTreeNode("The Prince of Tennis"); type.add(title); title = new DefaultMutableTreeNode("Naruto"); type.add(title); title = new DefaultMutableTreeNode("D.Gray-Man"); type.add(title); title = new DefaultMutableTreeNode("Digimon"); type.add(title); type = new DefaultMutableTreeNode("Worst"); style.add(type); title = new DefaultMutableTreeNode("Ayakashi Hisen"); type.add(title); title = new DefaultMutableTreeNode("เทวภูตทมิน"); type.add(title); title = new DefaultMutableTreeNode("Bakuman"); type.add(title); title = new DefaultMutableTreeNode("Black Cat"); type.add(title); title = new DefaultMutableTreeNode("Claymore"); type.add(title); title = new DefaultMutableTreeNode("GTO"); type.add(title); type = new DefaultMutableTreeNode("CODE BREAKER"); style.add(type); title = new DefaultMutableTreeNode("DRAGON BALL AF"); type.add(title); title = new DefaultMutableTreeNode("DRAGON BALL Z"); type.add(title); type = new DefaultMutableTreeNode("DRAGON BALL GT"); style.add(type); title = new DefaultMutableTreeNode("DRAGON BALL "); type.add(title); title = new DefaultMutableTreeNode("GANTZ "); type.add(title); title = new DefaultMutableTreeNode("HAJIME NO IPPO"); type.add(title); type = new DefaultMutableTreeNode("BEELZEBUB"); style.add(type); title = new DefaultMutableTreeNode("TORIKO"); type.add(title); title = new DefaultMutableTreeNode("New Prince of Tennis "); type.add(title); title = new DefaultMutableTreeNode("Ao no exorcist"); type.add(title); title = new DefaultMutableTreeNode("Bloody Monday"); type.add(title); title = new DefaultMutableTreeNode("Breaker"); type.add(title); title = new DefaultMutableTreeNode("Breaker New Waves"); type.add(title); title = new DefaultMutableTreeNode("Blazer Drive"); type.add(title); title = new DefaultMutableTreeNode("Code Breaker"); type.add(title); title = new DefaultMutableTreeNode("GTO Shonan 14 Days"); type.add(title); title = new DefaultMutableTreeNode("Lock On"); type.add(title); type = new DefaultMutableTreeNode("Soul Eater"); style.add(type); title = new DefaultMutableTreeNode("นูระหลานจอมภูติ"); type.add(title); title = new DefaultMutableTreeNode("Kiss x Sis"); type.add(title); title = new DefaultMutableTreeNode("Mayoi Neko Overrun!"); type.add(title); title = new DefaultMutableTreeNode("Kami Sen"); type.add(title); type = new DefaultMutableTreeNode("Haru to Natsu"); style.add(type); title = new DefaultMutableTreeNode("Countrouble"); type.add(title); title = new DefaultMutableTreeNode("Saint Seiya"); type.add(title); type = new DefaultMutableTreeNode("ฟงอวิ๋น ขี่พายุทะลุฟ้า"); style.add(type); title = new DefaultMutableTreeNode("กระบี่อิงฟ้า ดาบฆ่ามังกร"); type.add(title); title = new DefaultMutableTreeNode("Yulgang"); type.add(title); type = new DefaultMutableTreeNode("ยอดกุ๊กแดนมักร"); style.add(type); title = new DefaultMutableTreeNode("โคนัน"); type.add(title); title = new DefaultMutableTreeNode("Gundam"); type.add(title); title = new DefaultMutableTreeNode("Gundam Seed"); type.add(title); type = new DefaultMutableTreeNode("Gundam Wing"); style.add(type); title = new DefaultMutableTreeNode("มือขวากับขาโจ๋"); type.add(title); title = new DefaultMutableTreeNode("Keroro"); type.add(title); type = new DefaultMutableTreeNode("Gashbell"); style.add(type); title = new DefaultMutableTreeNode("Black Butler"); type.add(title); title = new DefaultMutableTreeNode("Chi'sweet home"); type.add(title); title = new DefaultMutableTreeNode("Cage of Eden"); type.add(title); title = new DefaultMutableTreeNode("Iris Zero"); type.add(title); return root; } public void makeButtons(){ JPanel panel = new JPanel(); textSearch = new JTextField(10); //Function Of Search panel.add(textSearch); JButton addSearchButton = new JButton("SEARCH"); //Button Of Search addSearchButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ actionSearch(); } }); panel.add(addSearchButton); //Function Of AddType contentPane.add(panel,BorderLayout.SOUTH); JButton addParentButton = new JButton("ADD TYPE"); //Button Of AddType addParentButton.addActionListener (new ActionListener(){ public void actionPerformed(ActionEvent event){ DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); if(selectedNode==null)return; DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent(); if(parent==null)return; DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New"); //เงื่อนไขเมื่อกดสร้างให้แสดงชื่อ NEW int selectedIndex = parent.getIndex(selectedNode); model.insertNodeInto(newNode,parent,selectedIndex+1); TreeNode[] nodes = model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); } }); panel.add(addParentButton);//Function Of AddTitle JButton addChildButton = new JButton("ADD TITLE"); //Function Of AddTitle addChildButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); if(selectedNode==null)return; DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");//เงื่อนไขเมื่อกดเพิ่มให้สร้างชื่อ NEW model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount()); TreeNode[] nodes = model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); } }); panel.add(addChildButton);//Function Of Delete JButton deleteButton = new JButton("DELETE"); //Button Of Delete deleteButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ DefaultMutableTreeNode selectedNode =(DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if(selectedNode!=null&&selectedNode.getParent()!=null) model.removeNodeFromParent(selectedNode); } }); panel.add(deleteButton); add(panel,BorderLayout.SOUTH); } public void actionSearch(){ Enumeration e = root.breadthFirstEnumeration(); boolean FIND = false; while(e.hasMoreElements()){ DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.nextElement(); if(node.getUserObject().equals(textSearch.getText())) FIND=true; } if(FIND) //เงื่อนไขของการค้นหาในการสร้างMessage box JOptionPane.showMessageDialog(null, "พบ " + textSearch.getText() + "ในร้าน HomeCatoon"); else JOptionPane.showMessageDialog(null,"ไม่พบ " + textSearch.getText() + "ในร้าน HomeCatoon"); } }
}
|