簡易名片管理 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){

}

}

}

發佈了0 篇原創文章 · 獲贊 1 · 訪問量 6819
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章