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

📄 accountnamemanageframe.java~431~

📁 一个使用java 写的进销存代码 使用了ejb 等技术 是学习j2ee的好
💻 JAVA~431~
📖 第 1 页 / 共 2 页
字号:
    //清空结构树的内容
    root.removeAllChildren();
    //更新结构树的显示
    treeModel.reload();
    //显示结构树的内容
    this.showAccountNameTree(0, root);
    //展开结构树的节点
    this.expandEntireTree(root);
  }
  //展开节点的方法
  public void expandEntireTree(TreeNode node) {
    TreePath treePath = new TreePath( ( (DefaultMutableTreeNode) node).getPath());
    jTree1.expandPath(treePath);
    for (int i = 0; i < node.getChildCount(); i++) {
      expandEntireTree(node.getChildAt(i));
    }
  }
  //通过递归算法显示会计科目结构树的方法
  public void showAccountNameTree(int parentId, DefaultMutableTreeNode parentTreeNode){
    //根据父标识取得会计科目记录
    String[][] tempAccountName = stockManagementData.getAccountNameByParentid(parentId);
    //为会计科目结构树加入会计科目数据
    DefaultMutableTreeNode tempTreeNode = null;
    for(int i = 0; i < tempAccountName.length; i++){
      //创建一个树节点
      tempTreeNode = new DefaultMutableTreeNode(tempAccountName[i][0] + "、" +
                                                tempAccountName[i][2]);
      //为会计科目树结构集合添加记录
      treeAccountNameVector.addElement(tempAccountName[i][2]);
      //为父节点加入树节点
      parentTreeNode.add(tempTreeNode);
      //显示下一级会计科目
      this.showAccountNameTree(Integer.parseInt(tempAccountName[i][0]), tempTreeNode);
    }
  }
  //结构树的选择事件
  public void jTree1_valueChanged(TreeSelectionEvent e) {
    //取得选择的树节点对象
    if(!jTree1.isSelectionEmpty()){
      this.showAccountName();
    }else{
      this.clearAccountName();
    }
  }
  //显示单个会计科目的方法
  public void showAccountName(){
    //取得当前选择节点
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)
        jTree1.getLastSelectedPathComponent();
    String tempStr = (String)node.getUserObject();
    String tempAccountId = tempStr.substring(0, tempStr.indexOf("、"));
    if(Integer.parseInt(tempAccountId) == 0){
      //当选择根节点时,清空编辑框的内容
      this.clearAccountName();
    }else{
      //显示会计科目的数据
      int selectedIndex = accountNameVector.indexOf(tempAccountId);
      jTextField2.setText(accountName[selectedIndex][0]);
      jTextField3.setText(accountName[selectedIndex][1]);
      jTextField4.setText(accountName[selectedIndex][2]);
    }
  }
  //清空单个会计科目显示的方法
  public void clearAccountName(){
    jTextField2.setText("");
    jTextField3.setText("");
    jTextField4.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);
    }
  }
  //查询方法
  public void search(String searchValue){
    //展开结构树的所有树技
    this.expandEntireTree(root);
    String tempAccountName = "";
    //根据查询值选择树选项
    for(int i = 0; i < treeAccountNameVector.size(); i++){
      tempAccountName = (String)treeAccountNameVector.elementAt(i);
      if(tempAccountName.indexOf(searchValue) > -1){
        //1表示根节点的位置
        jTree1.setSelectionRow(i + 1);
        break;
      }
    }
  }
  //单击事件方法
  public void actionPerformed(ActionEvent e) {
    //取得按钮的动作字符串
    String actionCommand = e.getActionCommand().trim();
    if(actionCommand.equals("update") | actionCommand.equals("delete")){
      if(jTree1.isSelectionEmpty()){
        JOptionPane.showMessageDialog(null, "请选择会计科目.");
        return;
      }
    }
    //单击按钮的处理代码
    if (actionCommand.equals("search")) {
      String searchValue = jTextField1.getText().trim();
      if(searchValue.length() == 0){
        JOptionPane.showMessageDialog(null, "请输入查询值");
        return;
      }
      //根据查询值选择树节点
      this.search(searchValue);
    }else if(actionCommand.equals("showAll")){
      //重新取得会计科目数组和显示结构树
      this.showAllAccountName();
    }else if(actionCommand.equals("create")){
      action = "create";
      this.clearAccountName();
      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 accountId = jTextField2.getText().trim();
      String parentIdStr = jTextField3.getText().trim();
      int parentId = 0;
      String accountName = jTextField4.getText().trim();
      if(action.equals("create") | action.equals("update")){
        if(parentIdStr.length() == 0 | accountName.length() == 0){
          JOptionPane.showMessageDialog(null, "父索引和会计科目名字不允许空值.");
          return;
        }
        //检查父索引是否整数
        if(dataMethod.checkInt(parentIdStr) == 0){
          JOptionPane.showMessageDialog(null, "父索引必须是整数.");
          return;
        }else{
          parentId = Integer.parseInt(parentIdStr);
        }
        //检查父索引是否存在
        if(accountNameVector.indexOf(parentIdStr) == -1){
          if(parentId != 0){
            JOptionPane.showMessageDialog(null, "父索引不存在,请检查会计科目的序号.");
            return;
          }
        }
      }
      if(action.equals("create")){
        //创建会计科目
        int result = stockManagementData.createAccountName(parentId, accountName);
        if(result == 1){
          //重新取得会计科目
          this.showAllAccountName();
          //选择新创建的会计科目
          this.search(accountName);
        }else{
          JOptionPane.showMessageDialog(null, "会计科目创建失败,请检查该会计科目"
                                        + "值是否超出字段长度.");
        }
      }else if (action.equals("update")){
        //更新会计科目
        int result = stockManagementData.updateAccountName(Integer.parseInt(
            accountId), parentId, accountName);
        if(result == 1){
          //重新取得会计科目
          this.showAllAccountName();
          //选择更新后的会计科目
          this.search(accountName);
        }else{
          JOptionPane.showMessageDialog(null, "会计科目更新失败.");
        }
      }else if (action.equals("delete")){
        if(accountId.length() == 0){
          JOptionPane.showMessageDialog(null, "会计科目删除失败.");

        }
        //删除会计科目
        int result = stockManagementData.deleteAccountName(Integer.parseInt(accountId));
        if(result == 1){
          //重新取得会计科目
          this.showAllAccountName();
        }else{
          JOptionPane.showMessageDialog(null, "会计科目删除失败.");
        }
      }
      this.checkBtn(false);
    }else if(actionCommand.equals("cancel")){
       this.jTree1_valueChanged(null);
       this.checkBtn(false);
    }else if(actionCommand.equals("exit")){
      exit();
    }
  }
}

⌨️ 快捷键说明

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