import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.*;
/**
* @author xiewenxuan
*
*/
public class AddFrame extends JFrame implements ActionListener {
JLabel lblID = null, lblName = null;
JTextField txtID = null, txtName = null;
JTextField txtFind = null;
JButton btnAdd = null, btnFind = null,btnView = null;
JComboBox cboFind = null;
Hashtable<String, String> IDName = null;
public AddFrame() {
lblID = new JLabel("学号");
lblName = new JLabel("姓名");
txtID = new JTextField(10);
txtName = new JTextField(10);
btnAdd = new JButton("添加");
btnFind = new JButton("查找");
btnView = new JButton("查看");
cboFind = new JComboBox();
cboFind.addItem("学号");
cboFind.addItem("姓名");
txtFind = new JTextField(10);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.add(lblID);
p1.add(txtID);
p1.add(lblName);
p1.add(txtName);
p1.add(btnAdd);
p2.add(cboFind);
p2.add(btnFind);
p2.add(txtFind);
p2.add(btnView);
this.add(p1, BorderLayout.NORTH);
this.add(p2, BorderLayout.CENTER);
IDName = new Hashtable<String, String>();
btnAdd.addActionListener(this);
btnAdd.addActionListener(this);
this.pack();
btnAdd.addActionListener(this);
btnFind.addActionListener(this);
btnView.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == btnAdd){
String key = this.txtID.getText().trim();
String value = this.txtName.getText().trim();
IDName.put(key, value);
}else{
if(cboFind.getSelectedItem().toString().equals("学号")){
//IDName.get
}else{
Enumeration<String> keys = IDName.keys();
Collection<String> values = IDName.values();
for(String str : values){
if(str.equals(txtFind.getText())){
String findkey = keys.nextElement();
System.out.println("key"+ findkey+"value"+str+"\n");
break;
}
// keys.nextElement();
}
}
}
}
}