?? customermanageframe.java
字號:
tmpBtn = (JButton)components[i];
tmpBtn.addActionListener(this);
}
}
}
//退出方法
public void exit(){
//隱藏窗口
this.setVisible(false);
//清空數(shù)組的內(nèi)容
customer = new String[0][17];
//清空列表框的內(nèi)容
listData1.clear();
//取得面板上的所有控件
Component[] components = contentPane.getComponents();
//創(chuàng)建臨時文本框控件
JTextField tmpTextField = new JTextField();
for(int i = 0; i < components.length; i++){
if(components[i].getClass().getName().equals("javax.swing.JTextField")){
tmpTextField = (JTextField)components[i];
//清空編輯框的內(nèi)容
tmpTextField.setText("");
}
}
}
//顯示查詢客戶的方法
public void showSearchCustomer(){
listData1.clear();
//為客戶列表框加入客戶數(shù)據(jù)
for(int i = 0; i < customer.length; i++){
listData1.addElement(customer[i][0]);
}
}
//顯示單個客戶的方法
public void showCustomer(){
//取得當(dāng)前選擇項(xiàng)的位置
int selectedIndex = jList1.getSelectedIndex();
//當(dāng)列表框不處于選擇狀態(tài),不顯示數(shù)據(jù)
if(selectedIndex == -1){
return;
}
//顯示客戶的數(shù)據(jù)
jTextField1.setText(customer[selectedIndex][0]);
jTextField2.setText(customer[selectedIndex][1]);
jTextField3.setText(customer[selectedIndex][2]);
jTextField4.setText(customer[selectedIndex][3]);
jTextField5.setText(customer[selectedIndex][4]);
jTextField6.setText(customer[selectedIndex][5]);
jTextField7.setText(customer[selectedIndex][6]);
jTextField8.setText(customer[selectedIndex][7]);
jTextField9.setText(customer[selectedIndex][8]);
jTextField10.setText(customer[selectedIndex][9]);
jTextField11.setText(customer[selectedIndex][10]);
jTextField12.setText(customer[selectedIndex][11]);
jTextField13.setText(customer[selectedIndex][12]);
jTextField14.setText(customer[selectedIndex][13]);
jTextField15.setText(customer[selectedIndex][14]);
jTextArea1.setText(customer[selectedIndex][16]);
}
//清空單個客戶顯示的方法
public void clearCustomer(){
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
jTextField8.setText("");
jTextField9.setText("");
jTextField10.setText("");
jTextField11.setText("");
jTextField12.setText("");
jTextField13.setText("");
jTextField14.setText("");
jTextField15.setText("");
jTextArea1.setText("");
}
//設(shè)置用戶的方法
public void setUser(User user) {
this.user = user;
}
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
exit();
}
}
//檢查按鈕的狀態(tài)
public void checkBtn(boolean isManipulated){
if(isManipulated){
jButton2.setEnabled(false);
jButton3.setEnabled(false);
jButton4.setEnabled(false);
jButton5.setEnabled(true);
jButton6.setEnabled(true);
}else{
jButton2.setEnabled(true);
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(false);
jButton6.setEnabled(false);
}
}
//列表1的選擇事件
void jList1_valueChanged(ListSelectionEvent e) {
if(listData1.size() > 0){
this.showCustomer();
}else{
this.clearCustomer();
}
}
//查詢方法
public void search(){
//取得查詢選項(xiàng)
int selectedIndex = jComboBox1.getSelectedIndex();
String searchValue = jTextField16.getText().trim();
switch (selectedIndex) {
case 0:
//根據(jù)客戶名字取得記錄
customer = stockManagementData.getCustomersByCustomerName(searchValue);
break;
case 1:
//根據(jù)地區(qū)取得記錄
customer = stockManagementData.getCustomersByCustomerZone(searchValue);
break;
}
this.showSearchCustomer();
}
//單擊事件方法
public void actionPerformed(ActionEvent e) {
//取得按鈕的動作字符串
String actionCommand = e.getActionCommand().trim();
if(actionCommand.equals("update") | actionCommand.equals("delete")){
if(jList1.isSelectionEmpty()){
JOptionPane.showMessageDialog(null, "請選擇客戶.");
return;
}
}
//單擊按鈕的處理代碼
if (actionCommand.equals("search")) {
String searchValue = jTextField16.getText().trim();
if(searchValue.length() == 0){
JOptionPane.showMessageDialog(null, "請輸入查詢值");
return;
}
//查詢
search();
}else if(actionCommand.equals("create")){
action = "create";
this.clearCustomer();
this.checkBtn(true);
}else if(actionCommand.equals("update")){
action = "update";
this.checkBtn(true);
}else if(actionCommand.equals("delete")){
action = "delete";
this.checkBtn(true);
}else if(actionCommand.equals("ok")){
//取得客戶的值
String[] customerArray = new String[17];
customerArray[0] = jTextField1.getText().trim();
customerArray[1] = jTextField2.getText().trim();
customerArray[2] = jTextField3.getText().trim();
customerArray[3] = jTextField4.getText().trim();
customerArray[4] = jTextField5.getText().trim();
customerArray[5] = jTextField6.getText().trim();
customerArray[6] = jTextField7.getText().trim();
customerArray[7] = jTextField8.getText().trim();
customerArray[8] = jTextField9.getText().trim();
customerArray[9] = jTextField10.getText().trim();
customerArray[10] = jTextField11.getText().trim();
customerArray[11] = jTextField12.getText().trim();
customerArray[12] = jTextField13.getText().trim();
customerArray[13] = jTextField14.getText().trim();
customerArray[14] = jTextField15.getText().trim();
customerArray[15] = "0";
customerArray[16] = jTextArea1.getText().trim();
if(customerArray[0].length() == 0){
JOptionPane.showMessageDialog(null, "客戶名字不允許空值.");
return;
}
if(action.equals("create")){
//創(chuàng)建客戶
int result = stockManagementData.createCustomer(customerArray);
if(result == 1){
//為列表框加入客戶
listData1.addElement(customerArray[0]);
//更新客戶數(shù)組
String[][] tempStrs = new String[customer.length + 1][17];
System.arraycopy(customer, 0, tempStrs, 0, customer.length);
for(int i = 0; i < 17; i++){
tempStrs[customer.length][i] = customerArray[i];
}
customer = tempStrs;
jList1.setSelectedIndex(listData1.size() -1);
}else{
JOptionPane.showMessageDialog(null, "客戶創(chuàng)建失敗,請檢查該客戶是否存在和值是否超出字段長度.");
}
}else if (action.equals("update")){
//客戶信用限度保存不變
int selectedIndex = jList1.getSelectedIndex();
customerArray[15] = customer[selectedIndex][15];
//更新客戶
int result = stockManagementData.updateCustomer(customerArray);
if(result == 1){
//更新客戶數(shù)組
for(int i = 0; i < 17; i++){
if(i == 15){
continue;
}else{
customer[selectedIndex][i] = customerArray[i];
}
}
}else{
JOptionPane.showMessageDialog(null, "客戶更新失敗.");
}
}else if (action.equals("delete")){
//刪除客戶
int result = stockManagementData.deleteCustomer(customerArray[0]);
if(result == 1){
int selectedIndex = jList1.getSelectedIndex();
//刪除列表框的數(shù)據(jù)
listData1.removeElementAt(selectedIndex);
//更改數(shù)組的數(shù)據(jù)
String[][] tempStrs = new String[customer.length -1][17];
int line = 0;
for(int i = 0; i < customer.length; i++){
if(i == selectedIndex){
continue;
}else{
for(int j = 0; j < 17; j++){
tempStrs[line][j] = customer[i][j];
}
line++;
}
}
customer = tempStrs;
//清空編輯框的值
this.clearCustomer();
}else{
JOptionPane.showMessageDialog(null, "客戶刪除失敗.");
}
}
this.checkBtn(false);
}else if(actionCommand.equals("cancel")){
this.jList1_valueChanged(null);
this.checkBtn(false);
}else if(actionCommand.equals("exit")){
exit();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -