第一个java的小东东,还很弱智的,不过会变强的,
- /**
- 用到map,和awt;
- 下一版本要增加,更多属性,用到问文件的读写,对话框
- 问题:文本框双击异常没解决
- */
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- public class TongXunLu{
- Map <String,String> map=new HashMap<String,String>();
- TextField t1=new TextField(10);
- TextField t2=new TextField(10);
- TextField t3=new TextField(10);
- TextField t4=new TextField(10);
- TextArea ta=new TextArea("",5,3,1 );
- public TongXunLu() {
- Frame f=new Frame("电话本1.0(EKOI)");
- //f.setSize(600,400);
- f.setVisible(true);
- f.setLayout(new GridLayout(3,0));
- Panel p1=new Panel();
- Panel p2=new Panel();
- p1.setLayout(new FlowLayout());
- p2.setLayout(new FlowLayout());
- ta.setEditable(false);//*********
- Label l1=new Label("name");
- Label l2=new Label("number");
- Label l3=new Label("name");
- Label l4=new Label("number");
- Button b1=new Button("add");
- Button b2=new Button("search");
- Button b3=new Button("all item");
- b1.addActionListener(new B1Lister());
- b2.addActionListener(new B2Lister());
- b3.addActionListener(new B3Lister());
- p1.add(l1);
- p1.add(t1);
- p1.add(l2);
- p1.add(t2);
- p1.add(b1);
- f.add(p1);
- p2.add(l3);
- p2.add(t3);
- p2.add(b2);
- p2.add(b3);
- f.add(p2);
- f.add(ta);
- f.setResizable(false);
- f.pack();
- f.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(-1);
- }
- }
- );
- }
- public static void main(String []args) throws Exception
- {
- new TongXunLu();
- }
- class B1Lister implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- String str=t1.getText();
- String num=t2.getText();
- t1.setText("");
- t2.setText("");
- map.put(str,num);
- ta.setText("");
- ta.setText("insert ok");
- }
- }
- class B2Lister implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- String str=null;
- String num;
- str=t3.getText();
- t3.setText("");
- num=map.get(str);
- ta.setText("");
- if(num==null)
- ta.setText("对不起,没有记录");
- else
- ta.setText("name: "+str+"/nnum:"+num);
- }
- }
- class B3Lister implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- //Collection <String> c=map.values();
- ta.setText("");
- String str=map.toString();
- String str1=str.substring(1,str.length()-1);
- String[] end=str1.split(",");
- for( int i=0; i<end.length; i++)
- {
- String []t=end[i].split("=");
- ta.append("name:"+t[0]+" number:"+t[1]+"/n");
- //System.out.println("name:"+t[0]+" number:"+t[1]);
- }
- }
- }
- class Dis
- {
- String name;
- int num;
- }
- }