java Swing+mysql实现的家庭收支管理系统(个人收支录入和查询)

家庭收支管理系统

这段时间开发了一个由Java swing mysql实现的简单的家庭收支情况管理系统,也可以叫个人消费情况记录软件,功能比较紧凑简单,数据库采用的是mysql,非常适合Java初学者和学生党参看学习。

功能截图

登录页面:
在这里插入图片描述
主菜单页面:
在这里插入图片描述
收支添加:
在这里插入图片描述
收支管理:
在这里插入图片描述
收支项目添加:
在这里插入图片描述
收支项目管理:
在这里插入图片描述

技术点介绍

语言:java swing
jdk: jdk1.8
数据库:mysql
编译工具:eclipse(myeclipse、idea也可以)

源码(关键部分)

1、Dao层,UserDaoimpl.java:

package com.fa.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


import com.fa.dao.UserDao;
import com.fa.dto.User;
import com.fa.util.JdbcUtil;

public class UserDaoImpl implements UserDao{

	public User findUser(String name, String password) throws Exception {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		
		User user=null;
		JdbcUtil ju=new JdbcUtil();
		
		con=ju.getCon();
		String sql="select * from user where u_name=? and u_password=?";
		ps=con.prepareStatement(sql);
		ps.setString(1, name);
		ps.setString(2, password);
		rs=ps.executeQuery();
		while(rs.next()){
			user=new User();//根据u_name和u_password只能确定一个u_id,也就是一条记录
			user.setUserId(rs.getInt(1));
			user.setUserName(rs.getString(2));
			user.setUserPassword(rs.getString(3));
		}
		ju.closeJdbc(rs, ps, con);
		return user;
	}

}

2、用户实体类:User.java

package com.fa.dto;

/**
 * 
 * @author Administrator
 *
 */
public class User {
	private int userId;
	private String userName;
	private String userPassword;
	
	public User(){
		super();
	}
	
	public User(String name,String password){
		this.userName=name;
		this.userPassword=password;
	}
	
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPassword() {
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	public String toString(){
		return ""+this.userId+this.userName+this.userPassword+"";
	}
}

3、数据连接类:JdbcUtil.java

package com.fa.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


/**
 * 
 *
 */

public class JdbcUtil {
	private String url="jdbc:mysql://localhost:3306/familyaccount?characterEncoding=utf8";
	private String user="root";
	private String password="";
	private String jdbcName="com.mysql.jdbc.Driver";
	
	//获取数据库连接,
	//缺陷:每使用一次这个方法就会加载一次驱动
	public Connection getCon() throws Exception{
		Class.forName(jdbcName);
		Connection con=DriverManager.getConnection(url, user, password);
		return con;	
	}
	
	//关闭数据库连接
	//缺陷:rs关闭异常的话,后面的就不能关闭了
	public void closeJdbc(ResultSet rs,Statement st,Connection con) throws Exception{
		if(rs!=null)
			rs.close();
		if(st!=null)
			st.close();
		if(con!=null)
			con.close();
	}
	
//	public static void main(String[] args) {
//		JdbcUtil ju=new JdbcUtil();
//		Connection con=null;
//		try{
//			con=ju.getCon();
//			System.out.println("数据连接成功");
//		}catch (Exception e){
//			e.printStackTrace();
//		}finally{
//			try{
//				ju.closeJdbc(null,null,con);
//			}catch (Exception e){
//				e.printStackTrace();
//			}finally{
//				System.out.println("数据库关闭成功");
//			}
//		}
//	}

}

4、登录界面:LogOnFrm.java

/*
 * LogOnFrm.java
 *
 * Created on __DATE__, __TIME__
 */

package com.fa.view;

import javax.swing.JOptionPane;

import com.fa.dao.impl.UserDaoImpl;
import com.fa.dto.User;
import com.fa.util.StringUtil;

/**
 *
 * @author  __USER__
 */
public class LogOnFrm extends javax.swing.JFrame {

	/** Creates new form LogOnFrm */
	public LogOnFrm() {
		initComponents();
		setLocation(400, 250);
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		userTxt = new javax.swing.JTextField();
		jLabel3 = new javax.swing.JLabel();
		passwordTxt = new javax.swing.JPasswordField();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("\u7ba1\u7406\u5458\u767b\u5f55");
		setResizable(false);

		jLabel1.setFont(new java.awt.Font("幼圆", 1, 24));
		//jLabel1
				//.setIcon(new javax.swing.ImageIcon(
						//"E:\\Workspaces\\MyEclipse 8.5\\FamilyAccount\\images\\money.png")); // NOI18N
		jLabel1.setText("\u5bb6\u5ead\u6536\u652f\u7ba1\u7406\u7cfb\u7edf");

		jLabel2.setFont(new java.awt.Font("幼圆", 0, 14));
		//jLabel2
				//.setIcon(new javax.swing.ImageIcon(
					//	"E:\\Workspaces\\MyEclipse 8.5\\FamilyAccount\\images\\userName.png")); // NOI18N
		jLabel2.setText("\u7528\u6237\u540d");

		userTxt.setFont(new java.awt.Font("宋体", 0, 14));

		jLabel3.setFont(new java.awt.Font("幼圆", 0, 14));
		//jLabel3
				//.setIcon(new javax.swing.ImageIcon(
						//"E:\\Workspaces\\MyEclipse 8.5\\FamilyAccount\\images\\password.png")); // NOI18N
		jLabel3.setText("\u5bc6  \u7801");

		passwordTxt.setFont(new java.awt.Font("宋体", 0, 14));

		jButton1.setFont(new java.awt.Font("幼圆", 1, 14));
		//jButton1
				//.setIcon(new javax.swing.ImageIcon(
					//	"E:\\Workspaces\\MyEclipse 8.5\\FamilyAccount\\images\\login.png")); // NOI18N
		jButton1.setText("\u767b\u5f55");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jButton2.setFont(new java.awt.Font("幼圆", 1, 14));
		//jButton2
			//	.setIcon(new javax.swing.ImageIcon(
				//		"E:\\Workspaces\\MyEclipse 8.5\\FamilyAccount\\images\\reset.png")); // NOI18N
		jButton2.setText("\u91cd\u7f6e");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				129,
																				129,
																				129)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								false)
																						.addGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								layout
																										.createSequentialGroup()
																										.addComponent(
																												jButton1)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												Short.MAX_VALUE)
																										.addComponent(
																												jButton2))
																						.addGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								layout
																										.createSequentialGroup()
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addComponent(
																																jLabel2)
																														.addComponent(
																																jLabel3))
																										.addGap(
																												34,
																												34,
																												34)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING,
																																false)
																														.addComponent(
																																passwordTxt,
																																0,
																																0,
																																Short.MAX_VALUE)
																														.addComponent(
																																userTxt,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																120,
																																Short.MAX_VALUE)))))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				100,
																				100,
																				100)
																		.addComponent(
																				jLabel1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				277,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap(123, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(50, 50, 50)
										.addComponent(jLabel1)
										.addGap(26, 26, 26)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																userTxt,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(31, 31, 31)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel3)
														.addComponent(
																passwordTxt,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(62, 62, 62)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jButton1)
														.addComponent(jButton2))
										.addContainerGap(81, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		String logName = userTxt.getText();
		String logPassword = new String(passwordTxt.getPassword());

		if (StringUtil.isEmpty(logName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空!");
			return;
		}
		if (StringUtil.isEmpty(logPassword)) {
			JOptionPane.showMessageDialog(null, "密码不能为空!");
			return;
		}

		UserDaoImpl udi = new UserDaoImpl();
		User logUser = null;
		try {
			logUser = udi.findUser(logName, logPassword);
			if (logUser != null) { //为什么logUser!=null就不行,logUser.getUserId()!=0
				//JOptionPane.showMessageDialog(null,"登录成功");
				this.dispose();
				new MainFrm().setVisible(true);
			} else {
				JOptionPane.showMessageDialog(null, "用户名或密码错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "登录失败");
		}
	}

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		this.userTxt.setText("");
		this.passwordTxt.setText("");
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new LogOnFrm().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPasswordField passwordTxt;
	private javax.swing.JTextField userTxt;
	// End of variables declaration//GEN-END:variables

}

5、主页面:MainFrm.java:

/*
 * MainFrm.java
 *
 * Created on __DATE__, __TIME__
 */

package com.fa.view;

import javax.swing.JOptionPane;

/**
 *
 * @author  __USER__
 */
public class MainFrm extends javax.swing.JFrame {

	/** Creates new form MainFrm */
	public MainFrm() {
		initComponents();
		this.setExtendedState(MAXIMIZED_BOTH);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jButton3 = new javax.swing.JButton();
		jButton4 = new javax.swing.JButton();
		jDesktopPane1 = new javax.swing.JDesktopPane();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("\u5bb6\u5ead\u6536\u652f\u7ba1\u7406\u7cfb\u7edf");
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				formWindowClosing(evt);
			}
		});

		jButton1.setFont(new java.awt.Font("幼圆", 1, 18));
		//jButton1
			//	.setIcon(new javax.swing.ImageIcon(
				//		"E:\\Workspaces\\MyEclipse 8.5\\FamilyAccount\\images\\add.png")); // NOI18N
		jButton1.setText("\u6536\u652f\u6dfb\u52a0");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});
		jPanel1.add(jButton1);

		jButton2.setFont(new java.awt.Font("幼圆", 1, 18));
		//jButton2
			//	.setIcon(new javax.swing.ImageIcon(
				//		"E:\\Workspaces\\MyEclipse 8.5\\FamilyAccount\\images\\bookManager.png")); // NOI18N
		jButton2.setText("\u6536\u652f\u7ba1\u7406");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});
		jPanel1.add(jButton2);

		jButton3.setFont(new java.awt.Font("幼圆", 1, 18));
		//jButton3
			//	.setIcon(new javax.swing.ImageIcon(
				//		"E:\\Workspaces\\MyEclipse 8.5\\FamilyAccount\\images\\edit.png")); // NOI18N
		jButton3.setText("\u9879\u76ee\u6dfb\u52a0");
		jButton3.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton3ActionPerformed(evt);
			}
		});
		jPanel1.add(jButton3);

		jButton4.setFont(new java.awt.Font("幼圆", 1, 18));
		//jButton4
				//.setIcon(new javax.swing.ImageIcon(
					//	"E:\\Workspaces\\MyEclipse 8.5\\FamilyAccount\\images\\bookTypeManager.png")); // NOI18N
		jButton4.setText("\u9879\u76ee\u7ba1\u7406");
		jButton4.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton4ActionPerformed(evt);
			}
		});
		jPanel1.add(jButton4);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 550,
				Short.MAX_VALUE).addComponent(jDesktopPane1,
				javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(
												jPanel1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jDesktopPane1,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												334, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		AccountMngInterFrm accountmngfrm = new AccountMngInterFrm();
		this.jDesktopPane1.add(accountmngfrm);
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		accountAddInterFrm accountaddfrm = new accountAddInterFrm();
		this.jDesktopPane1.add(accountaddfrm);
	}

	private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
		ItemMngInterFrm itemmngfrm = new ItemMngInterFrm();
		this.jDesktopPane1.add(itemmngfrm);
	}

	private void formWindowClosing(java.awt.event.WindowEvent evt) {
		int option = JOptionPane.showConfirmDialog(null, "您确定要退出系统吗?");
		if (option == JOptionPane.YES_OPTION) {
			System.exit(0);
		}
		this
				.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
	}

	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
		itemAddInternalFrame itemaddfrm = new itemAddInternalFrame();
		this.jDesktopPane1.add(itemaddfrm);
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JButton jButton4;
	private javax.swing.JDesktopPane jDesktopPane1;
	private javax.swing.JPanel jPanel1;
	// End of variables declaration//GEN-END:variables

}

6、项目添加:ItemAddInternalFrame.java:

/*
 * itemAddInternalFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.fa.view;

import javax.swing.JOptionPane;

import com.fa.dao.impl.ItemDaoImpl;
import com.fa.dto.Item;
import com.fa.util.StringUtil;

/**
 *
 * @author  __USER__
 */
public class itemAddInternalFrame extends javax.swing.JInternalFrame {

	/** Creates new form itemAddInternalFrame */
	public itemAddInternalFrame() {
		initComponents();
		setLocation(400, 100);
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		itemaddnametxt = new javax.swing.JTextField();
		itemaddtypetxt = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();

		setClosable(true);
		setIconifiable(true);
		setTitle("\u6536\u652f\u9879\u76ee\u6dfb\u52a0");
		setVisible(true);

		jLabel1.setFont(new java.awt.Font("幼圆", 0, 14));
		jLabel1.setText("\u9879\u76ee\u540d\u79f0");

		jLabel2.setFont(new java.awt.Font("幼圆", 0, 14));
		jLabel2.setText("\u9879\u76ee\u7c7b\u578b");

		itemaddnametxt.setFont(new java.awt.Font("宋体", 0, 14));

		itemaddtypetxt.setFont(new java.awt.Font("宋体", 0, 14));

		jButton1.setFont(new java.awt.Font("幼圆", 1, 14));
		//jButton1
				//.setIcon(new javax.swing.ImageIcon(
						//"E:\\Workspaces\\MyEclipse 8.5\\FamilyAccount\\images\\add.png")); // NOI18N
		jButton1.setText("\u6dfb\u52a0");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jButton2.setFont(new java.awt.Font("幼圆", 1, 14));
		//jButton2
				//.setIcon(new javax.swing.ImageIcon(
						//"E:\\Workspaces\\MyEclipse 8.5\\FamilyAccount\\images\\reset.png")); // NOI18N
		jButton2.setText("\u91cd\u7f6e");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(75, 75, 75)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jButton1)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jLabel1)
																						.addComponent(
																								jLabel2))
																		.addGap(
																				29,
																				29,
																				29)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								itemaddtypetxt,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								70,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addGroup(
																								layout
																										.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.TRAILING)
																										.addComponent(
																												jButton2)
																										.addComponent(
																												itemaddnametxt,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												152,
																												javax.swing.GroupLayout.PREFERRED_SIZE)))))
										.addContainerGap(82, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(42, 42, 42)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																itemaddnametxt,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(45, 45, 45)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel2)
														.addComponent(
																itemaddtypetxt,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												73, Short.MAX_VALUE)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jButton1)
														.addComponent(jButton2))
										.addGap(53, 53, 53)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		String itemname = itemaddnametxt.getText();
		String itemtype = itemaddtypetxt.getText();

		if (StringUtil.isEmpty(itemname)) {
			JOptionPane.showMessageDialog(null, "项目名称不能为空!");
			return;
		}
		if (StringUtil.isEmpty(itemtype)) {
			JOptionPane.showMessageDialog(null, "项目类型不能为空!");
			return;
		}

		ItemDaoImpl idi = new ItemDaoImpl();
		Item item = new Item(itemname, itemtype);
		int i;
		try {
			if (idi.ifExist(item)) {
				JOptionPane.showMessageDialog(null, "该项目已存在!");
			} else {
				i = idi.addItem(item);
				if (i != 0) {
					JOptionPane.showMessageDialog(null, "添加成功!");
					this.itemaddnametxt.setText("");
					this.itemaddtypetxt.setText("");
				} else {
					JOptionPane.showMessageDialog(null, "添加失败!");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "添加失败!");
		}
	}

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		this.itemaddnametxt.setText("");
		this.itemaddtypetxt.setText("");
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JTextField itemaddnametxt;
	private javax.swing.JTextField itemaddtypetxt;
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	// End of variables declaration//GEN-END:variables

}

写在最后

完整源码,可加博主V交流:Code2Life2

  • 14
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

anmu4200

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值