java实现商品实时录入

//代表各的主页面

package com.gui;
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.io.*;
import java.nio.channels.ClosedSelectorException;
import java.sql.*;
import javax.swing.JFileChooser;
import javax.swing.plaf.ScrollBarUI;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;

import com.DateSystem.Good_data;
import com.DateSystem.Goods;
import com.DateSystem.Userdate;
import java.util.List;

public class Main extends JFrame implements ActionListener,AdjustmentListener{

	/**
	 * @param args
	 */
	//主窗体组件
	MenuBar menuBar;
    Menu menu_xitong,menu_guanli,menu_gongju,menu_Help;
	MenuItem item_up_password, item_reLanding, item_exit;
	MenuItem item_input, item_find, item_delete;
	MenuItem item_jisuanqi, item_jishiben, item_beijing;
	MenuItem item_LookHelp, item_About;
	
	//录入界面要用的组件
	JPanel jp_btn,jp_main,jp_table,jp_text;
	JLabel lbl_id, lbl_name, lbl_address, lbl_in_price, lbl_out_price, lbl_kucun, lbl_jinhuo, lbl_chuhuo;
	TextField text_id,text_name,text_address,text_in_price,text_out_price,text_kucun,text_jinhuo,text_chuhuo;
	JButton submit,reset,delete;
	JScrollPane jsp;
	JTable table;
	DefaultTableModel model;
	
	
	CardLayout c=new CardLayout();
	JPanel card;
	//查找页面要用的组件
	
	JPanel jp1,jp2,jp3;
	JPanel jp2_main;
	JButton find_id,find_name,find_delete,find_all;
	JTextField text_find;
	JScrollPane jsp_find;
	JTable table_find;
	DefaultTableModel model_find;
	
	//背景更改要用的组件
	
	int r =255,g=255,b=255;
	TextField tfr,tfg,tfb;
	Scrollbar sbr,sbg,sbb;
	Panel display;
	JPanel jp3_main;
	
	public Main(){
		super("主窗口");
		menuBar=new MenuBar();
		
		menu_gongju=new Menu("工具");
		menu_guanli=new Menu("管理");
		menu_Help=new  Menu("帮助");
		menu_xitong=new Menu("系统");
		
		item_About=new MenuItem("关于");
		item_beijing=new MenuItem("背景设置");
		item_LookHelp=new MenuItem("查看帮助");
		item_up_password=new MenuItem("修改密码");
		item_reLanding=new MenuItem("重新登录");
		item_exit=new MenuItem("退出系统");
		item_jishiben=new MenuItem("记事本");
		item_jisuanqi=new MenuItem("计算器");
		item_find=new  MenuItem("查找");
		item_input=new MenuItem("录入");		
		item_delete=new MenuItem("删除");
		
		//注册监听
		item_About.addActionListener(this);
		item_beijing.addActionListener(this);
		item_LookHelp.addActionListener(this);
		item_up_password.addActionListener(this);
		item_reLanding.addActionListener(this);
		item_exit.addActionListener(this);
		item_jishiben.addActionListener(this);
		item_jisuanqi.addActionListener(this);
		item_find.addActionListener(this);
		item_input.addActionListener(this);
		item_delete.addActionListener(this);
		
		
		menu_xitong.add(item_up_password);
		menu_xitong.add(item_reLanding);
		menu_xitong.add(item_exit);
		
		menu_guanli.add(item_input);
		menu_guanli.add(item_find);
		//menu_guanli.add(item_delete);
		
		menu_gongju.add(item_jisuanqi);
		menu_gongju.add(item_jishiben);
		menu_gongju.add(item_beijing);
		
		
		menu_Help.add(item_About);
		menu_Help.add(item_LookHelp);
		
		menuBar.add(menu_xitong);
		menuBar.add(menu_guanli);
		menuBar.add(menu_gongju);
		menuBar.add(menu_Help);
		
		
		
		setMenuBar(menuBar);
				
		setResizable(false);
		
		
		initComponents();//调用方法初始化界面
		Find();
		change_color();
		//查找功能的布局以及组建
		
		//this.setLayout(new CardLayout());
		card=new JPanel();
		card.setLayout(c);		
		card.add("1", jp_main);
		card.add("2", jp2_main);
		card.add("3", jp3_main);
		//this.getContentPane().add("Center", jp_main);
		this.add(card);
		this.setSize(1000, 500);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	//录入界面初始化
	private void initComponents() {//构造面板
		System.out.println("pass");
		lbl_id=new JLabel("商品编号",JLabel.CENTER);
		lbl_name=new JLabel("商品名称",JLabel.CENTER);
		lbl_address=new JLabel("商品产地",JLabel.CENTER);
		lbl_in_price=new JLabel("进价",JLabel.CENTER);
		lbl_out_price=new JLabel("售价",JLabel.CENTER);
		lbl_jinhuo=new JLabel("进货量",JLabel.CENTER);
		lbl_chuhuo=new JLabel("销售量",JLabel.CENTER);
		lbl_kucun=new JLabel("库存量",JLabel.CENTER);
		
		text_id=new TextField();
		text_name = new TextField();
		text_address=new  TextField();
		text_in_price=new TextField();
		text_out_price=new TextField();
		text_jinhuo=new TextField();
		text_chuhuo=new TextField();
		text_kucun=new  TextField();
		
		
		jp_table=new JPanel(new BorderLayout());		
		jp_text =new JPanel(new GridLayout(2,8));
		
		
		jp_text.add(lbl_id);
		jp_text.add(lbl_name);
		jp_text.add(lbl_address);
		jp_text.add(lbl_in_price);
		jp_text.add(lbl_out_price);
		jp_text.add(lbl_jinhuo);
		jp_text.add(lbl_chuhuo);
		jp_text.add(lbl_kucun);
		
		
		jp_text.add(text_id);
		jp_text.add(text_name);
		jp_text.add(text_address);
		jp_text.add(text_in_price);
		jp_text.add(text_out_price);
		jp_text.add(text_jinhuo);
		jp_text.add(text_chuhuo);
		jp_text.add(text_kucun);
		
		model = new DefaultTableModel(new Object[][] { 			
			   
			  }, new String[] { "商品编号", "商品名称", "商品产地" ,"进价","售价","库存量","进货量","出货量" });
		
		table =new JTable(model);
		jsp=new JScrollPane(table);
		
		submit=new JButton("录入商品");
		reset=new JButton("重置信息");
		delete=new JButton("删除所选商品");
	
		submit.addActionListener(this);
		reset.addActionListener(this);
		delete.addActionListener(this);
		jp_btn=new JPanel(new GridLayout(1,3));
		jp_btn.add(submit);
		jp_btn.add(delete);
		jp_btn.add(reset);
		
		jp_table.add("Center",jp_text);
		jp_table.add("South",jp_btn);
		
		jp_main=new JPanel(new BorderLayout(10,10));
		jp_main.add("Center",jsp);
		jp_main.add("North",jp_table);
		System.out.println("pass");
	}
	
	
	//查找面板初始化面板
	private void Find() {
		jp1=new JPanel(new GridLayout(1,1));
		jp2=new JPanel(new GridLayout(1,4));
		jp3=new JPanel(new BorderLayout());
		
		
		
		
		find_id =new JButton("根据商品编号查询");
		find_name=new JButton("根据商品名称查询");
		find_all=new JButton("查询所有信息");
		find_delete=new JButton("删除所选信息");
		text_find=new JTextField();
		jsp_find=new JScrollPane();
		table_find=new JTable();
		model_find=new DefaultTableModel();
		
		model_find = new DefaultTableModel(new Object[][] { 			
				   
		  }, new String[] { "商品编号", "商品名称", "商品产地" ,"进价","售价","库存量","进货量","出货量" });
	
		table_find =new JTable(model_find);
		jsp_find=new JScrollPane(table_find);
		
		find_id.addActionListener(this);
		find_name.addActionListener(this);
		find_all.addActionListener(this);
		find_delete.addActionListener(this);
				
		jp1.add(text_find);
		jp2.add(find_id);
		jp2.add(find_name);
		jp2.add(find_all);
		jp2.add(find_delete);
		
		jp3.add("North",jp1);
		jp3.add("Center",jp2);
		
		
		jp2_main=new JPanel(new BorderLayout(10,10));
		jp2_main.add("Center",jsp_find);
		jp2_main.add("North",jp3);
		
	}
	
	//背景更改
	private void change_color() {
		
		jp3_main=new  JPanel();
		display =new  Panel();
		display.setBackground(Color.WHITE);
		Panel pcolor =new Panel();
		pcolor.setLayout(new BorderLayout());
		Panel pw=new Panel();
		pw.setLayout(new GridLayout(3,2,0,0));

		jp3_main.setLayout(new  BorderLayout());
		tfr= new TextField("255");
		tfg=new TextField("255");
		tfb=new TextField("255");
		
		tfr.setEditable(false);
		tfg.setEditable(false);
		tfb.setEditable(false);
		
		sbr=new Scrollbar(Scrollbar.HORIZONTAL,255,0,0,255);
		sbg=new Scrollbar(Scrollbar.HORIZONTAL,255,0,0,255);
		sbb=new Scrollbar(Scrollbar.HORIZONTAL,255,0,0,255);
		
		sbr.setName("SBR");
		sbg.setName("SBG");
		sbb.setName("SBB");
		
		sbr.setBackground(Color.red);
		sbg.setBackground(Color.green);
		sbb.setBackground(Color.blue);
		
		sbr.addAdjustmentListener(this);
		sbg.addAdjustmentListener(this);
		sbb.addAdjustmentListener(this);
		
		pw.add(new Label("红色"));
		pw.add(tfr);

		pw.add(new Label("绿色"));
		pw.add(tfg);
		

		pw.add(new Label("蓝色"));
		pw.add(tfb);
		
		Panel pc= new Panel();
		pc.setLayout(new GridLayout(3, 1, 0, 0));
		pc.add(sbr);
		pc.add(sbg);
		pc.add(sbb);
		
		pcolor.add(pw,BorderLayout.WEST);
		pcolor.add(pc,BorderLayout.CENTER);
		
		
		jp3_main.add(display,BorderLayout.CENTER);
		jp3_main.add(pcolor,BorderLayout.SOUTH);
		
	}
	
	//查看帮助文档,调用本地的程序,查看帮助文档
	public  void useCMDCommand() throws IOException{          
        Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "..\BaoDing_Shoop\保定商场商品管理系统项目帮助.doc"}); 
    } 
	
	//主函数
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		java.awt.EventQueue.invokeLater(new Runnable() {
			   public void run() {
			    new Main().setVisible(true);
			   }
			});
		
	}

	//监听方法
	public void actionPerformed(ActionEvent e) {
		
		//得到事件源
		String btString=e.getActionCommand();
		//录入功能		
		if (btString.equals("录入商品")) {//追加到最后一行
			
			
			if(!text_id.getText().equals("") && !text_name.getText().equals("") && !text_address.getText().equals("") && !text_in_price.getText().equals("") && !text_out_price.getText().equals("") && !text_jinhuo.getText().equals("") && !text_chuhuo.getText().equals("") && !text_kucun.getText().equals("")) {
				
				//获取文本框信息
				int  idString=Integer.parseInt(text_id.getText());
				
				String ids=text_id.getText();
				
				String nameString=text_name.getText();
				String addresString=text_address.getText();
				double in_priceString=Double.parseDouble(text_in_price.getText());
				double out_priceString=Double.parseDouble(text_out_price.getText());
				int  jinhuoString=Integer.parseInt(text_jinhuo.getText());
				int  chuhuoString=Integer.parseInt(text_chuhuo.getText());
				int  kucun=Integer.parseInt(text_kucun.getText());
				if (in_priceString<=0) {
					JOptionPane.showMessageDialog(this, "亲~,进价要大于零哦!");
				}else {
					
					if (in_priceString>=out_priceString) {
						JOptionPane.showMessageDialog(this, "亲~,要有盈利的,销售价格要大于进价!");
					}else {
						System.out.print("录入商品");
						if (idString!=0 && !nameString.equals("") && !addresString.equals("") && in_priceString!=0 && out_priceString!=0 && jinhuoString!=0 && chuhuoString!=0 && kucun!=0) {
							
							//对数据库的操作
							
							 String sql="insert into result values('"+
									 ids +"','" +
									 nameString+"','"+
									 addresString+"',"+
									 in_priceString+","+
									 out_priceString+","+
									 jinhuoString+","+
									 chuhuoString+","+
									 kucun+
									 ")";
							Userdate userdate=new Userdate();
							boolean flag=userdate.add_goods(sql);
							if (flag) {
								//对表格的操作
								model.insertRow(model.getRowCount(), new Object[] 
										 { ids, nameString,addresString,in_priceString,out_priceString,jinhuoString,chuhuoString,kucun }			 
								 
										 );
								
								
							}else {
								 JOptionPane.showMessageDialog(this, "商品编号重复!");
							}
							 
						}else {
							JOptionPane.showMessageDialog(this, "请输入商品详细信息!");
						}
					}
				}
				
			}else {
				
				JOptionPane.showMessageDialog(this, "请输入商品的详细信息!");
			}
		}
		//删除功能
		else if(btString.equals("删除所选商品")){
			//获取要删除的行,没有选择是-1
			int row=table.getSelectedRow();
			System.out.print("删除商品操作");
			System.out.print(row);
			if (row==-1) {
				JOptionPane.showMessageDialog(this,"请选择要删除商品的商品名!");				
			}else {
				//System.out.print(row);
				
				//数据库删除商品操作
				//int  shangpin_id= Integer.parseInt((String) model.getValueAt(row, 0)) ;
				String shangpin_id=(String) model.getValueAt(row, 0) ;
				System.out.print(row);
				
				String sql="delete from result where id='"+shangpin_id+"'";
				Userdate userdate=new Userdate();
				boolean flag=userdate.add_goods(sql);
				if (flag) {
					model.removeRow(row);
				}
				
			}	
			
		}
		//重置功能
		else if(btString.equals("重置信息")){
			
			text_id.setText("");
			text_name.setText("");
			text_address.setText("");
			text_in_price.setText("");
			text_out_price.setText("");
			text_jinhuo.setText("");
			text_chuhuo.setText("");
			text_kucun.setText("");
		}else if(btString.equals("修改密码")){
			UpData data=new UpData();
			data.setVisible(true);
		}else if(btString.equals("计算器")){
			Calculator calculator = new Calculator();		
			calculator.setSize(250,350);
			calculator.setLocationRelativeTo(null);
			calculator.setVisible(true);
			calculator.setResizable(false);			
		}else if(btString.equals("退出系统")){
			System.exit(0);
		}else if (btString.equals("记事本")) {
			NotePad notePad=new NotePad();
			notePad.setVisible(true);
		}else if(btString.equals("查看帮助")){
			
			
			//System.out.println("help_pss");
			try {
				useCMDCommand();
				
				System.out.println("help_pss");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
		}
		else if(btString.equals("关于")){
			About about =new About();
			about.setVisible(true);
		}else if(btString.equals("重新登录")){
			System.out.print("重新登录");
			Landing landing = new Landing();
			landing.setVisible(true);
			this.dispose();			
		}else if(btString.equals("查找")){
			c.show(card,"2");
			
		}else if(btString.equals("录入")){
			c.show(card,"1");			
		}else if(btString.equals("删除")){
			c.show(card,"2");
			JOptionPane.showMessageDialog(this, "请先查找所要删除的商品信息!");
		}else if(btString.equals("背景设置")){
			c.show(card,"3");			
		}
		//查找功能
		else if(btString.equals("根据商品编号查询")){
			
			if (text_find.getText().equals("")) {
				JOptionPane.showMessageDialog(this, "查询信息为空!");
			}else {
				
				//int  id =Integer.parseInt(text_find.getText().trim());
				String id =text_find.getText().trim();
				
				Good_data data=new Good_data();
				
				Goods goods =data.findStudentById(id);
				
				
				
				//String id_s=Integer.toString(id);
				//获取文本框信息
				//int  idString=goods.getId();
				//String ids=Integer.toString(goods.getId());
				String  idString=goods.getId();
				
				
				String nameString=goods.getName();
				String addresString=goods.getAddress();
				double  in_priceString=goods.getIn_price();
				double  out_priceString=goods.getOut_price();
				int  jinhuoString=goods.getJinhuo();
				int  chuhuoString=goods.getChuhuo();
				int  kucun=goods.getKucun();
			
				
				//判断用户所输入的商品编号数据库中是否有
				if (idString==null) {
					JOptionPane.showMessageDialog(this,"对不起,小编没有从数据库中找到商品编号为:"+id+"的信息!");	
				}else {
					//对表格的操作
					model_find.insertRow(model_find.getRowCount(), new Object[] 
							 { idString, nameString,addresString,in_priceString,out_priceString,jinhuoString,chuhuoString,kucun }			 
					 
							 );				
				}				
			}			
		}
		else if(btString.equals("根据商品名称查询")){
			String name =text_find.getText().trim();
			
			Good_data data=new Good_data();
			String sql="select  * from result where nam like '%"+name+"%'";
			
			 List<Goods> datalist=data.findGoods(sql);
			
			//获取文本框信息
			 
			 if (name.equals("")) {
				JOptionPane.showMessageDialog(this, "查询信息为空!");
			}else {
				
				if (datalist.size()<=0) {
					JOptionPane.showMessageDialog(this,"对不起,小编没有从数据库中找到商品名为:"+name+"的信息!");					
				}
				 
				for(Goods d:datalist){//遍历数据,将查询所得到的数据逐一赋值到表中
					
				//int  idString=d.getId();
				String idString=d.getId();
				
				String nameString=d.getName();
				String addresString=d.getAddress();
				double in_priceString=d.getIn_price();
				double  out_priceString=d.getOut_price();
				int jinhuoString=d.getJinhuo();
				int  chuhuoString=d.getChuhuo();
				int  kucun=d.getKucun();
				
				//对表格的操作
				model_find.insertRow(model_find.getRowCount(), new Object[] 
						 { idString, nameString,addresString,in_priceString,out_priceString,jinhuoString,chuhuoString,kucun }			 
				 
						 );
				 }				
			}			 
		}	
		else if(btString.equals("删除所选信息")){
			//获取要删除的行,没有选择是-1
			int row=table_find.getSelectedRow();
			System.out.print("删除商品操作");
			System.out.print(row);
			if (row==-1) {
				JOptionPane.showMessageDialog(this,"请选择要删除商品!");				
			}else {
				//System.out.print(row);
				
				//数据库删除商品操作
				//int shangpin_id=(Integer) model_find.getValueAt(row, 0);
				String shangpin_id=(String) model_find.getValueAt(row, 0);
				System.out.print(row);
				
				String sql="delete from result where id='"+shangpin_id+"'";
				Userdate userdate=new Userdate();
				boolean flag=userdate.add_goods(sql);
				if (flag) {
					model_find.removeRow(row);
				}
			}
		}else if(btString.equals("查询所有信息")){
			String name =text_find.getText().trim();
			
			Good_data data=new Good_data();
			String sql="select  * from result";
			
			List<Goods> datalist=data.findGoods(sql);
						
			for(Goods d:datalist){//遍历数据,将查询所得到的数据逐一赋值到表中
				
			//int  idString=d.getId();
			String idString=d.getId();
			
			String nameString=d.getName();
			String addresString=d.getAddress();
			double in_priceString=d.getIn_price();
			double  out_priceString=d.getOut_price();
			int jinhuoString=d.getJinhuo();
			int  chuhuoString=d.getChuhuo();
			int  kucun=d.getKucun();
			
			//对表格的操作
			model_find.insertRow(model_find.getRowCount(), new Object[] 
					 { idString, nameString,addresString,in_priceString,out_priceString,jinhuoString,chuhuoString,kucun }			 
			 
					 );
			 }				
					
		}
	}
	@Override
	public void adjustmentValueChanged(AdjustmentEvent e) {
		// TODO Auto-generated method stub
		
		
		String sb_name =((Scrollbar)e.getSource()).getName();
		int value =e.getValue();
		if (sb_name.equals("SBR")) {
			r=value;
			tfr.setText(Integer.toString(r));
		}else if (sb_name.equals("SBG")) {
			g=value;
			tfg.setText(Integer.toString(g));
		}else if (sb_name.equals("SBB")) {
			b=value;
			tfb.setText(Integer.toString(b));
		}
		refresh();
	}
	
	public void refresh() {
		Color c=new  Color(r,g,b);
		display.setBackground(c);
		table.setBackground(c);
		table_find.setBackground(c);		
	}
	
	
}


原文地址:https://www.cnblogs.com/jiangu66/p/3188522.html