⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 goods.java

📁 课程设计:ATM Project源码
💻 JAVA
字号:
/* Copyright 2002 MonkeyStar SoftWare, Inc. All rights reserved.
 * The Start of the ATM Project for Product Company
 * @author Wujun, Student Number:09800134
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.BorderFactory.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.io.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.table.*;
public class Goods extends JPanel implements ListSelectionListener{
	//JDBCAdapter adoGoods = new JDBCAdapter("company");
	Vector m_Product;
	Socket Winsock;
	//Public String SelectedID;
	ListSelectionModel selectMode =null;
	String[] s = {"南瓜","柚子","葡萄","弥猴桃","流连","水蜜桃","仙人果","凤梨","荔枝","草莓","蕃茄","西瓜"};
	ImageIcon[] icons = new ImageIcon[12];
	JLabel label1 = new JLabel("进货商品种类:");
	JLabel label2 = new JLabel("进货数量:");
	JLabel label3 = new JLabel("设定商品单价:");
	JLabel label4 = new JLabel("(元)");
	JLabel label5 = new JLabel("供货商:");
	Font TableFont = new Font("宋体",Font.BOLD,13);
	JTextField txtNum = new JTextField(10);
	JTextField txtProvider = new JTextField(20);
	JComboBox Combo1;
	JComboBox ComboPrice= new JComboBox();
	public JButton btStorein = new JButton();
	public JButton btSetprice = new JButton();
	Icon imgbtStorein = new ImageIcon(".\\img\\btstorein.jpg");
    Icon imgbtStoreinOn = new ImageIcon(".\\img\\btstorein_over.jpg");
    Icon imgbtStoreinDown= new ImageIcon(".\\img\\btstorein_down.jpg");
    Icon imgbtSetprice = new ImageIcon(".\\img\\setprice.jpg");
    Icon imgbtSetpriceOn = new ImageIcon(".\\img\\setprice_over.jpg");
    Icon imgbtSetpriceDown= new ImageIcon(".\\img\\setprice_down.jpg");
	GoodsModel tm=new GoodsModel();
    JTable tableGoods=new JTable(tm);
	JScrollPane tbScroll = new JScrollPane(tableGoods);
	Component f;
	private ActionListener listener=new ActionResponse();
	public Goods(Component ComponentF)
	{
		this.setLayout(null);
		f=ComponentF;
		selectMode = tableGoods.getSelectionModel();
        selectMode.addListSelectionListener(this);
		btStorein.setRolloverEnabled(true);
     	btStorein.setIcon(imgbtStorein);
        btStorein.setRolloverIcon(imgbtStoreinOn);
        btStorein.setPressedIcon(imgbtStoreinDown);
        btStorein.setBorder(null);
        btStorein.addActionListener(listener);
        
        btSetprice.setRolloverEnabled(true);
     	btSetprice.setIcon(imgbtSetprice);
        btSetprice.setRolloverIcon(imgbtSetpriceOn);
        btSetprice.setPressedIcon(imgbtSetpriceDown);
        btSetprice.setBorder(null);
        btSetprice.addActionListener(listener);
		tbScroll.setBounds(0,0,510,220);
		btStorein.setBounds(380,230,110,30);
		btSetprice.setBounds(380,270,110,30);
		getStoreDB();
		setCombo();
		
		//TableColumn colorColumn = tableGoods.getColumn("库存量");
		//colorColumn.setCellEditor(null);
		//DefaultTableCellRenderer colorColumnRenderer = new DefaultTableCellRenderer();
        //colorColumnRenderer.setBackground(Color.pink);
        //colorColumnRenderer.setToolTipText("Click for combo box");
        //colorColumn.setCellRenderer(colorColumnRenderer);
        tableGoods.getColumnModel().getColumn(0).setPreferredWidth(3);
        tableGoods.getColumnModel().getColumn(1).setPreferredWidth(15);
        tableGoods.getColumnModel().getColumn(2).setPreferredWidth(5);
        tableGoods.getColumnModel().getColumn(3).setPreferredWidth(5);
        tableGoods.getColumnModel().getColumn(4).setPreferredWidth(50);
        tableGoods.getColumnModel().getColumn(5).setPreferredWidth(180);
        tableGoods.getColumnModel().getColumn(6).setPreferredWidth(30);
        tableGoods.setRowHeight(30);
        //tableGoods.setForeground(Color.blue);
        this.add(btStorein);
        this.add(btSetprice);
		this.add(tbScroll);
	}
	class ActionResponse implements ActionListener
	{
    	
    	public void actionPerformed (ActionEvent e) 
   		{  
           String ProName=((ItemObj)Combo1.getSelectedItem()).name;
           if (e.getSource()==btStorein)
           {
      		 	
      		 	String title = "提示:";
	       	  	int type = JOptionPane.INFORMATION_MESSAGE;
        	  	String[] options={"确定","取消"};
        	  	int optionType=JOptionPane.YES_NO_OPTION;
        	  	int messageType=JOptionPane.QUESTION_MESSAGE; 
       		  	String message = "确定增加 "+((ItemObj)Combo1.getSelectedItem()).name+" 的库存量吗?";       
        	  	int result = JOptionPane.showOptionDialog(f,message, title, optionType,messageType,null,options,options[1]);
        	  	if (result == JOptionPane.YES_OPTION) 
        	  	{
	        	  	try{
	        	  	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		    		Connection c = DriverManager.getConnection("jdbc:odbc:company","","");
	    			Statement s = c.createStatement();
	    			s.executeUpdate("UPDATE Store SET Amount=Amount+"+txtNum.getText().trim()+" WHERE Product='"+((ItemObj)Combo1.getSelectedItem()).name+"'");
                	s.close();
                	JOptionPane.showMessageDialog(f, "商品库存添加完毕!", "提示:", JOptionPane.INFORMATION_MESSAGE);
                	txtNum.setText("");
                	getStoreDB();}
                	catch(SQLException Exp)
    		 		{System.out.println(Exp.toString()); }
     	   			 catch(ClassNotFoundException Exp)
     				{JOptionPane.showMessageDialog(f, Exp.toString(), "错误:", JOptionPane.INFORMATION_MESSAGE);}
     				catch(Exception Exp)
     				{}	 	
                }  	
			}
			if (e.getSource()==btSetprice)
            {
           	 if(tableGoods.getSelectedRow()>=0)
           	 {
           	    String title = "提示:";
	       	  	int type = JOptionPane.INFORMATION_MESSAGE;
        	  	String[] options={"确定","取消"};
        	  	int optionType=JOptionPane.YES_NO_OPTION;
        	  	int messageType=JOptionPane.QUESTION_MESSAGE; 
       		  	String message = "确定更新 "+((Store)m_Product.elementAt(tableGoods.getSelectedRow())).ProName+" 的库存记录吗?";       
        	  	int result = JOptionPane.showOptionDialog(f,message, title, optionType,messageType,null,options,options[1]);
        	  	if (result == JOptionPane.YES_OPTION) 
        	  	{
                    try{
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		    		Connection c = DriverManager.getConnection("jdbc:odbc:company","","");
	    			Statement s = c.createStatement();
	    			s.executeUpdate("UPDATE Store SET Price="+ComboPrice.getSelectedItem().toString()+" WHERE ProductID='"+String.valueOf(tableGoods.getSelectedRow()+1)+"'");
	    			s.executeUpdate("UPDATE Store SET Provider='"+txtProvider.getText().trim()+"' WHERE ProductID='"+String.valueOf(tableGoods.getSelectedRow()+1)+"'");
                	s.close();
                	getStoreDB();}
                	catch(SQLException Exp)
    		 		{System.out.println(Exp.toString()); }
     	   			 catch(ClassNotFoundException Exp)
     				{JOptionPane.showMessageDialog(f, Exp.toString(), "错误:", JOptionPane.INFORMATION_MESSAGE);}
     				catch(Exception Exp)
     				{}	 	
                    message= new String("库存价格修改成功!\n要马上把价格变化通知到自动售货机上吗?");
        	  	    result = JOptionPane.showOptionDialog(f,message, title, optionType,messageType,null,options,options[1]);
        	  	    if (result == JOptionPane.YES_OPTION)
        	  	    {
        	  	    	message=new String("请输入自动售货机网络地址:");
        	  	    	String answer=JOptionPane.showInputDialog(f,message,"联机提示",messageType);
        	  	    		try{
        	  					Winsock = new Socket((new ReadConfig()).getAutoSrvIP(),6000);
        	  					
        	  					SendThread st = new SendThread(Winsock,ProName,Double.parseDouble(ComboPrice.getSelectedItem().toString()),f);
        	  				    st.start();
        	  					}
        	  					catch(IOException exp)
        	  					{System.out.println(exp.toString());}
        	  	    	        catch(Exception exp)
        	  	    	        {System.out.println(exp.toString());}
        	  	    }
                }
              }
              else
              JOptionPane.showMessageDialog(f, "您没有选择具体记录!", "提示:", JOptionPane.INFORMATION_MESSAGE);  
             }
             tableGoods.revalidate();
           }
           
          }  	
			
           
      
	public void valueChanged(ListSelectionEvent e1)
    {
        String tempString = "";
        int[] rows = tableGoods.getSelectedRows();
        int[] columns = tableGoods.getSelectedColumns();

        for (int i=0; i<rows.length; i++) {        
            for (int j=0; j<columns.length; j++)
                tempString = tempString+" "+(String)tableGoods.getValueAt(rows[i], 5);      
        }
        txtProvider.setText(tempString);
        Combo1.setSelectedIndex(tableGoods.getSelectedRow());
    }      	
	public void setCombo()
	{
		ItemObj[] obj = new ItemObj[12];
		for(int i=0; i < 12; i++)
        {
            icons[i] = new ImageIcon(".\\img\\small\\list"+String.valueOf(i+1)+".jpg");
            obj[i] = new ItemObj(s[i],icons[i]);
        }
        Combo1= new JComboBox(obj);
        Combo1.setRenderer(new ACellRenderer());
        Combo1.setMaximumRowCount(6);
		Combo1.setBounds(90,232,110,25);
		label1.setBounds(0,230,110,30);
		label2.setBounds(220,230,100,30);
		label3.setBounds(0,260,110,30);
		label4.setBounds(145,260,110,30);
		label5.setBounds(180,260,110,30);
		txtProvider.setBounds(230,265,135,20);
		txtNum.setBounds(285,235,80,20);
		ComboPrice.setBounds(90,265,50,20);
		for(int i=1;i<=10;i++)
		{
			ComboPrice.addItem(String.valueOf(i));
		}
		this.add(label1);
		this.add(label2);
		this.add(label3);
		this.add(label4);
		this.add(label5);
		this.add(txtProvider);
		this.add(txtNum);
		this.add(Combo1);
		this.add(ComboPrice);
	}
	
	public void getStoreDB()
    {
  try{
     	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	    Connection c = DriverManager.getConnection("jdbc:odbc:company","","");
	    Statement s = c.createStatement();
	    m_Product=new Vector();
	    for (int i=0;i<12;i++)
        {	ResultSet r = s.executeQuery("SELECT * FROM Store WHERE ProductID='"+String.valueOf(i+1)+"'");
            while(r.next()) 
	        {
	    	   Store store = new Store();
    	 	   store.ProductID=r.getString("ProductID");
   		 	   store.ProName=r.getString("Product");
   		 	   store.Price=r.getDouble("Price");
   			   store.Amount=r.getInt("Amount");
   			   store.StartDate=r.getString("StartDate");
   			   store.LastTime=r.getString("LastTime");
         	   store.Provider=r.getString("Provider");
    	  	   m_Product.addElement(store);
	    	}
	    }
	    s.close();
	    tm.setItem(m_Product);
	 }
	 catch(SQLException Exp)
     {System.out.println(Exp.toString()); }
     catch(ClassNotFoundException Exp)
     {JOptionPane.showMessageDialog(f, Exp.toString(), "错误:", JOptionPane.INFORMATION_MESSAGE);}
     catch(Exception Exp)
     {}
  }  
    
}

class ItemObj
{
      String name;
      ImageIcon icon;
    
      public ItemObj(String name, ImageIcon icon){
        this.name = name;
        this.icon = icon;
      }
}    
class ACellRenderer extends JLabel implements ListCellRenderer
{
    ACellRenderer()
    {
        setOpaque(true);
    }
    
    public Component getListCellRendererComponent(JList list,
                                                  Object value,
                                                  int index,
                                                  boolean isSelected,
                                                  boolean cellHasFocus)
    {
        if (value != null)
        {
            setText(((ItemObj)value).name);
            setIcon(((ItemObj)value).icon);
        }

        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        }
        else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }

        return this;
    }    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -