วันศุกร์ที่ 15 มีนาคม พ.ศ. 2556

SimpleFrame(LAB 2-1)-TreeSearchTest(LAB 2-2)



                                                                     SimpleFrame(LAB 2-1)


import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class SimpleFrame extends JFrame{
 private static final int DEFAULT_WIDTH=400;
 private static final int DEFAULT_HEIGHT=300;
 public SimpleFrame(){
  setTitle("Simple Frame");
  setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
 }
 public static void main(String[]args){
  JFrame frame = new SimpleFrame();
  frame.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e ){
    System.exit(0);
   }
  });
  frame.setVisible(true);
 }
}







                                                                  TreeSearchTest(LAB 2-2)


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;

class TreeSearchTest extends JFrame {
 private DefaultTreeModel model;
 private DefaultMutableTreeNode root;
 private JTree tree;
 private static final int DEFAULT_WIDTH = 300;
 private static final int DEFAULT_HEIGTH = 200;
 Container contentPane = getContentPane();
  JTextField textSearch;
 public TreeSearchTest() {
  setTitle ("TreeSearchTest");
  setSize (DEFAULT_WIDTH,DEFAULT_HEIGTH);
  root =makeSampleTree();
  model = new DefaultTreeModel (root);
  tree = new JTree (model);
  JScrollPane scrollPane = new JScrollPane(tree);
  add(scrollPane,BorderLayout.CENTER);
  makeButtons();
 }
 public DefaultMutableTreeNode makeSampleTree(){
  DefaultMutableTreeNode root = new DefaultMutableTreeNode ("chaopraya university");
  DefaultMutableTreeNode country = new DefaultMutableTreeNode ("nakhonsawan");
  root.add(country);
  DefaultMutableTreeNode state = new DefaultMutableTreeNode ("muang");
  country.add(state);
  DefaultMutableTreeNode city = new DefaultMutableTreeNode ("naimuang");
  state.add(city);
  country = new DefaultMutableTreeNode ("Phechabun");
  root.add(country);
  state = new DefaultMutableTreeNode ("Hongphai");
  country.add(state);
  city = new DefaultMutableTreeNode ("kongtool");
  state.add(city);
  return root;
 }
 public void makeButtons(){
  JPanel panel = new JPanel();
  textSearch = new JTextField(15);
  panel.add(textSearch);
  JButton addSearchButton = new JButton("Search");
  addSearchButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent eventent){
    actionSearch();
   }
  });
  panel.add(addSearchButton);
  contentPane.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)
   JOptionPane.showMessageDialog(null,"พบโหนด" + textSearch.getText()+ "ในTree");
  else JOptionPane.showMessageDialog(null,"ไม่พบโหนด"+ textSearch.getText()+ "ในTree");
 }
 public static void main(String[]args){
  JFrame frame = new TreeSearchTest();
  frame.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e ){
    System.exit(0);
   }
  });
  frame.setVisible(true);
 }
}





ไม่มีความคิดเห็น:

แสดงความคิดเห็น