JavaSwing+derby通讯录

JavaSwing+derby通讯录_第1张图片

此通讯录为sun公司在2006年公布,它的优点有:

分层结构如上图

1.AddressFrame为主窗体它里面装入三个窗体AddressListPanel-AddressActionPanel-AddressPanel

2.AddressListPanel负责装入列表内容;AddressActionPanel负责按钮监听;AddressPanel负责编辑

3.AddressFrame负责连接数据库AddressDao

4.AddressDao又使用了Configuration.properties外部配置文件

总结:一:思路非常之清晰,就像一位朋友所说,拍《盗梦空间》的那位仁兄导演应该是学过编程的。

            二:此系统看似很小,里面五脏俱全,如果每个语句与方法一句一句的分析与参透,将会受益非浅!


完整源码请在此下载:http://download.csdn.net/detail/youyigong/3779854    同时我将此打包,可以独立运行

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class AddressFrame extends JFrame implements ActionListener,	ListSelectionListener{
	private AddressActionPanel addressActionPanel;
	private AddressListPanel addressListPanel;
	private AddressPanel addressPanel;
	private AddressDao db;
	private WindowAdapter windowAdapter;
	private int selectedEntry = -1;
	class WindowCloser extends WindowAdapter{
		public void windowClosing(WindowEvent e){
			db.disconnect();
		}
	}
	
	public AddressFrame(){
		initComponents();
		loadFrameIcon();
		windowAdapter = new WindowCloser();
		this.addWindowListener(windowAdapter);
		
		db = new AddressDao();
		db.connect();
		
		addressActionPanel.addActionListener(this);
		
		addressPanel.setEditable(false);
		List<ListEntry> entries = db.getListEntries();
        addressListPanel.addListEntries(entries);
        addressListPanel.addListSelectionListener(this);
		
	}

	private void loadFrameIcon() {//设置软件图标
		URL imgUrl = null;
		ImageIcon imgIcon = null;
		imgUrl = AddressFrame.class.getResource("icon/5.png");
		imgIcon = new ImageIcon(imgUrl);
		Image img = imgIcon.getImage();
		this.setIconImage(img);
	}

	private void initComponents() {//所有控件添加至主窗体
		addressActionPanel = new AddressActionPanel();
		addressListPanel = new AddressListPanel();
		addressPanel = new AddressPanel();
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("通讯录(derby版)");
		this.getContentPane().add(addressActionPanel, BorderLayout.SOUTH);
		this.getContentPane().add(addressPanel,BorderLayout.CENTER);
		this.getContentPane().add(addressListPanel,BorderLayout.WEST);
		pack();
		
	}
	private void cancelAddress(){//取消按钮
		addressPanel.clear();
		addressPanel.setEditable(false);
		ListEntry entry = addressListPanel.getSelectedListEntry();
		if(entry != null){
			int id = entry.getId();
			Address address = db.getAddress(id);
			addressPanel.setAddress(address);
		}
	}
	
	private void newAddress(){//新建时
		addressPanel.clear();
		addressPanel.setEditable(true);
	}
	
	private void deleteAddress(){
		int id = addressPanel.getId();
		if(id != -1){
			db.deleteRecord(id);
			int selectedIndex = addressListPanel.deleteSelectedEntry();
			if(selectedIndex >= 0 ){
				selectedIndex = addressListPanel.setSelectedIndex(selectedIndex);
				ListEntry entry = addressListPanel.getSelectedListEntry();
				if(entry != null){
					id = entry.getId();
					Address address = db.getAddress(id);
					addressPanel.setAddress(address);
				}else{
					addressPanel.clear();
				}
			}
		}else{
			addressPanel.clear();
		}
		addressPanel.setEditable(false);
	}
	
	private void editAddress(){//编辑时
		int selected = addressListPanel.getSelectedIndex();
		if(selected > 0){//只要选择还有
			addressPanel.setEditable(true);
		}
	}
	private void saveAddress() {
        if (addressPanel.isEditable()) {
            Address address = addressPanel.getAddress();
            int id = address.getId();
                String lname = address.getLastName();
                String fname = address.getFirstName();
                String mname = address.getMiddleName();
            if (id == -1) {
                id = db.saveRecord(address);
                address.setId(id);

                ListEntry entry = new ListEntry(lname, fname, mname, id);
                addressListPanel.addListEntry(entry);

            } else {
                db.editRecord(address);
                ListEntry entry = addressListPanel.getSelectedListEntry();
                entry.setFirstName(fname);
                entry.setMiddleName(mname);
                entry.setLastName(lname);
                addressListPanel.repaint();
            }
            addressPanel.setEditable(false);
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        System.out.println("ActionEvent: " + actionCommand);
        if (actionCommand.equalsIgnoreCase("CANCEL_ADDRESS")) {
            cancelAddress();
        } else if (actionCommand.equalsIgnoreCase("NEW_ADDRESS")) {
            newAddress();
        } else if (actionCommand.equalsIgnoreCase("DELETE_ADDRESS")) {
            deleteAddress();
        } else if (actionCommand.equalsIgnoreCase("EDIT_ADDRESS")) {
            editAddress();
        } else if (actionCommand.equalsIgnoreCase("SAVE_ADDRESS")) {
            saveAddress();
        }
    }

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting()) {
            return;
        }
        JList entryList = (JList) e.getSource();
        selectedEntry = entryList.getSelectedIndex();
        ListEntry entry = (ListEntry)entryList.getSelectedValue();
        if (entry != null) {
            int id = entry.getId();
            Address address = db.getAddress(id);
            addressPanel.setAddress(address);
        } else {
            addressPanel.clear();
        }
		
	}
	
	public static void main(String[] args) {
		AddressFrame app = new AddressFrame();
        app.setVisible(true);
	}

	
}



你可能感兴趣的:(String,null,delete,Class,Derby,通讯)