基于eclipse+swing+mysql的汽车保养记录系统的开发2-----系统主界面的实现

目标:实现汽车保养记录系统主界面,包含:

1:实现一个系统菜单

2:实现一个添加客户信息条目

3:实现一个查询客户信息条目

4:实现一个仓库货物信息条目

5:实现一个返回登录界面条目

6:系统主界面实现的效果图如下:
 

1:系统主界面的实现

1.1:带有背景图的窗口

在source.view上右键,选择new->class,创建一个名为MainWindow的java文件。

在MainWindow.java中我们也是以一个init()接口来实现系统主界面的布局。

下面的代码实现了一个带有背景图的窗口,

package source.view;

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

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MainWindow extends JFrame implements ActionListener{
	Dimension dimension = new Dimension(800, 500);
	ImageIcon mainImageIcon = new ImageIcon("image/mainbg.jpg");
	Image icon = new ImageIcon("image/icon.png").getImage();
	JLabel label;
	
	public MainWindow(){
		init();
		setSize(dimension);
		setIconImage(icon);
		setForeground(Color.yellow);
		setLocationRelativeTo(null);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args){
		new MainWindow();
	}

	private void init() {
		// TODO Auto-generated method stub
		setTitle("瑞兴汽车保养系统");
		setBackground(new Color(212,232,248));
		label = new JLabel(mainImageIcon);

		add(label);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

}

效果如下图:

PS:系统主界面的背景图实现方式和登录界面的实现方式有所差异,主要是因为系统主界面中不需要数据输入,因此就使用了一个Label来显示背景图。而在登录界面中,我们需要输入用户名、密码等数据,如果使用Label来实现背景图效果的话,会存在一个层叠关系,导致文本框等控件不会显示,或者说显示文本框等控件的时候,背景图无法显示,因此,在登录界面采用绘制的方式显示背景图。

1.2:实现系统菜单

系统菜单的实现比较简单,就是先实例化一个JMenuBar类,然后向JMenuBar中添加一个JMenu菜单,再想JMenu菜单中添加各JMenuItem,向init()接口中添加如下代码即可实现:

        addCusItem = new JMenuItem("添加客户信息");
		addCusItem.setFont(new Font("宋体", Font.PLAIN, 16));
		//modifyCusInfoItem = new JMenuItem("修改客户信息");
		//modifyCusInfoItem.setFont(new Font("宋体", Font.PLAIN, 16));
		inquriyCusInfoItem = new JMenuItem("查询客户信息");
		inquriyCusInfoItem.setFont(new Font("宋体", Font.PLAIN, 16));
		storeHouseItem = new JMenuItem("仓库货物信息");
		storeHouseItem.setFont(new Font("宋体", Font.PLAIN, 16));
		addCusItem.addActionListener(this);
		//modifyCusInfoItem.addActionListener(this);
		inquriyCusInfoItem.addActionListener(this);
		storeHouseItem.addActionListener(this);
		returnLogin =new JMenuItem("返回登录界面");
		returnLogin.setFont(new Font("宋体", Font.PLAIN, 16));
		returnLogin.addActionListener(this);
		cusInfoMenu.add(addCusItem);
		//cusInfoMenu.add(modifyCusInfoItem);
		cusInfoMenu.add(inquriyCusInfoItem);
		cusInfoMenu.add(storeHouseItem);
		cusInfoMenu.add(returnLogin);

		menuBar.add(cusInfoMenu);
		setJMenuBar(menuBar);

实现的效果如下图:

1.3:实现返回登录界面

返回登录界面的效果就是不显示系统主界面,而显示登录界面,代码实现比较简单,如下:

@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == returnLogin){
			dispose();
			new Login();
		}
	}

1.4:实现添加客户信息

1.4.1:布局界面的实现

添加客户信息界面主要是由一系列的标签以及文本框组成,以及一个添加按钮,将客户信息提交到数据库,一个关闭按钮,回退到系统主界面。

在source.view上右键,选择new->class,创建一个名为AddCustomer的java文件,其实现如下:

public class AddCustomer extends JFrame implements ActionListener{
	private JPanel contentPane;
	private JTextField nameTextField;
	private JTextField carIDTextField;
	private JLabel nameLabel1;
	private JLabel carIDLabel1;
	private JLabel phoneNumLabel;
	private JLabel carVinNoLabel;
	private JTextField phoneNumTextField;
	private JTextField carVinNoTextField;
	private JLabel buyDateLabel;
	private JRadioButton radioButton,radioButton_1;
	private JTextField buyDateTextField;
	private JLabel meliageLabel;
	private JTextField meliageTextField;
	private JLabel latestMentenanceDateLabel;
	private JTextField latestMentenanceDateTextField;
	private JLabel latestItemLabel;
	private JTextField latestItemTextField;
	private JLabel priceLabel;
	private JTextField priceTextField;
	JComboBox comboBox_1;
	JPanel centerpPanel;
	JButton addButton,returnButton;
	Image icon = new ImageIcon("image/icon.jpg").getImage();
	
	public static void main(String[] args){
		new AddCustomer();
	}
	
	public AddCustomer(){
		setTitle("增加客户信息");
		setSize(555, 433);
		setIconImage(icon);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		centerpPanel = new JPanel();
		centerpPanel.setLayout(null);
		nameLabel1 = new JLabel("姓  名:");
		nameLabel1.setFont(new Font("宋体", Font.PLAIN, 20));
		nameLabel1.setBounds(50, 26, 116, 42);
		centerpPanel.add(nameLabel1);
		
		nameTextField = new JTextField();
		nameTextField.setBounds(125, 35, 120, 30);
		centerpPanel.add(nameTextField);
		nameTextField.setColumns(10);
		
		carIDLabel1 = new JLabel("车牌号:");
		carIDLabel1.setFont(new Font("宋体", Font.PLAIN, 20));
		carIDLabel1.setBounds(260, 26, 116, 42);
		centerpPanel.add(carIDLabel1);
		
		carIDTextField = new JTextField();
		carIDTextField.setBounds(335, 35, 120, 30);
		centerpPanel.add(carIDTextField);
		carIDTextField.setColumns(10);
		
		phoneNumLabel = new JLabel("电  话:");
		phoneNumLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		phoneNumLabel.setBounds(50, 80, 116, 42);
		centerpPanel.add(phoneNumLabel);
		
		phoneNumTextField = new JTextField();
		phoneNumTextField.setBounds(125, 88, 120, 30);
		centerpPanel.add(phoneNumTextField);
		phoneNumTextField.setColumns(10);
	
		carVinNoLabel = new JLabel("车架号:");
		carVinNoLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		carVinNoLabel.setBounds(260, 80, 116, 42);
		centerpPanel.add(carVinNoLabel);
		
		carVinNoTextField = new JTextField();
		carVinNoTextField.setBounds(335, 88, 120, 30);
		centerpPanel.add(carVinNoTextField);
		carVinNoTextField.setColumns(10);

		buyDateLabel = new JLabel("购车日期:");
		buyDateLabel.setFont(new Font("宋体", Font.PLAIN, 15));
		buyDateLabel.setBounds(50, 140, 137, 42);
		centerpPanel.add(buyDateLabel);
		
		buyDateTextField = new JTextField();
		buyDateTextField.setBounds(125, 145, 120, 30);
		centerpPanel.add(buyDateTextField);
		buyDateTextField.setColumns(10);
		
		meliageLabel = new JLabel("里  程:");
		meliageLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		meliageLabel.setBounds(260, 140, 116, 42);
		centerpPanel.add(meliageLabel);
		
		meliageTextField = new JTextField();
		meliageTextField.setBounds(335, 145, 120, 30);
		centerpPanel.add(meliageTextField);
		meliageTextField.setColumns(10);
		
		latestMentenanceDateLabel = new JLabel("保养日期:");
		latestMentenanceDateLabel.setFont(new Font("宋体", Font.PLAIN, 15));
		latestMentenanceDateLabel.setBounds(50, 193, 137, 42);
		centerpPanel.add(latestMentenanceDateLabel);
		
		latestMentenanceDateTextField = new JTextField();
		latestMentenanceDateTextField.setBounds(125, 200, 120, 30);
		centerpPanel.add(latestMentenanceDateTextField);
		latestMentenanceDateTextField.setColumns(10);
		
		latestItemLabel = new JLabel("保养项目:");
		latestItemLabel.setFont(new Font("宋体", Font.PLAIN, 15));
		latestItemLabel.setBounds(50, 243, 137, 42);
		centerpPanel.add(latestItemLabel);
		
		latestItemTextField = new JTextField();
		latestItemTextField.setBounds(125, 248, 330, 30);
		centerpPanel.add(latestItemTextField);
		latestItemTextField.setColumns(10);
		
		priceLabel = new JLabel("价格:");
		priceLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		priceLabel.setBounds(260, 193, 137, 42);
		centerpPanel.add(priceLabel);
		
		priceTextField = new JTextField();
		priceTextField.setBounds(335, 200, 120, 30);
		centerpPanel.add(priceTextField);
		priceTextField.setColumns(10);
		
		JPanel panel = new JPanel();
		panel.setBounds(362, 85, 120, 30);
		centerpPanel.add(panel);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		getContentPane().add(centerpPanel,BorderLayout.CENTER);
		
		JPanel panel2=new JPanel();
		panel2.setLayout(new FlowLayout());
		addButton=new JButton("添加");
		addButton.setFont(new Font("宋体", Font.PLAIN, 20));
		addButton.addActionListener(this);
		returnButton=new JButton("关闭");
		returnButton.setFont(new Font("宋体", Font.PLAIN, 20));
		returnButton.addActionListener(this);
		panel2.add(addButton);
		panel2.add(returnButton);
		add(panel2,BorderLayout.SOUTH);
		setVisible(true);
	}
}	

AddCustomer.java文件的实现比较直接,就是一堆标签以及对应的文本框的堆叠。此外,简单说明一下setBounds(a, b, c, d)四个参数的代表的意思,a表示距离做边框的距离,b代表距离上边框的距离,c代表控件所占的长度,d代表控件所占的宽度,在布局控件的时候,需要在稿纸上计算出每个控件的大概位置,感觉显示的美观即可。

1.4.2:按钮功能的实现

添加客户信息面板的按钮有两个:添加按钮用于提交信息,关闭按钮用于返回主界面。

关闭按钮的实现比较简单,如下:

@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		CustomerInfo customerInfo = new CustomerInfo();
		
		if(e.getSource()== returnButton){
			dispose();
			new MainWindow();
		}
}

就是关闭当前窗口,然后创建一个新的系统主界面窗口。

提交按钮的公交比较复杂,需要统计各文本框的内容,然后将其填充的Customer的实例中,传递给mysql数据库解析存储。

此外,需要注意的是,在统计各文本框内容的时候,需要判空处理,否则会引起异常,其实现如下:

if(e.getSource() == addButton){
			String cusName = nameTextField.getText().trim();
			String carID = carIDTextField.getText().trim();
			String phoneNum = phoneNumTextField.getText().trim();
			String carVinNo = carVinNoTextField.getText().trim();
			String buyDay = buyDateTextField.getText().trim();
			String meliageString = meliageTextField.getText().trim();
			String latestMentenanceDay = latestMentenanceDateTextField.getText().trim();
			String priceString = priceTextField.getText().trim();
			String latestItem = latestItemTextField.getText().trim();
			Date buyDate = null;
			try{
				buyDate = Dao.getStringToDate(buyDay);
			}catch (Exception e1) {
				e1.printStackTrace();
			}
			Date latestMentenanceDate = null;
			try{
				if(latestMentenanceDay.length() != 0){
					latestMentenanceDate = Dao.getStringToDate(latestMentenanceDay);
				}
			}catch (Exception e1) {
				e1.printStackTrace();
			}
			int meliage = 0;
			try{
				if(meliageString.length() !=0)
					meliage = Integer.parseInt(meliageString);
			}catch (NumberFormatException e2) {
			    e2.printStackTrace();
			}
			int price = 0;
			try{
				if(priceString.length() !=0)
					price = Integer.parseInt(priceString);
			}catch (NumberFormatException e2) {
			    e2.printStackTrace();
			}
			
			
			if(cusName.equals("")||meliageString.equals("")||phoneNum.equals("")||carID.equals("")||latestItem.equals("")||carVinNo.equals("")||buyDate.equals("")||priceString.equals("")||latestMentenanceDay.equals("")){
				JOptionPane.showMessageDialog(this,"请输入正确的信息");
				return;
			}
			else if(CustomerDao.selectCustomerByCarID(carID)!=null && (CustomerDao.selectCustomerByCarID(carID).size() != 0)){
				JOptionPane.showMessageDialog(this,"该编号已存在");
				return;
			}else{
				customerInfo.setCusName(cusName);
				if(carID.length() != 0)
					customerInfo.setCarID(carID);
				if(phoneNum.length() !=0)
					customerInfo.setPhoneNum(phoneNum);
				customerInfo.setCarVinNo(carVinNo);
				customerInfo.setBuyDate(buyDate);
				customerInfo.setMeliage(meliage);
				if(latestMentenanceDay.length() != 0)
					customerInfo.setLatestMentenanceDate(latestMentenanceDate);
				if(latestItem.length()!=0)
					customerInfo.setLatestItem(latestItem);
				customerInfo.setPrice(price);
				System.out.println("增加新客户。。。");
				System.out.println(customerInfo);
				int result = CustomerDao.insertCustomerInfo(customerInfo);
				if (result!=0) {
					JOptionPane.showMessageDialog(this, "修改成功!");
					dispose();//添加成功后关闭面板,返回主界面
					new MainWindow();
				} else {
					JOptionPane.showMessageDialog(this, "修改失败!");
				}
			}
		}

需要注意的是,在日期输入的时候,需要满足固定的日期格式,否则会提示错误,如下:

关于检查日期格式的代码(source.Dao.Dao.java)实现如下:

//String 型转化为Date型
		public static Date getStringToDate(String date){
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			Date dt=new Date();
			try {
				dt=sdf.parse(date);
			} catch (ParseException e) {
				JOptionPane.showMessageDialog(null, "请输入正确的日期格式!格式如2016-01-01");
			}
			
			return (new java.sql.Date(dt.getTime()));
		}

以及将Date类型的数据转换成String类型的实现如下:

		//Date型转化为String 型
		public static String getDateToString(Date date){
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			String string =new String();
			string=sdf.format(date);
			return string;
		}

1.4.3:CustomerInfo model的实现

在窗口控件与mysql数据库交互的过程中,最主要的一个数据类型就是CustomerInfo类型,主要是用于实现客户的各种统计信息,在source.model上右键,选择new->class,创建一个名为CustomerInfo的java文件,CustomerInfo的实现如下:

package source.model;

import java.util.Date;

public class CustomerInfo {
	private String cusName;//车主姓名
	private String carID;//车牌号
	private String phoneNum;//联系方式
	private String carVinNo;//车架号
	private Date buyDate;//购车日期
	private int meliage;//行车里程
	private Date latestMentenanceDate;//最近的保养日期
	private String latestItem;//最近的保养项目
	private int price;//保养价格
	
	public String getCusName(){
		return cusName;
	}
	public void setCusName(String cusName){
		this.cusName = cusName;
	}
	
	public String getCarID(){
		return carID;
	}
	public void setCarID(String carID){
		this.carID = carID;
	}
	
	public String getPhoneNum(){
		return phoneNum;
	}
	public void setPhoneNum(String phoneNum){
		this.phoneNum = phoneNum;
	}
	
	public String getCarVinNo(){
		return carVinNo;
	}
	public void setCarVinNo(String carVinNo){
		this.carVinNo = carVinNo;
	}
	
	public Date getBuyDate(){
		return buyDate;
	}
	public void setBuyDate(Date buyDate){
		this.buyDate = buyDate;
	}
	
	public int getMeliage(){
		return meliage;
	}
	public void setMeliage(int meliage){
		this.meliage = meliage;
	}
	
	public Date getLatestMentenanceDate(){
		return latestMentenanceDate;
	}
	public void setLatestMentenanceDate(Date latestMentenanceDate){
		this.latestMentenanceDate = latestMentenanceDate;
	}
	
	public int getPrice(){
		return price;
	}
	public void setPrice(int price){
		this.price = price;
	}
	
	public String getLatestItem(){
		return latestItem;
	}
	public void setLatestItem(String latestItem){
		this.latestItem = latestItem;
	}

	public String toString(){
		return "CustomerInfo [cusName=" + cusName + ", carID=" + carID + ", phoneNum=" + phoneNum
				 + ", carVinNo=" + carVinNo + ", buyDate=" + buyDate + ", meliage= " + meliage
				 + ", latestMentenanceDate=" +latestMentenanceDate + ", latestItem=" + latestItem + ", price=" + price + "]";
	}

}

实现上述代码后,添加客户信息的窗口已经实现,效果如下:

1.4.4 查询客户信息是否在数据库中存在

在实现添加按钮功能的时候,需要向数据库查询客户信息是否已经存在。由于对一辆汽车来说车牌就是其身份证,因此本系统选择车牌作为是否应在数据库中存在客户信息的依据。

在source.Dao上右键,选择new->class,创建一个名为CustomerDao的java文件,并在其实现一个检查客户信息是否在数据库存在的接口,如下:

public static List<CustomerInfo> selectCustomerByCarID(String carID) {
		// TODO Auto-generated method stub
		List<CustomerInfo> cusList = new ArrayList<CustomerInfo>();
		CommonDao dao = new CommonDao();
		String sql = "select cusName, carID, phoneNum, carVinNo, buyDate, meliage, latestMentenanceDate, price, latestItem from customerInfo where carID='"
					+ carID+ "'";
		ResultSet rs = dao.query(sql);
		try{
			while(rs.next()){
				CustomerInfo customer = new CustomerInfo();
				if((rs.getString("cusName").length() != 0))
					customer.setCusName(rs.getString("cusName"));
				if((rs.getString("carID")).length() != 0)
					customer.setCarID(rs.getString("carID"));
				if((rs.getString("phoneNum")).length() != 0)
					customer.setPhoneNum(rs.getString("phoneNum"));
				if((rs.getString("carVinNo")).length() != 0)
					customer.setCarVinNo(rs.getString("carVinNo"));
				if((rs.getDate("buyDate")) != null)
					customer.setBuyDate(rs.getDate("buyDate"));
				customer.setMeliage(rs.getInt("meliage"));
				if((rs.getDate("latestMentenanceDate")) != null)
					customer.setLatestMentenanceDate(rs.getDate("latestMentenanceDate"));
				customer.setPrice(rs.getInt("price"));
				if((rs.getString("latestItem")) != null)
					customer.setLatestItem(rs.getString("latestItem"));
				cusList.add(customer);
				System.out.println(customer);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		dao.close();
		return cusList;
	}

1.4.5:向数据库中添加新的客户信息

如果客户信息在数据库中不存在,就像数据库中插入新的客户信息,其在source.Dao.CustomerDao.java中的实现如下:

	public static int insertCustomerInfo(CustomerInfo customerInfo) {
		// TODO Auto-generated method stub
		int result = 0;
		CommonDao dao = new CommonDao();
		String sql = "insert into customerInfo values(?,?,?,?,?,?,?,?,?)";
		try{
			result = dao.update(sql, customerInfo.getCusName(), customerInfo.getCarID(),customerInfo.getPhoneNum(),customerInfo.getCarVinNo(),
					customerInfo.getBuyDate(),customerInfo.getMeliage(),customerInfo.getLatestMentenanceDate(),customerInfo.getPrice(),customerInfo.getLatestItem());
		}catch(Exception e){
			e.printStackTrace();
		}
		dao.close();
		return result;
	}

关于查询客户信息的实现,在后续文章中继续讲解......

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值