?? accountnamemanageframe.java
字號(hào):
root.removeAllChildren();
//更新結(jié)構(gòu)樹(shù)的顯示
treeModel.reload();
//顯示結(jié)構(gòu)樹(shù)的內(nèi)容
this.showAccountNameTree(0, root);
//展開(kāi)結(jié)構(gòu)樹(shù)的節(jié)點(diǎn)
this.expandEntireTree(root);
}
//展開(kāi)節(jié)點(diǎn)的方法
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));
}
}
//通過(guò)遞歸算法顯示會(huì)計(jì)科目結(jié)構(gòu)樹(shù)的方法
public void showAccountNameTree(int parentId, DefaultMutableTreeNode parentTreeNode){
//根據(jù)父標(biāo)識(shí)取得會(huì)計(jì)科目記錄
String[][] tempAccountName = stockManagementData.getAccountNameByParentid(parentId);
//為會(huì)計(jì)科目結(jié)構(gòu)樹(shù)加入會(huì)計(jì)科目數(shù)據(jù)
DefaultMutableTreeNode tempTreeNode = null;
for(int i = 0; i < tempAccountName.length; i++){
//創(chuàng)建一個(gè)樹(shù)節(jié)點(diǎn)
tempTreeNode = new DefaultMutableTreeNode(tempAccountName[i][0] + "、" +
tempAccountName[i][2]);
//為會(huì)計(jì)科目樹(shù)結(jié)構(gòu)集合添加記錄
treeAccountNameVector.addElement(tempAccountName[i][2]);
//為父節(jié)點(diǎn)加入樹(shù)節(jié)點(diǎn)
parentTreeNode.add(tempTreeNode);
//顯示下一級(jí)會(huì)計(jì)科目
this.showAccountNameTree(Integer.parseInt(tempAccountName[i][0]), tempTreeNode);
}
}
//結(jié)構(gòu)樹(shù)的選擇事件
public void jTree1_valueChanged(TreeSelectionEvent e) {
//取得選擇的樹(shù)節(jié)點(diǎn)對(duì)象
if(!jTree1.isSelectionEmpty()){
this.showAccountName();
}else{
this.clearAccountName();
}
}
//顯示單個(gè)會(huì)計(jì)科目的方法
public void showAccountName(){
//取得當(dāng)前選擇節(jié)點(diǎn)
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
jTree1.getLastSelectedPathComponent();
String tempStr = (String)node.getUserObject();
String tempAccountId = tempStr.substring(0, tempStr.indexOf("、"));
if(Integer.parseInt(tempAccountId) == 0){
//當(dāng)選擇根節(jié)點(diǎn)時(shí),清空編輯框的內(nèi)容
this.clearAccountName();
}else{
//顯示會(huì)計(jì)科目的數(shù)據(jù)
int selectedIndex = accountNameVector.indexOf(tempAccountId);
jTextField2.setText(accountName[selectedIndex][0]);
jTextField3.setText(accountName[selectedIndex][1]);
jTextField4.setText(accountName[selectedIndex][2]);
}
}
//清空單個(gè)會(huì)計(jì)科目顯示的方法
public void clearAccountName(){
jTextField2.setText("");
jTextField3.setText("");
jTextField4.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);
}
}
//查詢方法
public void search(String searchValue){
//展開(kāi)結(jié)構(gòu)樹(shù)的所有樹(shù)技
this.expandEntireTree(root);
String tempAccountName = "";
//根據(jù)查詢值選擇樹(shù)選項(xiàng)
for(int i = 0; i < treeAccountNameVector.size(); i++){
tempAccountName = (String)treeAccountNameVector.elementAt(i);
if(tempAccountName.indexOf(searchValue) > -1){
//1表示根節(jié)點(diǎn)的位置
jTree1.setSelectionRow(i + 1);
break;
}
}
}
//單擊事件方法
public void actionPerformed(ActionEvent e) {
//取得按鈕的動(dòng)作字符串
String actionCommand = e.getActionCommand().trim();
if(actionCommand.equals("update") | actionCommand.equals("delete")){
if(jTree1.isSelectionEmpty()){
JOptionPane.showMessageDialog(null, "請(qǐng)選擇會(huì)計(jì)科目.");
return;
}
}
//單擊按鈕的處理代碼
if (actionCommand.equals("search")) {
String searchValue = jTextField1.getText().trim();
if(searchValue.length() == 0){
JOptionPane.showMessageDialog(null, "請(qǐng)輸入查詢值");
return;
}
//根據(jù)查詢值選擇樹(shù)節(jié)點(diǎn)
this.search(searchValue);
}else if(actionCommand.equals("showAll")){
//重新取得會(huì)計(jì)科目數(shù)組和顯示結(jié)構(gòu)樹(shù)
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")){
//取得會(huì)計(jì)科目的值
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, "父索引和會(huì)計(jì)科目名字不允許空值.");
return;
}
//檢查父索引是否整數(shù)
if(dataMethod.checkInt(parentIdStr) == 0){
JOptionPane.showMessageDialog(null, "父索引必須是整數(shù).");
return;
}else{
parentId = Integer.parseInt(parentIdStr);
}
//檢查父索引是否存在
if(accountNameVector.indexOf(parentIdStr) == -1){
if(parentId != 0){
JOptionPane.showMessageDialog(null, "父索引不存在,請(qǐng)檢查會(huì)計(jì)科目的序號(hào).");
return;
}
}
}
if(action.equals("create")){
//創(chuàng)建會(huì)計(jì)科目
int result = stockManagementData.createAccountName(parentId, accountName);
if(result == 1){
//重新取得會(huì)計(jì)科目
this.showAllAccountName();
//選擇新創(chuàng)建的會(huì)計(jì)科目
this.search(accountName);
}else{
JOptionPane.showMessageDialog(null, "會(huì)計(jì)科目創(chuàng)建失敗,請(qǐng)檢查該會(huì)計(jì)科目"
+ "值是否超出字段長(zhǎng)度.");
}
}else if (action.equals("update")){
//更新會(huì)計(jì)科目
int result = stockManagementData.updateAccountName(Integer.parseInt(
accountId), parentId, accountName);
if(result == 1){
//重新取得會(huì)計(jì)科目
this.showAllAccountName();
//選擇更新后的會(huì)計(jì)科目
this.search(accountName);
}else{
JOptionPane.showMessageDialog(null, "會(huì)計(jì)科目更新失敗.");
}
}else if (action.equals("delete")){
if(accountId.length() == 0){
JOptionPane.showMessageDialog(null, "請(qǐng)選擇會(huì)計(jì)科目.");
return;
}
//刪除會(huì)計(jì)科目
int result = stockManagementData.deleteAccountName(Integer.parseInt(accountId));
if(result == 1){
//重新取得會(huì)計(jì)科目
this.showAllAccountName();
}else{
JOptionPane.showMessageDialog(null, "會(huì)計(jì)科目刪除失敗.");
}
}
this.checkBtn(false);
}else if(actionCommand.equals("cancel")){
this.jTree1_valueChanged(null);
this.checkBtn(false);
}else if(actionCommand.equals("exit")){
exit();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -