www.pudn.com > 使用 Java 读取条形码代码文件.rar > InventoryView.java


package com.jmart; 
 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 
import com.jmart.data.*; 
 
public class InventoryView extends JFrame implements ActionListener { 
	private JButton ivjbtnAdd = null; 
	private JButton ivjbtnDelete = null; 
	private JPanel ivjJFrameContentPane = null; 
	private JLabel ivjlblName = null; 
	private JLabel ivjlblPrice = null; 
	private JLabel ivjstcLblPrice = null; 
	private JLabel ivjstcLblQty = null; 
	private JLabel ivjstcName = null; 
	private JTextField ivjtxtQty = null; 
	private InventoryController controller; 
	private JButton ivjbtnDeleteAll = null; 
	private JPanel ivjJPanel1 = null; 
	private JLabel ivjlblItemNum = null; 
	private JLabel ivjstcLblItemNum = null; 
	private InventoryData data; 
	private JLabel ivjJLabel1 = null; 
	private JTextField ivjtxtName = null; 
	private JTextField ivjtxtPrice = null; 
/** 
 * InventoryScreen constructor comment. 
 */ 
public InventoryView() { 
	super(); 
	initialize(); 
} 
/** 
 * InventoryScreen constructor comment. 
 * @param title java.lang.String 
 */ 
public InventoryView(String title) { 
	super(title); 
} 
 
public void actionPerformed(java.awt.event.ActionEvent e)  
{ 
	getbtnAdd().setEnabled(false); 
	getbtnDelete().setEnabled(false); 
	getbtnDeleteAll().setEnabled(false); 
	if (e.getSource() == getbtnAdd()) 
		controller.processData(InventoryController.ADD, getData()); 
	else if (e.getSource() == getbtnDelete()) 
	{ 
		InventoryData invData = getData(); 
		invData.setQuantity(invData.getQuantity() * -1); 
		controller.processData(InventoryController.DELETE, invData); 
	} 
	else if (e.getSource() == getbtnDeleteAll()) 
		controller.processData(InventoryController.DELETE_ALL, getData()); 
} 
/** 
 * Return the btnAdd property value. 
 * @return javax.swing.JButton 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JButton getbtnAdd() { 
	if (ivjbtnAdd == null) { 
		try { 
			ivjbtnAdd = new javax.swing.JButton(); 
			ivjbtnAdd.setName("btnAdd"); 
			ivjbtnAdd.setMnemonic('a'); 
			ivjbtnAdd.setText("Add to Inventory"); 
			ivjbtnAdd.setBounds(100, 239, 174, 25); 
			ivjbtnAdd.setEnabled(false); 
			// user code begin {1} 
			ivjbtnAdd.addActionListener(this); 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjbtnAdd; 
} 
/** 
 * Return the btnDelete property value. 
 * @return javax.swing.JButton 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JButton getbtnDelete() { 
	if (ivjbtnDelete == null) { 
		try { 
			ivjbtnDelete = new javax.swing.JButton(); 
			ivjbtnDelete.setName("btnDelete"); 
			ivjbtnDelete.setMnemonic('d'); 
			ivjbtnDelete.setText("Delete From Inventory"); 
			ivjbtnDelete.setBounds(100, 274, 174, 25); 
			ivjbtnDelete.setEnabled(false); 
			// user code begin {1} 
			ivjbtnDelete.addActionListener(this); 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjbtnDelete; 
} 
/** 
 * Return the btnDeleteAll property value. 
 * @return javax.swing.JButton 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JButton getbtnDeleteAll() { 
	if (ivjbtnDeleteAll == null) { 
		try { 
			ivjbtnDeleteAll = new javax.swing.JButton(); 
			ivjbtnDeleteAll.setName("btnDeleteAll"); 
			ivjbtnDeleteAll.setMnemonic('l'); 
			ivjbtnDeleteAll.setText("Delete All From Inventory"); 
			ivjbtnDeleteAll.setBounds(100, 308, 174, 25); 
			ivjbtnDeleteAll.setEnabled(false); 
			// user code begin {1} 
			ivjbtnDeleteAll.addActionListener(this); 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjbtnDeleteAll; 
} 
 
private InventoryData getData()  
{ 
	data.setQuantity(Integer.parseInt(gettxtQty().getText())); 
	if ( ! data.isRecordFound()) 
	{ 
		data.setName(gettxtName().getText()); 
		data.setPrice(new java.math.BigDecimal(gettxtPrice().getText())); 
	} 
	getlblItemNum().setText(""); 
	getlblName().setText(""); 
	getlblPrice().setText(""); 
	gettxtName().setText(""); 
	gettxtPrice().setText(""); 
	gettxtQty().setText(""); 
	return data; 
} 
/** 
 * Return the JFrameContentPane property value. 
 * @return javax.swing.JPanel 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JPanel getJFrameContentPane() { 
	if (ivjJFrameContentPane == null) { 
		try { 
			ivjJFrameContentPane = new javax.swing.JPanel(); 
			ivjJFrameContentPane.setName("JFrameContentPane"); 
			ivjJFrameContentPane.setLayout(null); 
			getJFrameContentPane().add(getJPanel1(), getJPanel1().getName()); 
			getJFrameContentPane().add(getbtnAdd(), getbtnAdd().getName()); 
			getJFrameContentPane().add(getbtnDelete(), getbtnDelete().getName()); 
			getJFrameContentPane().add(getbtnDeleteAll(), getbtnDeleteAll().getName()); 
			getJFrameContentPane().add(getJLabel1(), getJLabel1().getName()); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjJFrameContentPane; 
} 
/** 
 * Return the JLabel1 property value. 
 * @return javax.swing.JLabel 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JLabel getJLabel1() { 
	if (ivjJLabel1 == null) { 
		try { 
			ivjJLabel1 = new javax.swing.JLabel(); 
			ivjJLabel1.setName("JLabel1"); 
			ivjJLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jmart.gif"))); 
			ivjJLabel1.setText(""); 
			ivjJLabel1.setBounds(70, 10, 235, 68); 
			ivjJLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjJLabel1; 
} 
/** 
 * Return the JPanel1 property value. 
 * @return javax.swing.JPanel 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JPanel getJPanel1() { 
	if (ivjJPanel1 == null) { 
		try { 
			ivjJPanel1 = new javax.swing.JPanel(); 
			ivjJPanel1.setName("JPanel1"); 
			ivjJPanel1.setLayout(null); 
			ivjJPanel1.setBounds(19, 82, 337, 141); 
			getJPanel1().add(getstcLblItemNum(), getstcLblItemNum().getName()); 
			getJPanel1().add(getlblItemNum(), getlblItemNum().getName()); 
			getJPanel1().add(getstcName(), getstcName().getName()); 
			getJPanel1().add(getlblName(), getlblName().getName()); 
			getJPanel1().add(getstcLblPrice(), getstcLblPrice().getName()); 
			getJPanel1().add(getlblPrice(), getlblPrice().getName()); 
			getJPanel1().add(getstcLblQty(), getstcLblQty().getName()); 
			getJPanel1().add(gettxtName(), gettxtName().getName()); 
			getJPanel1().add(gettxtPrice(), gettxtPrice().getName()); 
			getJPanel1().add(gettxtQty(), gettxtQty().getName()); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjJPanel1; 
} 
/** 
 * Return the lblItemNum property value. 
 * @return javax.swing.JLabel 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JLabel getlblItemNum() { 
	if (ivjlblItemNum == null) { 
		try { 
			ivjlblItemNum = new javax.swing.JLabel(); 
			ivjlblItemNum.setName("lblItemNum"); 
			ivjlblItemNum.setFont(new java.awt.Font("Arial", 1, 18)); 
			ivjlblItemNum.setText(""); 
			ivjlblItemNum.setBounds(143, 17, 126, 14); 
			ivjlblItemNum.setForeground(new java.awt.Color(51,153,204)); 
			ivjlblItemNum.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjlblItemNum; 
} 
/** 
 * Return the lblName property value. 
 * @return javax.swing.JLabel 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JLabel getlblName() { 
	if (ivjlblName == null) { 
		try { 
			ivjlblName = new javax.swing.JLabel(); 
			ivjlblName.setName("lblName"); 
			ivjlblName.setText(""); 
			ivjlblName.setBounds(143, 48, 126, 14); 
			ivjlblName.setForeground(new java.awt.Color(51,153,204)); 
			ivjlblName.setVisible(true); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjlblName; 
} 
/** 
 * Return the lblPrice property value. 
 * @return javax.swing.JLabel 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JLabel getlblPrice() { 
	if (ivjlblPrice == null) { 
		try { 
			ivjlblPrice = new javax.swing.JLabel(); 
			ivjlblPrice.setName("lblPrice"); 
			ivjlblPrice.setText(""); 
			ivjlblPrice.setBounds(143, 79, 126, 14); 
			ivjlblPrice.setForeground(new java.awt.Color(51,153,204)); 
			ivjlblPrice.setVisible(true); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjlblPrice; 
} 
/** 
 * Return the stcLblItemNum property value. 
 * @return javax.swing.JLabel 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JLabel getstcLblItemNum() { 
	if (ivjstcLblItemNum == null) { 
		try { 
			ivjstcLblItemNum = new javax.swing.JLabel(); 
			ivjstcLblItemNum.setName("stcLblItemNum"); 
			ivjstcLblItemNum.setFont(new java.awt.Font("Arial", 1, 18)); 
			ivjstcLblItemNum.setText("Item Num:"); 
			ivjstcLblItemNum.setBounds(22, 17, 106, 14); 
			ivjstcLblItemNum.setForeground(new java.awt.Color(51,153,204)); 
			ivjstcLblItemNum.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjstcLblItemNum; 
} 
/** 
 * Return the stcLblPrice property value. 
 * @return javax.swing.JLabel 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JLabel getstcLblPrice() { 
	if (ivjstcLblPrice == null) { 
		try { 
			ivjstcLblPrice = new javax.swing.JLabel(); 
			ivjstcLblPrice.setName("stcLblPrice"); 
			ivjstcLblPrice.setText("Price: "); 
			ivjstcLblPrice.setBounds(64, 79, 64, 14); 
			ivjstcLblPrice.setForeground(new java.awt.Color(51,153,204)); 
			ivjstcLblPrice.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjstcLblPrice; 
} 
/** 
 * Return the stcLblQty property value. 
 * @return javax.swing.JLabel 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JLabel getstcLblQty() { 
	if (ivjstcLblQty == null) { 
		try { 
			ivjstcLblQty = new javax.swing.JLabel(); 
			ivjstcLblQty.setName("stcLblQty"); 
			ivjstcLblQty.setText("Quantity: "); 
			ivjstcLblQty.setBounds(64, 110, 64, 14); 
			ivjstcLblQty.setForeground(new java.awt.Color(51,153,204)); 
			ivjstcLblQty.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjstcLblQty; 
} 
/** 
 * Return the stcName property value. 
 * @return javax.swing.JLabel 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JLabel getstcName() { 
	if (ivjstcName == null) { 
		try { 
			ivjstcName = new javax.swing.JLabel(); 
			ivjstcName.setName("stcName"); 
			ivjstcName.setText("Name: "); 
			ivjstcName.setBounds(64, 48, 64, 14); 
			ivjstcName.setForeground(new java.awt.Color(51,153,204)); 
			ivjstcName.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjstcName; 
} 
/** 
 * Return the JTextField2 property value. 
 * @return javax.swing.JTextField 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JTextField gettxtName() { 
	if (ivjtxtName == null) { 
		try { 
			ivjtxtName = new javax.swing.JTextField(); 
			ivjtxtName.setName("txtName"); 
			ivjtxtName.setText(""); 
			ivjtxtName.setBounds(143, 45, 126, 20); 
			ivjtxtName.setVisible(true); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjtxtName; 
} 
/** 
 * Return the JTextField1 property value. 
 * @return javax.swing.JTextField 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JTextField gettxtPrice() { 
	if (ivjtxtPrice == null) { 
		try { 
			ivjtxtPrice = new javax.swing.JTextField(); 
			ivjtxtPrice.setName("txtPrice"); 
			ivjtxtPrice.setText(""); 
			ivjtxtPrice.setBounds(143, 76, 59, 20); 
			ivjtxtPrice.setVisible(true); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjtxtPrice; 
} 
/** 
 * Return the txtQty property value. 
 * @return javax.swing.JTextField 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private javax.swing.JTextField gettxtQty() { 
	if (ivjtxtQty == null) { 
		try { 
			ivjtxtQty = new javax.swing.JTextField(); 
			ivjtxtQty.setName("txtQty"); 
			ivjtxtQty.setText("1"); 
			ivjtxtQty.setBounds(143, 107, 55, 20); 
			// user code begin {1} 
			// user code end 
		} catch (java.lang.Throwable ivjExc) { 
			// user code begin {2} 
			// user code end 
			handleException(ivjExc); 
		} 
	} 
	return ivjtxtQty; 
} 
/** 
 * Called whenever the part throws an exception. 
 * @param exception java.lang.Throwable 
 */ 
private void handleException(java.lang.Throwable exception) { 
 
	/* Uncomment the following lines to print uncaught exceptions to stdout */ 
	// System.out.println("--------- UNCAUGHT EXCEPTION ---------"); 
	// exception.printStackTrace(System.out); 
} 
/** 
 * Initialize the class. 
 */ 
/* WARNING: THIS METHOD WILL BE REGENERATED. */ 
private void initialize() { 
	try { 
		// user code begin {1} 
		// user code end 
		setName("InventoryScreen"); 
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 
		setSize(375, 350); 
		setTitle("Inventory Manager"); 
		setContentPane(getJFrameContentPane()); 
	} catch (java.lang.Throwable ivjExc) { 
		handleException(ivjExc); 
	} 
	// user code begin {2} 
	controller = new InventoryController(this); 
	// user code end 
} 
/** 
 * main entrypoint - starts the part when it is run as an application 
 * @param args java.lang.String[] 
 */ 
public static void main(java.lang.String[] args) { 
	try { 
		InventoryView aInventoryScreen; 
		aInventoryScreen = new InventoryView(); 
		aInventoryScreen.addWindowListener(new java.awt.event.WindowAdapter() { 
			public void windowClosing(java.awt.event.WindowEvent e) { 
				System.exit(0); 
			}; 
		}); 
		aInventoryScreen.show(); 
		java.awt.Insets insets = aInventoryScreen.getInsets(); 
		aInventoryScreen.setSize(aInventoryScreen.getWidth() + insets.left + insets.right, aInventoryScreen.getHeight() + insets.top + insets.bottom); 
		aInventoryScreen.setVisible(true); 
	} catch (Throwable exception) { 
		System.err.println("Exception occurred in main() of javax.swing.JFrame"); 
		exception.printStackTrace(System.out); 
	} 
} 
 
public void setData(InventoryData data)  
{ 
	this.data = data; 
	getbtnAdd().setEnabled(true); 
	getbtnDelete().setEnabled(true); 
	getbtnDeleteAll().setEnabled(true); 
	if ( ! data.isRecordFound()) 
	{ 
		getlblItemNum().setText(data.getItemNumber()); 
		gettxtName().setVisible(true); 
		gettxtPrice().setVisible(true); 
		getlblName().setVisible(false); 
		getlblPrice().setVisible(false); 
		getbtnDelete().setEnabled(false); 
		getbtnAdd().setEnabled(true); 
		getbtnDeleteAll().setEnabled(false); 
		gettxtName().requestFocus(); 
	} 
	else 
	{ 
		getlblItemNum().setText(data.getItemNumber()); 
		getlblName().setText(data.getName()); 
		getlblPrice().setText(data.getPrice().toString()); 
		gettxtName().setVisible(false); 
		gettxtPrice().setVisible(false); 
		getlblName().setVisible(true); 
		getlblPrice().setVisible(true); 
		gettxtQty().setText("1"); 
		getbtnDelete().setEnabled(true); 
		getbtnAdd().setEnabled(true); 
		getbtnDeleteAll().setEnabled(true); 
		gettxtQty().requestFocus(); 
	} 
	 
} 
}