?? stocktakecheckframe.java
字號:
package stockmanageinterface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.*;
import data.*;
import maininterface.*;
import user.*;
public class StocktakeCheckFrame extends JFrame implements ActionListener {
JPanel contentPane;
//創建標簽控件
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
//創建按鈕控件
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
//創建滾動框控件
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
//創建列表框數據類和列表框控件
DefaultListModel listData1 = new DefaultListModel();
JList jList1 = new JList(listData1);
//創建表格控件
JTable jTable1 = new JTable();
//創建表格模式類
StockManagementTableModel smTableModel = new StockManagementTableModel();
//創建標題數組
String[] colNames = {"商品條形碼", "庫存數", "盤點數", "差異數"};
//創建字體類
Font dialog13 = new java.awt.Font("Dialog", 0, 13);
//聲明數據類
StockManagementData stockManagementData = null;
//聲明用戶類
User user = null;
//聲明主窗口類
StockManagementMainFrame stockManagementMainFrame = null;
//創建倉庫數組
String[][] warehouses = new String[0][4];
//創建盤點核查數組
String[][] stocktakeArray = new String[0][4];
//創建帳套日期字符串
String ledgerDate = "";
public StocktakeCheckFrame(StockManagementMainFrame stockManagementMainFrame) {
this.stockManagementMainFrame = stockManagementMainFrame;
//取得主窗口的數據類
stockManagementData = stockManagementMainFrame.getStockManagementData();
//取得主窗口的用戶類
user = stockManagementMainFrame.getUser();
//取得主窗口的賬套日期
ledgerDate = stockManagementMainFrame.getLedgerDate();
//取得庫存模塊的用戶權限
int stockManageFunction = user.getStockManageFunction();
//檢查用戶權限
if ( (stockManageFunction & 32) != 32) {
JOptionPane.showMessageDialog(null, user.getUserName() + "用戶不具有該權限.");
System.exit(0);
}
//檢查賬套日期
if(ledgerDate.length() == 0){
JOptionPane.showMessageDialog(null, user.getUserName() + "請選擇賬套.");
return;
}
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(667, 352));
this.setTitle("庫存盤點核查窗口");
//設置標簽的屬性
jLabel1.setText("倉庫列表");
jLabel1.setBounds(new Rectangle(28, 19, 134, 16));
jLabel2.setText("盤點單核查表");
jLabel2.setBounds(new Rectangle(195, 19, 165, 16));
//設置按鈕的屬性
jButton1.setText("顯示倉庫");
jButton1.setActionCommand("showWarehouse");
jButton1.setBounds(new Rectangle(28, 257, 103, 25));
jButton2.setText("顯示差異記錄");
jButton2.setActionCommand("showDifference");
jButton2.setBounds(new Rectangle(140, 257, 120, 25));
jButton3.setText("顯示相同記錄");
jButton3.setActionCommand("showSame");
jButton3.setBounds(new Rectangle(269, 257, 120, 25));
jButton4.setText("顯示全部記錄");
jButton4.setActionCommand("showAll");
jButton4.setBounds(new Rectangle(398, 257, 120, 25));
jButton5.setText("退出");
jButton5.setActionCommand("exit");
jButton5.setBounds(new Rectangle(527, 257, 103, 25));
//設置滾動框的屬性
jScrollPane1.getViewport().add(jList1, null);
jScrollPane1.setBounds(new Rectangle(28, 45, 152, 195));
jScrollPane2.getViewport().add(jTable1, null);
jScrollPane2.setBounds(new Rectangle(195, 45, 435, 195));
//為列表框加入選擇接收器
jList1.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//當多種事件被激發的時候,不執行接收器后面的代碼
if (e.getValueIsAdjusting()) return;
jList1_valueChanged(e);
}
});
//為面板加入各個控件
contentPane.add(jLabel1, null);
contentPane.add(jLabel2, null);
contentPane.add(jScrollPane1, null);
contentPane.add(jScrollPane2, null);
contentPane.add(jButton1, null);
contentPane.add(jButton2, null);
contentPane.add(jButton3, null);
contentPane.add(jButton4, null);
contentPane.add(jButton5, null);
//設置窗口類的字體和為按鈕加入動作接收器
setupFontAndListener();
}
//設置窗口類的字體和為按鈕加入動作接收器的方法
public void setupFontAndListener(){
Component[] components = contentPane.getComponents();
//創建臨時按鈕控件
JButton tmpBtn = new JButton();
for(int i = 0; i < components.length; i++){
components[i].setFont(dialog13);
if(components[i].getClass().getName().equals("javax.swing.JButton")){
tmpBtn = (JButton)components[i];
tmpBtn.addActionListener(this);
}
}
}
//退出方法
public void exit(){
//隱藏窗口
this.setVisible(false);
//清空數組的內容
warehouses = new String[0][4];
stocktakeArray = new String[0][4];
//清空列表框的內容
listData1.clear();
//清空表格的內容
this.showTableData(stocktakeArray);
}
//設置用戶的方法
public void setUser(User user) {
this.user = user;
}
//設置賬套的方法
public void setLedgerDate(String ledgerDate) {
this.ledgerDate = ledgerDate;
}
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
exit();
}
}
//顯示倉庫的方法
public void showWarehouse(){
listData1.clear();
//取得倉庫的值
warehouses = stockManagementData.getAllWarehouse();
//為倉庫列表框加入倉庫數據
for(int i = 0; i < warehouses.length; i++){
listData1.addElement(warehouses[i][0]);
}
}
//顯示盤點單核查表記錄的方法
public void showStocktakeArray(){
//取得當前選擇項的位置
int selectedIndex = jList1.getSelectedIndex();
//當列表框不處于選擇狀態,不顯示數據
if(selectedIndex == -1){
return;
}
//取得倉庫名
String warehouse = (String)listData1.getElementAt(selectedIndex);
//根據倉庫取得庫存數
String[][] stockQuantities = stockManagementData.
getStocktakeQuantityByWarehouse(ledgerDate, warehouse, 2);
//創建庫存商品條形碼集合類
Vector stockGoodsBarcode = new Vector();
for(int i = 0; i < stockQuantities.length; i++){
stockGoodsBarcode.addElement(stockQuantities[i][0]);
}
//根據倉庫取得盤點數
String[][] stocktakeQuantities = stockManagementData.
getStocktakeQuantityByWarehouse(ledgerDate, warehouse, 4);
//創建盤點商品條形碼集合類
Vector stocktakeGoodsBarcode = new Vector();
for(int i = 0; i < stocktakeQuantities.length; i++){
stocktakeGoodsBarcode.addElement(stocktakeQuantities[i][0]);
}
//創建商品條形碼的集合類,過濾重復的商品條形碼
TreeSet goodsBarcodeSet = new TreeSet();
for(int i = 0; i < stockQuantities.length; i++){
goodsBarcodeSet.add(stockQuantities[i][0]);
}
for(int i = 0; i < stocktakeQuantities.length; i++){
goodsBarcodeSet.add(stocktakeQuantities[i][0]);
}
//重新創建盤點核查數組
stocktakeArray = new String[goodsBarcodeSet.size()][4];
java.util.Iterator iterator = goodsBarcodeSet.iterator();
//將數據放入盤點核查數組
int i = 0;
while(iterator.hasNext()){
String goodsBarcode = (String)iterator.next();
//取得庫存數
int stockQuantity = 0;
if(stockGoodsBarcode.indexOf(goodsBarcode) != -1){
stockQuantity = Integer.parseInt(stockQuantities[stockGoodsBarcode.indexOf(goodsBarcode)][1]);
}
//取得盤點數
int stocktakeQuantity = 0;
if(stocktakeGoodsBarcode.indexOf(goodsBarcode) != -1){
stocktakeQuantity = Integer.parseInt(stocktakeQuantities[stocktakeGoodsBarcode.indexOf(goodsBarcode)][1]);
}
stocktakeArray[i][0] = goodsBarcode; //商品條形碼
stocktakeArray[i][1] = String.valueOf(stockQuantity); //庫存數
stocktakeArray[i][2] = String.valueOf(stocktakeQuantity); //盤點數
stocktakeArray[i][3] = String.valueOf(stocktakeQuantity - stockQuantity); //差異數
i++;
}
this.showTableData(stocktakeArray);
}
//顯示盤點差異記錄的方法
public void showDifferenceStocktakeArray(){
String[][] tempStocktakeArray = new String[stocktakeArray.length][4];
int line = 0;
for(int i = 0; i < stocktakeArray.length; i++){
int difference = Integer.parseInt(stocktakeArray[i][3]);
if(difference != 0){
tempStocktakeArray[line][0] = stocktakeArray[i][0];
tempStocktakeArray[line][1] = stocktakeArray[i][1];
tempStocktakeArray[line][2] = stocktakeArray[i][2];
tempStocktakeArray[line][3] = stocktakeArray[i][3];
line++;
}
}
String[][] temp = new String[line][4];
System.arraycopy(tempStocktakeArray, 0, temp, 0, line);
//顯示差異記錄
this.showTableData(temp);
}
//顯示盤點相同記錄的方法
public void showSameStocktakeArray(){
String[][] tempStocktakeArray = new String[stocktakeArray.length][4];
int line = 0;
for(int i = 0; i < stocktakeArray.length; i++){
int difference = Integer.parseInt(stocktakeArray[i][3]);
if(difference == 0){
tempStocktakeArray[line][0] = stocktakeArray[i][0];
tempStocktakeArray[line][1] = stocktakeArray[i][1];
tempStocktakeArray[line][2] = stocktakeArray[i][2];
tempStocktakeArray[line][3] = stocktakeArray[i][3];
line++;
}
}
String[][] temp = new String[line][4];
System.arraycopy(tempStocktakeArray, 0, temp, 0, line);
//顯示相同記錄
this.showTableData(temp);
}
//顯示盤點全部記錄的方法
public void showAllStocktakeArray(){
this.showTableData(stocktakeArray);
}
//顯示表格內容的方法
public void showTableData(Object[][] detail){
//設置表格的標題
smTableModel.setColumnNames(colNames);
//設置表格的數據
smTableModel.setData(detail);
jTable1 = new JTable(smTableModel);
//設置表格的字體
jTable1.setFont(dialog13);
//將數據表格加入數據滾動框
jScrollPane2.getViewport().add(jTable1, null);
//設置列的寬度
jTable1.getColumnModel().getColumn(0).setPreferredWidth(50);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(20);
jTable1.getColumnModel().getColumn(2).setPreferredWidth(20);
jTable1.getColumnModel().getColumn(3).setPreferredWidth(20);
}
//清空盤點單核查表數據的方法
public void clearStocktakeArray(){
stocktakeArray = new String[0][4];
this.showTableData(stocktakeArray);
}
//列表1的選擇事件
void jList1_valueChanged(ListSelectionEvent e) {
if(listData1.size() > 0){
this.showStocktakeArray();
}else{
this.clearStocktakeArray();
}
}
//單擊事件
public void actionPerformed(ActionEvent e) {
//取得按鈕的動作字符串
String actionCommand = e.getActionCommand().trim();
if (actionCommand.equals("showWarehouse")) {
this.showWarehouse();
}else if(actionCommand.equals("showDifference")){
this.showDifferenceStocktakeArray();
}else if(actionCommand.equals("showSame")){
this.showSameStocktakeArray();
}else if(actionCommand.equals("showAll")){
this.showAllStocktakeArray();
}else if(actionCommand.equals("exit")){
exit();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -