Java---设计模式---遇见MVC-②

表现层:展示数据、人机交互、收集参数调用逻辑层。

主界面:ListPanl

package cn.hncu.addr.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import cn.hncu.addr.business.MyList;
import cn.hncu.addr.vo.AddressUser;

public class ListPanl extends JPanel {
	private JFrame mainFram=null;
	private JList listAddrs = null;
	//注入
	private MyList myList = new MyList();
	/**
	 * Create the panel.
	 */
	public ListPanl(JFrame mainFram) {
		this.mainFram = mainFram;
		initContentPane();
		initListAddrsData();
	}
	private void initListAddrsData() {
		listAddrs.setListData(myList.toArray());
	}
	
	public ListPanl(JFrame mainFram, List<AddressUser> resList) {
		this.mainFram = mainFram;
		initContentPane();
		listAddrs.setListData(resList.toArray());
	}
	
	
	public void initContentPane(){
		setMinimumSize(new Dimension(800, 600));
		setBackground(new Color(255, 255, 255));
		setLayout(null);
		
		JLabel label = new JLabel("我的地址App");
		label.setBounds(293, 25, 219, 47);
		label.setForeground(Color.RED);
		label.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 36));
		add(label);
		
		JButton btnToAddPanel = new JButton("添加...");
		btnToAddPanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ListPanl.this.mainFram.setContentPane(new AddPanel(ListPanl.this.mainFram));
				ListPanl.this.mainFram.validate();
			}
		});
		btnToAddPanel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnToAddPanel.setBounds(129, 378, 115, 54);
		add(btnToAddPanel);
		
		JButton btnToDeletePanel = new JButton("删除...");
		btnToDeletePanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//收集数据
				Object objAddressUser = listAddrs.getSelectedValue();
				if (objAddressUser==null) {
					JOptionPane.showMessageDialog(null, "请选中你要删除的一条记录");
					return;
				}
				AddressUser oldAddressUser = (AddressUser) objAddressUser;
				
				ListPanl.this.mainFram.setContentPane(new DeletePanel(ListPanl.this.mainFram,oldAddressUser));
				ListPanl.this.mainFram.validate();
			}
		});
		btnToDeletePanel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnToDeletePanel.setBounds(270, 378, 115, 54);
		add(btnToDeletePanel);
		
		JButton btnToUpDatePanel = new JButton("修改...");
		btnToUpDatePanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//收集数据
				Object objAddressUser = listAddrs.getSelectedValue();
				if (objAddressUser==null) {
					JOptionPane.showMessageDialog(null, "请选中你要修改的一条记录");
					return;
				}
				AddressUser oldAddressUser = (AddressUser) objAddressUser;
				
				ListPanl.this.mainFram.setContentPane(new UpdatePanel(ListPanl.this.mainFram,oldAddressUser));
				ListPanl.this.mainFram.validate();
			}
		});
		btnToUpDatePanel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnToUpDatePanel.setBounds(412, 378, 115, 54);
		add(btnToUpDatePanel);
		
		JButton btnToQueryPanel = new JButton("查询...");
		btnToQueryPanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ListPanl.this.mainFram.setContentPane(new QueryPanel(ListPanl.this.mainFram));
				ListPanl.this.mainFram.validate();
			}
		});
		btnToQueryPanel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnToQueryPanel.setBounds(542, 378, 115, 54);
		add(btnToQueryPanel);
		
		listAddrs = new JList();
		listAddrs.setBounds(209, 112, 378, 202);
		add(listAddrs);
		listAddrs.setFont(new Font("宋体", Font.BOLD, 16));
		listAddrs.setForeground(new Color(0, 0, 255));
		listAddrs.setBackground(new Color(255, 239, 213));
	}
	
}

添加界面:AddPanel

package cn.hncu.addr.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.hncu.addr.business.MyList;
import cn.hncu.addr.vo.AddressUser;

public class AddPanel extends JPanel {
	private JTextField tfdName;
	private JTextField tfdAge;
	private JTextField tfdAddr;
	
	
	private JFrame mainFram=null;
	private MyList myList = new MyList();

	/**
	 * Create the panel.
	 */
	public AddPanel(JFrame mainFram) {
		this.mainFram = mainFram;
		setMinimumSize(new Dimension(800, 600));
		setBackground(new Color(102, 204, 255));
		setLayout(null);
		
		JLabel label = new JLabel("添加地址");
		label.setForeground(new Color(204, 0, 51));
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(203, 10, 159, 67);
		add(label);
		
		JLabel label_1 = new JLabel("姓名:");
		label_1.setFont(new Font("宋体", Font.PLAIN, 26));
		label_1.setBounds(158, 103, 78, 39);
		add(label_1);
		
		tfdName = new JTextField();
		tfdName.setBounds(233, 103, 159, 39);
		add(tfdName);
		tfdName.setColumns(10);
		
		tfdAge = new JTextField();
		tfdAge.setColumns(10);
		tfdAge.setBounds(233, 164, 159, 44);
		add(tfdAge);
		
		JLabel label_2 = new JLabel("年龄:");
		label_2.setFont(new Font("宋体", Font.PLAIN, 26));
		label_2.setBounds(158, 164, 78, 44);
		add(label_2);
		
		tfdAddr = new JTextField();
		tfdAddr.setColumns(10);
		tfdAddr.setBounds(233, 236, 288, 39);
		add(tfdAddr);
		
		JLabel label_3 = new JLabel("地址:");
		label_3.setFont(new Font("宋体", Font.PLAIN, 26));
		label_3.setBounds(158, 236, 78, 39);
		add(label_3);
		
		JButton btnAdd = new JButton("添加");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//1 收集参数
				String name = tfdName.getText();
				if (name ==null||name.trim().length()==0) {
					JOptionPane.showMessageDialog(null, "名字不能为空!");
					return;
				}
				int age = 0;
				try {
					age = Integer.parseInt(tfdAge.getText().trim());
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null, "年龄只能为数字!");
					return;
				}
				String address = tfdAddr.getText();
			
				//2 组织参数
				AddressUser addressUser = new AddressUser();
				addressUser.setName(name);
				addressUser.setAge(age);
				addressUser.setAddress(address);
				
				//3 调用逻辑层
				boolean boo = myList.add(addressUser);
				
				//4 导向结果界面
				if (boo) {
					AddPanel.this.mainFram.setContentPane(new ListPanl(AddPanel.this.mainFram));
					AddPanel.this.mainFram.validate();
				}else{
					JOptionPane.showMessageDialog(null, "地址信息添加失败!");
				}
			}
		});
		btnAdd.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnAdd.setBounds(143, 326, 93, 44);
		add(btnAdd);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddPanel.this.mainFram.setContentPane(new ListPanl(AddPanel.this.mainFram));
				AddPanel.this.mainFram.validate();
			}
		});
		btnBack.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnBack.setBounds(313, 326, 93, 44);
		add(btnBack);

	}
}

删除界面:DeletePanel

package cn.hncu.addr.ui;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.Font;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import cn.hncu.addr.business.MyList;
import cn.hncu.addr.vo.AddressUser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DeletePanel extends JPanel {
	private JTextField tfdName;
	private JTextField tfdAge;
	private JTextField tfdAddress;

	private JFrame mainFrame = null;
	private AddressUser oldAddressUser = null;
	//注入
	private MyList myList = new MyList();
	/**
	 * Create the panel.
	 * @param oldAddressUser 
	 */
	public DeletePanel(JFrame mainFrame, AddressUser oldAddressUser) {
		this.mainFrame = mainFrame;
		this.oldAddressUser = oldAddressUser;
		setMinimumSize(new Dimension(800, 600));
		setBackground(new Color(255, 255, 255));
		setLayout(null);
		
		JLabel label = new JLabel("删除地址信息");
		label.setForeground(new Color(204, 0, 51));
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(267, 25, 242, 67);
		add(label);
		
		JLabel label_1 = new JLabel("姓名:");
		label_1.setFont(new Font("宋体", Font.PLAIN, 26));
		label_1.setBounds(237, 123, 78, 39);
		add(label_1);
		
		tfdName = new JTextField();
		tfdName.setEditable(false);
		tfdName.setColumns(10);
		tfdName.setBounds(312, 123, 159, 39);
		add(tfdName);
		
		JLabel label_2 = new JLabel("年龄:");
		label_2.setFont(new Font("宋体", Font.PLAIN, 26));
		label_2.setBounds(237, 184, 78, 44);
		add(label_2);
		
		tfdAge = new JTextField();
		tfdAge.setEditable(false);
		tfdAge.setColumns(10);
		tfdAge.setBounds(312, 184, 159, 44);
		add(tfdAge);
		
		JLabel label_3 = new JLabel("地址:");
		label_3.setFont(new Font("宋体", Font.PLAIN, 26));
		label_3.setBounds(237, 256, 78, 39);
		add(label_3);
		
		tfdAddress = new JTextField();
		tfdAddress.setEditable(false);
		tfdAddress.setColumns(10);
		tfdAddress.setBounds(312, 256, 288, 39);
		add(tfdAddress);
		
		JButton btnDelete = new JButton("删除");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean boo = myList.delete(DeletePanel.this.oldAddressUser);
				if (boo) {
					DeletePanel.this.mainFrame.setContentPane(new ListPanl(DeletePanel.this.mainFrame));
					DeletePanel.this.mainFrame.validate();
				}else{
					JOptionPane.showMessageDialog(null, "删除失败,该信息已被删除!");
				}
			}
		});
		btnDelete.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnDelete.setBounds(222, 346, 93, 44);
		add(btnDelete);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeletePanel.this.mainFrame.setContentPane(new ListPanl(DeletePanel.this.mainFrame));
				DeletePanel.this.mainFrame.validate();
			}
		});
		btnBack.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnBack.setBounds(392, 346, 93, 44);
		add(btnBack);
		
		initDeletePanelData();
	}
	private void initDeletePanelData() {
		tfdName.setText(oldAddressUser.getName());
		tfdAge.setText(""+oldAddressUser.getAge());
		tfdAddress.setText(oldAddressUser.getAddress());
	}
}

修改界面:UpdatePanel

package cn.hncu.addr.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.hncu.addr.business.MyList;
import cn.hncu.addr.vo.AddressUser;

public class UpdatePanel extends JPanel {

	
	private JFrame mainFrame=null;
	private AddressUser oldAddressUser = null;
	//注入
	private MyList myList = new MyList();
	private JTextField tfdName;
	private JTextField tfdAge;
	private JTextField tfdAddress;
	/**
	 * Create the panel.
	 */
	public UpdatePanel(JFrame mainFrame, AddressUser oldAddressUser) {
		this.mainFrame = mainFrame;
		this.oldAddressUser = oldAddressUser;
		setLayout(null);
		
		tfdName = new JTextField();
		tfdName.setText((String) null);
		tfdName.setEditable(false);
		tfdName.setColumns(10);
		tfdName.setBounds(311, 137, 159, 39);
		add(tfdName);
		
		JLabel label = new JLabel("修改地址信息");
		label.setForeground(new Color(204, 0, 51));
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(255, 33, 257, 67);
		add(label);
		
		JLabel label_1 = new JLabel("姓名:");
		label_1.setFont(new Font("宋体", Font.PLAIN, 26));
		label_1.setBounds(236, 137, 78, 39);
		add(label_1);
		
		JLabel label_2 = new JLabel("年龄:");
		label_2.setFont(new Font("宋体", Font.PLAIN, 26));
		label_2.setBounds(236, 198, 78, 44);
		add(label_2);
		
		tfdAge = new JTextField();
		tfdAge.setText("0");
		tfdAge.setColumns(10);
		tfdAge.setBounds(311, 198, 159, 44);
		add(tfdAge);
		
		JLabel label_3 = new JLabel("地址:");
		label_3.setFont(new Font("宋体", Font.PLAIN, 26));
		label_3.setBounds(236, 270, 78, 39);
		add(label_3);
		
		tfdAddress = new JTextField();
		tfdAddress.setText((String) null);
		tfdAddress.setColumns(10);
		tfdAddress.setBounds(311, 270, 288, 39);
		add(tfdAddress);
		
		JButton btnUpdate = new JButton("修改");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//1收集参数(姓名,年龄,地址)
				int age = 0;
				try {
					age = Integer.parseInt(tfdAge.getText().trim());
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null, "年龄只能为数字!");
					return;
				}
				String address = tfdAddress.getText();
				
				//2组织参数
				AddressUser addressUser = new AddressUser();
				addressUser.setName(UpdatePanel.this.oldAddressUser.getName());
				addressUser.setAge(age);
				addressUser.setAddress(address);
				
				//3调用逻辑层
				boolean boo = myList.update(addressUser);
				//4导向结果界面
				if (boo) {
					UpdatePanel.this.mainFrame.setContentPane(new ListPanl(UpdatePanel.this.mainFrame));
					UpdatePanel.this.mainFrame.validate();
				}else{
					JOptionPane.showMessageDialog(null, "地址信息修改失败!");
				}
				
			}
		});
		btnUpdate.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnUpdate.setBounds(221, 360, 93, 44);
		add(btnUpdate);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UpdatePanel.this.mainFrame.setContentPane(new ListPanl(UpdatePanel.this.mainFrame));
				UpdatePanel.this.mainFrame.validate();
			}
		});
		btnBack.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnBack.setBounds(391, 360, 93, 44);
		add(btnBack);
		
		initUpdatePanel();
	}
	private void initUpdatePanel() {
		tfdName.setText(oldAddressUser.getName());
		tfdAge.setText(""+oldAddressUser.getAge());
		tfdAddress.setText(oldAddressUser.getAddress());
	}

	
}

查询界面:QueryPanel

package cn.hncu.addr.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.hncu.addr.business.MyList;
import cn.hncu.addr.vo.AddressQueryVO;
import cn.hncu.addr.vo.AddressUser;

public class QueryPanel extends JPanel {
	
	
	private JFrame mainFram=null;
	private MyList myList = new MyList();
	private JTextField tfdName;
	private JTextField tfdAddress;
	private JTextField tfdMinAge;
	private JTextField tfdMaxAge;
	/**
	 * Create the panel.
	 */
	public QueryPanel(JFrame mainFram) {
		setMinimumSize(new Dimension(800, 600));
		this.mainFram = mainFram;
		setLayout(null);
		
		JLabel label = new JLabel("查询地址信息");
		label.setForeground(new Color(204, 0, 51));
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(255, 31, 248, 67);
		add(label);
		
		tfdName = new JTextField();
		tfdName.setColumns(10);
		tfdName.setBounds(164, 137, 159, 39);
		add(tfdName);
		
		JLabel label_1 = new JLabel("姓名:");
		label_1.setFont(new Font("宋体", Font.PLAIN, 26));
		label_1.setBounds(89, 137, 78, 39);
		add(label_1);
		
		tfdAddress = new JTextField();
		tfdAddress.setColumns(10);
		tfdAddress.setBounds(423, 137, 288, 39);
		add(tfdAddress);
		
		JLabel label_2 = new JLabel("地址:");
		label_2.setFont(new Font("宋体", Font.PLAIN, 26));
		label_2.setBounds(348, 137, 78, 39);
		add(label_2);
		
		tfdMinAge = new JTextField();
		tfdMinAge.setColumns(10);
		tfdMinAge.setBounds(216, 249, 159, 44);
		add(tfdMinAge);
		
		JLabel label_3 = new JLabel("最小年龄:");
		label_3.setFont(new Font("宋体", Font.PLAIN, 26));
		label_3.setBounds(89, 249, 153, 44);
		add(label_3);
		
		JLabel label_4 = new JLabel("最大年龄:");
		label_4.setFont(new Font("宋体", Font.PLAIN, 26));
		label_4.setBounds(396, 249, 153, 44);
		add(label_4);
		
		tfdMaxAge = new JTextField();
		tfdMaxAge.setColumns(10);
		tfdMaxAge.setBounds(523, 249, 159, 44);
		add(tfdMaxAge);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				QueryPanel.this.mainFram.setContentPane(new ListPanl(QueryPanel.this.mainFram));
				QueryPanel.this.mainFram.validate();
			}
		});
		btnBack.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnBack.setBounds(396, 361, 93, 44);
		add(btnBack);
		
		JButton btnQuery = new JButton("查询");
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//1收集参数
				String queryName = tfdName.getText().trim();
				String queryAddress = tfdAddress.getText().trim();
				String queryMinAge = tfdMinAge.getText().trim();
				String queryMaxAge = tfdMaxAge.getText().trim();
				
				//2组织参数
				AddressQueryVO queryVO = new AddressQueryVO();
				queryVO.setAddress(queryAddress);
				queryVO.setName(queryName);
				int minAge = 0;
				if (!"".equals(queryMinAge)) {
					try {
						minAge = Integer.parseInt(queryMinAge);
					} catch (NumberFormatException e1) {
						JOptionPane.showMessageDialog(null, "年龄只能为数字!");
						return;
					}
				}
				
				int maxAge = 0;
				if (!"".equals(queryMaxAge)) {
					try {
						maxAge = Integer.parseInt(queryMaxAge);
					} catch (NumberFormatException e1) {
						JOptionPane.showMessageDialog(null, "年龄只能为数字!");
						return;
					}
				}
				queryVO.setAge(minAge);
				queryVO.setAge2(maxAge);
				//3调用逻辑层
				List<AddressUser> resList = myList.query(queryVO);
				//4导向结果界面
				QueryPanel.this.mainFram.setContentPane(new ListPanl(QueryPanel.this.mainFram,resList));
				QueryPanel.this.mainFram.validate();
			}
		});
		btnQuery.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		btnQuery.setBounds(226, 361, 93, 44);
		add(btnQuery);
		
	}
}


猜你喜欢

转载自blog.csdn.net/qq_34928644/article/details/79917243