📄 customermanageframe.java
字号:
tmpBtn = (JButton)components[i];
tmpBtn.addActionListener(this);
}
}
}
//退出方法
public void exit(){
//隐藏窗口
this.setVisible(false);
//清空数组的内容
customer = new String[0][17];
//清空列表框的内容
listData1.clear();
//取得面板上的所有控件
Component[] components = contentPane.getComponents();
//创建临时文本框控件
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];
//清空编辑框的内容
tmpTextField.setText("");
}
}
}
//显示查询客户的方法
public void showSearchCustomer(){
listData1.clear();
//为客户列表框加入客户数据
for(int i = 0; i < customer.length; i++){
listData1.addElement(customer[i][0]);
}
}
//显示单个客户的方法
public void showCustomer(){
//取得当前选择项的位置
int selectedIndex = jList1.getSelectedIndex();
//当列表框不处于选择状态,不显示数据
if(selectedIndex == -1){
return;
}
//显示客户的数据
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("");
}
//设置用户的方法
public void setUser(User user) {
this.user = user;
}
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
exit();
}
}
//检查按钮的状态
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(){
//取得查询选项
int selectedIndex = jComboBox1.getSelectedIndex();
String searchValue = jTextField16.getText().trim();
switch (selectedIndex) {
case 0:
//根据客户名字取得记录
customer = stockManagementData.getCustomersByCustomerName(searchValue);
break;
case 1:
//根据地区取得记录
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")){
//创建客户
int result = stockManagementData.createCustomer(customerArray);
if(result == 1){
//为列表框加入客户
listData1.addElement(customerArray[0]);
//更新客户数组
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, "客户创建失败,请检查该客户是否存在和值是否超出字段长度.");
}
}else if (action.equals("update")){
//客户信用限度保存不变
int selectedIndex = jList1.getSelectedIndex();
customerArray[15] = customer[selectedIndex][15];
//更新客户
int result = stockManagementData.updateCustomer(customerArray);
if(result == 1){
//更新客户数组
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();
//删除列表框的数据
listData1.removeElementAt(selectedIndex);
//更改数组的数据
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();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -