简易名片管理 java

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Hashtable;
public class cardManage extends JFrame implements ActionListener, ItemListener, ListSelectionListener {

/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
private int favorNum = 2;
private boolean isInput;
private JRadioButton addControl = new JRadioButton("添加"),
lookControl = new JRadioButton("查看"),
deleteControl = new JRadioButton("删除"),
favorButton = new JRadioButton("爱好"),
educationButton = new JRadioButton("学历");
private String strFavor3;
private String strFavor1,strFavor2;
private JButton addButton = new JButton("添加");
private JButton clearButton = new JButton("清空");
private JList store = new JList();//显示名片中所有的联系人;
private DefaultListModel model = new DefaultListModel(); //设置JList模式
private JPanel pWest = new JPanel(),
pSouth = new JPanel();
private JPanel pSouth1 = new JPanel();
private JPanel pSouth2 = new JPanel();
private Box boxWest = Box.createVerticalBox();//创建垂直箱式布局容器;
private Box pCenter = Box.createVerticalBox();//创建垂直箱式布局容器;
private JCheckBox favor1 = new JCheckBox("篮球");
private JCheckBox favor2 = new JCheckBox("音乐");
private Object[] item ={"大专","本科","高中","小学"};//学历
private JComboBox favor3 ;
private JScrollPane storeFather;
private JLabel jlabelName = new JLabel(" 姓名");
private JLabel jlabelAdress = new JLabel(" 地址");
private JLabel jlabelTelephone = new JLabel("联系电话");
private JLabel jlabelEmail = new JLabel("电子邮件");
private JTextField jtextName = new JTextField(11);
private JTextField jtextAdress = new JTextField(11);
private JTextField jtextTelephone = new JTextField(10);
private JTextField jtextEmail = new JTextField(10);
private JPanel namePanel = new JPanel();
private JPanel adressPanel = new JPanel();
private JPanel telephonePanel = new JPanel();
private JPanel emailPanel = new JPanel();
private JPanel buttonPanel = new JPanel();
private JPanel radioButton = new JPanel();
private JPanel button = new JPanel();
private Hashtable<String, People> peopleTable = new Hashtable<String, People>();//存储联系人;
//private People people = new People();
CardLayout cardLayout = new CardLayout();//卡片布局
private class People {
private String name= null;
private String adress = null;
private String telephone = null;
private String email = null;
private String[] favor = new String[favorNum];
private String education = null;
People(){
for(int i= 0;i<favorNum;++i){
this.favor[i]= new String();
}
}
public void setName(String name){
this.name=name;
}
public void setAdress(String adress){
this.adress=adress;
}
public void setTelephone(String telephone){
this.telephone=telephone;
}
public void setEmail(String email){
this.email=email;
}
public void setFavor(String[] favor){
for(int i = 0;i<favor.length;++i){
this.favor[i]=favor[i];
}
}
public void setEducation(String education){
this.education=education;
}
public String getName(){
return name;
}
public String getAdress(){
return adress;
}
public String getTelephone(){
return telephone;
}
public String getEmail(){
return email;
}
public String[] getFavor(){
return favor;
}
public String getEducation(){
return education;
}
}
private void setIsInput(boolean bool){//是所有输入框或按钮变成不可用或可用;
jtextName.setEditable(bool);
jtextAdress.setEditable(bool);
jtextTelephone.setEditable(bool);
jtextEmail.setEditable(bool);
addButton.setEnabled(bool);
clearButton.setEnabled(bool);
favorButton.setEnabled(bool);
educationButton.setEnabled(bool);
favor1.setEnabled(bool);
favor2.setEnabled(bool);
favor3.setEnabled(bool);
isInput = bool;
}
private void clearNull() {
jtextName.setText(null);// TODO Auto-generated method stub
jtextAdress.setText(null);
jtextTelephone.setText(null);
jtextEmail.setText(null);

}
private void showInformation(String str) {
People information =peopleTable.get(str);// TODO Auto-generated method stub
jtextName.setText(information.getName());
jtextAdress.setText(information.getAdress());
jtextTelephone.setText(information.getTelephone());
jtextEmail.setText(information.getEmail());
favor3.setSelectedItem(information.getEducation());
String f[] = new String[favorNum];
for(int i =0;i<favorNum;++i)
f[i] = new String();
if(f[0].equals(favor1.getText()))
favor1.setSelected(true);
else favor1.setSelected(false);
if(f[1].equals(favor2.getText()))
favor2.setSelected(true);
else favor2.setSelected(false);

}
private void addPeople() {
People people = new People();// TODO Auto-generated method stub
people.setName(jtextName.getText());
people.setAdress(jtextAdress.getText());
people.setTelephone(jtextTelephone.getText());
people.setEmail(jtextEmail.getText());
people.setEducation(strFavor3);
String[] str = new String[favorNum];
for(int i =0;i<favorNum;++i)
str[i] = new String();
str[0]=strFavor1;
str[1]=strFavor2;
people.setFavor(str);
String s1 = jtextName.getText();
try{
String s = s1.substring(0, 1);//取姓
peopleTable.put(s, people);//把数据按姓为键值存储到Hashtable
}catch(NullPointerException e){}
model.addElement(s1); //JList对数据的处理,是用它的model来完成的;

}
public void Init(){
ButtonGroup group1 = new ButtonGroup();//按钮组,实现单选功能;
group1.add(addControl);group1.add(lookControl);
ButtonGroup group2= new ButtonGroup();
group2.add(favorButton);group2.add(educationButton);
favor3 = new JComboBox(item);
storeFather = new JScrollPane(store);
pWest.setBorder(BorderFactory.createTitledBorder("选择操作"));
pWest.setSize(50,50);
storeFather.setBorder(BorderFactory.createTitledBorder("名片列表"));
pWest.add(addControl);pWest.add(lookControl);
boxWest.add(pWest);
boxWest.add(Box.createVerticalStrut(8));
boxWest.add(storeFather);
pSouth.setLayout(cardLayout);pSouth.setBorder(BorderFactory.createTitledBorder("名片附加信息"));
pSouth1.add(favor1);pSouth1.add(favor2);
pSouth2.add(favor3);
pSouth.add(pSouth1,"1");pSouth.add(pSouth2,"2");//将组件放到卡片布局容器里
//pCenter.setLayout(new GridLayout(6,2));//中心面板布局;
pCenter.setBorder(BorderFactory.createTitledBorder("名片详细信息"));
namePanel.setLayout(new FlowLayout());
adressPanel.setLayout(new FlowLayout());
telephonePanel.setLayout(new FlowLayout());
emailPanel.setLayout(new FlowLayout());
namePanel.add(jlabelName);namePanel.add(jtextName);
pCenter.add(namePanel);
pCenter.add(Box.createVerticalStrut(6));
adressPanel.add(jlabelAdress);adressPanel.add(jtextAdress);
pCenter.add(adressPanel);
pCenter.add(Box.createVerticalStrut(6));
telephonePanel.add(jlabelTelephone);telephonePanel.add(jtextTelephone);
pCenter.add(telephonePanel);
pCenter.add(Box.createVerticalStrut(6));
emailPanel.add(jlabelEmail);emailPanel.add(jtextEmail);
pCenter.add(emailPanel);
pCenter.add(Box.createVerticalStrut(6));
buttonPanel.add(addButton);buttonPanel.add(clearButton);
pCenter.add(Box.createVerticalStrut(6));
radioButton.setLayout(new BorderLayout());
buttonPanel.setLayout(new FlowLayout());
button.setLayout(new BorderLayout());
radioButton.add(favorButton,BorderLayout.NORTH);
radioButton.add(educationButton,BorderLayout.CENTER);
button.add(buttonPanel,BorderLayout.CENTER);
button.add(radioButton,BorderLayout.WEST);
pCenter.add(button);
pCenter.add(Box.createVerticalStrut(6));
pCenter.add(pSouth);
store.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//设置为单选;
store.setAlignmentY(0);//?????????????????????????????????????????????????????
store.setLayoutOrientation(JList.VERTICAL);//设置垂直放置
store.setVisibleRowCount(1);
store.setModel(model);
}
cardManage(){
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,450);
setTitle("名片信息管理");
Init();
// boxWest.setSize(50,50);
getContentPane().add(pCenter,BorderLayout.CENTER);
getContentPane().add(boxWest,BorderLayout.WEST);
addButton.addActionListener(this);
clearButton.addActionListener(this);
addControl.addActionListener(this);
lookControl.addActionListener(this);
favorButton.addActionListener(this);
educationButton.addActionListener(this);
favor1.addItemListener(this);
favor2.addItemListener(this);
favor3.addItemListener(this);
store.addListSelectionListener(this);
setIsInput(false);
}
public static void main(String[] args) {
cardManage card = new cardManage();// TODO Auto-generated method stub
card.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==addControl){
setIsInput(true);
}// TODO Auto-generated method stub
if(e.getSource()==lookControl){
setIsInput(false);
try {
String str = ((String) store.getSelectedValue()).substring(0,1);
showInformation(str);
}catch(NullPointerException e1){
JOptionPane.showConfirmDialog(null, "无任何联系人或未选择查看的联系人","提示框",JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource()==addButton){
if(!jtextName.getText().equals(null)&&!jtextTelephone.getText().equals(null))//判断姓名和电话是否为空
addPeople();
}
if(e.getSource()==clearButton){
clearNull();
}
if(e.getSource()==favorButton){//卡片布局的响应;
cardLayout.show(pSouth, "1");// TODO Auto-generated method stub
}
if(e.getSource()==educationButton){
cardLayout.show(pSouth, "2");
}

}
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==favor3){//下拉条的响应
strFavor3=String.valueOf(e.getItem());// TODO Auto-generated method stub
}
if(e.getSource()==favor1){
strFavor1 = favor1.getText();
}
if(e.getSource()==favor2){
strFavor2 = favor2.getText();
}
}
@Override
public void valueChanged(ListSelectionEvent e) {//选择JList内容的响应;
if(store.getSelectedIndex()!=-1){

}

}

}

你可能感兴趣的:(java)