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

📄 jinhuodan_iframe.java

📁 java 从入门到精通 清华大学出版社 适合初学者调试使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * @return javax.swing.JTextField
	 */
	private JTextField getPzslField() {
		if (pzslField == null) {
			pzslField = new JTextField();
			pzslField.setEditable(false);
		}
		return pzslField;
	}

	/**
	 * 初始化(货品总数)文本框的方法
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getHpzsField() {
		if (hpzsField == null) {
			hpzsField = new JTextField();
			hpzsField.setEditable(false);
		}
		return hpzsField;
	}

	/**
	 * 初始化(合计金额)文本框的方法
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getHjjeField() {
		if (hjjeField == null) {
			hjjeField = new JTextField();
			hjjeField.setEditable(false);
		}
		return hjjeField;
	}

	/**
	 * 初始化(验收结论)文本框的方法
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getYsjlField() {
		if (ysjlField == null) {
			ysjlField = new JTextField();
		}
		return ysjlField;
	}

	/**
	 * 初始化(添加)按钮的方法
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getTjButton() {
		if (tjButton == null) {
			tjButton = new JButton();
			tjButton.setText("添加");
			tjButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// 初始化票号
					java.sql.Date date = new java.sql.Date(jhsjDate.getTime());
					jhsjField.setText(date.toString());
					String maxId = Dao.getRuKuMainMaxId(date);
					idField.setText(maxId);
					// 结束表格中没有编写的单元
					stopTableCellEditing();
					// 如果表格中不包含空行,就添加新行
					for (int i = 0; i <= table.getRowCount()-1; i++) {
						if (table.getValueAt(i, 0) == null)
							return;
					}
					DefaultTableModel model = (DefaultTableModel) table
							.getModel();
					model.addRow(new Vector());
				}
			});
		}
		return tjButton;
	}

	/**
	 * 初始化(入库)按钮的方法
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getRukuButton() {
		if (rukuButton == null) {
			rukuButton = new JButton();
			rukuButton.setText("入库");
			rukuButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					stopTableCellEditing(); // 结束表格中没有编写的单元
					String pzsStr = pzslField.getText(); // 品种数
					String jeStr = hjjeField.getText(); // 合计金额
					String jsfsStr = jsfsComboBox.getSelectedItem().toString(); // 结算方式
					String jsrStr = jsrComboBox.getSelectedItem() + ""; // 经手人
					String czyStr = jsrComboBox.getSelectedItem() + ""; // 操作员
					String rkDate = jhsjField.getText(); // 入库时间
					String ysjlStr = ysjlField.getText().trim(); // 验收结论
					String id = idField.getText(); // 票号
					String gysName = gysComboBox.getSelectedItem() + "";// 供应商名字
					if (jsrStr == null || jsrStr.isEmpty()) {
						JOptionPane.showMessageDialog(JinHuoDan_IFrame.this,
								"请填写经手人");
						return;
					}
					if (ysjlStr == null || ysjlStr.isEmpty()) {
						JOptionPane.showMessageDialog(JinHuoDan_IFrame.this,
								"填写验收结论");
						return;
					}
					if (table.getRowCount() <= 0) {
						JOptionPane.showMessageDialog(JinHuoDan_IFrame.this,
								"填加入库商品");
						return;
					}
					TbRukuMain ruMain = new TbRukuMain(id, pzsStr, jeStr,
							ysjlStr, gysName, rkDate, czyStr, jsrStr, jsfsStr);
					Set<TbRukuDetail> set = ruMain.getTabRukuDetails();
					int rows = table.getRowCount();
					for (int i = 0; i < rows; i++) {
						TbSpinfo spinfo = (TbSpinfo) table.getValueAt(i, 0);
						if (spinfo == null || spinfo.getId() == null
								|| spinfo.getId().isEmpty())
							continue;
						String djStr = (String) table.getValueAt(i, 6);
						String slStr = (String) table.getValueAt(i, 7);
						Double dj = Double.valueOf(djStr);
						Integer sl = Integer.valueOf(slStr);
						TbRukuDetail detail = new TbRukuDetail();
						detail.setTabSpinfo(spinfo.getId());
						detail.setTabRukuMain(ruMain.getRkId());
						detail.setDj(dj);
						detail.setSl(sl);
						set.add(detail);
					}
					boolean rs = Dao.insertRukuInfo(ruMain);
					if (rs) {
						JOptionPane.showMessageDialog(JinHuoDan_IFrame.this,
								"入库完成");
						DefaultTableModel dftm = new DefaultTableModel();
						table.setModel(dftm);
						pzslField.setText("0");
						hpzsField.setText("0");
						hjjeField.setText("0");
					}
				}
			});
		}
		return rukuButton;
	}

	/**
	 * 初始化表格滚动面板的方法
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getTablePane() {
		if (tablePane == null) {
			tablePane = new JScrollPane();
			tablePane.setViewportView(getTable());
		}
		return tablePane;
	}

	/**
	 * 初始化表格的方法
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTable() {
		if (table == null) {
			String[] columnNames = { "商品名称", "商品编号", "产地", "单位", "规格", "包装",
					"单价", "数量", "批号", "批准文号" };
			table = new JTable();
			table.setBorder(BorderFactory
					.createBevelBorder(BevelBorder.LOWERED));
			table.setShowGrid(true);
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			((DefaultTableModel) table.getModel())
					.setColumnIdentifiers(columnNames);
			TableColumn column = table.getColumnModel().getColumn(0);
			final DefaultCellEditor editor = new DefaultCellEditor(
					getSpComboBox());
			column.setCellEditor(editor);
			table.addPropertyChangeListener(new PropertyChangeListener() {
				public void propertyChange(java.beans.PropertyChangeEvent e) {
					if ((e.getPropertyName().equals("tableCellEditor"))) {
						ComputeInfo();
					}
				}
			});
		}
		return table;
	}

	/**
	 * 初始化商品下拉选择框的方法
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getSpComboBox() {
		if (spComboBox == null) {
			spComboBox = new JComboBox();
			spComboBox.addItem(new TbSpinfo());
			spComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ResultSet set = Dao
							.query("select * from tb_spinfo where gysName='"
									+ getGysComboBox().getSelectedItem() + "'");
					updateSpComboBox(set);
				}
			});
			spComboBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					TbSpinfo info = (TbSpinfo) spComboBox.getSelectedItem();
					// 如果选择有效就更新表格
					if (info != null && info.getId() != null) {
						updateTable();
					}
				}
			});
		}
		return spComboBox;
	}

	/**
	 * 更新商品下拉选择框的方法
	 * 
	 * @param set:JDBC返回的ResultSet结果集
	 */
	private void updateSpComboBox(ResultSet set) {
		try {
			while (set.next()) {
				TbSpinfo spinfo = new TbSpinfo();
				spinfo.setId(set.getString("id").trim());
				spinfo.setSpname(set.getString("spname").trim());
				spinfo.setCd(set.getString("cd").trim());
				spinfo.setJc(set.getString("jc").trim());
				spinfo.setDw(set.getString("dw").trim());
				spinfo.setGg(set.getString("gg").trim());
				spinfo.setBz(set.getString("bz").trim());
				spinfo.setPh(set.getString("ph").trim());
				spinfo.setPzwh(set.getString("pzwh").trim());
				spinfo.setMemo(set.getString("memo").trim());
				spinfo.setGysname(set.getString("gysname").trim());
				DefaultComboBoxModel model = (DefaultComboBoxModel) spComboBox
						.getModel();
				if (model.getIndexOf(spinfo) < 0) // 下来选择框不包含该商品
					spComboBox.addItem(spinfo); // 则添加选项
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * 根据商品下拉框的选择,更新表格当前行的内容
	 * 
	 * @return 无
	 */
	private synchronized void updateTable() {
		TbSpinfo spinfo = (TbSpinfo) spComboBox.getSelectedItem();
		int row = table.getSelectedRow();
		if (row >= 0 && spinfo != null) {
			table.setValueAt(spinfo.getId(), row, 1);
			table.setValueAt(spinfo.getCd(), row, 2);
			table.setValueAt(spinfo.getDw(), row, 3);
			table.setValueAt(spinfo.getGg(), row, 4);
			table.setValueAt(spinfo.getBz(), row, 5);
			table.setValueAt("0", row, 6);
			table.setValueAt("0", row, 7);
			table.setValueAt(spinfo.getPh(), row, 8);
			table.setValueAt(spinfo.getPzwh(), row, 9);
			table.editCellAt(row, 6);
		}
	}

	/**
	 * 初始化供应商下拉选择框的方法
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getGysComboBox() {
		if (gysComboBox == null) {
			gysComboBox = new JComboBox();
			List gysInfos = Dao.getGysInfos();
			for (Iterator iter = gysInfos.iterator(); iter.hasNext();) {
				List list = (List) iter.next();
				Item item = new Item();
				item.setId(list.get(0).toString().trim());
				item.setName(list.get(1).toString().trim());
				gysComboBox.addItem(item);
			}
			Item item = (Item) gysComboBox.getSelectedItem();
			TbGysinfo gysInfo = Dao.getGysInfo(item);
			getLxrField().setText(gysInfo.getLian());
		}
		return gysComboBox;
	}

	/**
	 * 该方法用于停止表格单元的编辑
	 */
	private void stopTableCellEditing() {
		TableCellEditor cellEditor = table.getCellEditor();
		if (cellEditor != null)
			cellEditor.stopCellEditing();
	}

	/**
	 * 初始化(经手人)下来选择框的方法
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJsrComboBox() {
		if (jsrComboBox == null) {
			jsrComboBox = new JComboBox();
			List<List> czyList = Dao.getJsrs();
			for (List<String> list : czyList) {
				String id = list.get(0);
				String name = list.get(1);
				Item item = new Item(id, name);
				item.setId(id + "");
				item.setName(name);
				jsrComboBox.addItem(item);
			}
		}
		return jsrComboBox;
	}

	/**
	 * @author lzwJava <br>
	 *         事件处理器,该处理器用于计算货品总数、合计金额等信息。
	 */
	private final void ComputeInfo() {
			// 计算代码
			int rows = table.getRowCount();
			int count = 0;
			double money = 0.0;
			// 计算品种数量
			TbSpinfo column = null;
			Object valueAt = table.getValueAt(rows - 1, 0);
			if(!(valueAt instanceof TbSpinfo))
				return;
			if (rows > 0)
				column = (TbSpinfo) valueAt;
			if (rows > 0 && (column == null || column.getId().isEmpty()))
				rows--;
			// 计算货品总数和金额
			for (int i = 0; i < rows; i++) {
				String column7 = (String) table.getValueAt(i, 7);
				String column6 = (String) table.getValueAt(i, 6);
				int c7 = (column7 == null || column7.isEmpty()) ? 0 : Integer
						.parseInt(column7);
				float c6 = (column6 == null || column6.isEmpty()) ? 0 : Float
						.parseFloat(column6);
				count += c7;
				money += c6 * c7;
			}

			pzslField.setText(rows + "");
			hpzsField.setText(count + "");
			hjjeField.setText(money + "");
	}
} // @jve:decl-index=0:visual-constraint="13,4"

⌨️ 快捷键说明

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