?? accountnamemanageframe.java~1~
字號:
package baseinforinterface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import data.*;
import maininterface.*;
import user.*;
import method.*;
import java.util.*;
public class AccountNameManageFrame extends JFrame implements ActionListener{
JPanel contentPane;
//創建滾動框
JScrollPane jScrollPane1 = new JScrollPane();
//創建結構樹控件和根節點
DefaultMutableTreeNode root = new DefaultMutableTreeNode("0、會計科目");;
DefaultTreeModel treeModel = new DefaultTreeModel(root);
JTree jTree1 = new JTree(treeModel);
//創建標簽控件
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
//創建編輯框控件
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
//創建按鈕控件
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JButton jButton6 = new JButton();
JButton jButton7 = new JButton();
JButton jButton8 = new JButton();
//創建字體類
Font dialog13 = new java.awt.Font("Dialog", 0, 13);
//聲明數據類
StockManagementData stockManagementData = null;
//聲明用戶類
User user = null;
//聲明主窗口類
StockManagementMainFrame stockManagementMainFrame = null;
//創建會計科目數組
String[][] accountName = new String[0][3];
//創建會計科目的序號集合
Vector accountNameVector = new Vector();
//創建動作字符串
String action = "";
public AccountNameManageFrame(StockManagementMainFrame stockManagementMainFrame) {
this.stockManagementMainFrame = stockManagementMainFrame;
//取得主窗口的數據類
stockManagementData = stockManagementMainFrame.getStockManagementData();
//取得主窗口的用戶類
user = stockManagementMainFrame.getUser();
//取得基礎信息模塊的用戶權限
int baseInforFunction = user.getBaseInforFunction();
//檢查用戶權限
if((baseInforFunction & 512) != 512){
JOptionPane.showMessageDialog(null, user.getUserName() + "用戶不具有該權限.");
System.exit(0);
}
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(620, 421));
this.setTitle("會計科目數據管理窗口");
//設置標簽控件屬性
jLabel1.setText("會計科目列表:");
jLabel1.setBounds(new Rectangle(24, 21, 94, 16));
jLabel2.setText("查詢值");
jLabel2.setBounds(new Rectangle(253, 49, 67, 16));
jLabel3.setText("序號");
jLabel3.setBounds(new Rectangle(253, 125, 70, 16));
jLabel4.setText("父索引");
jLabel4.setBounds(new Rectangle(253, 200, 70, 16));
jLabel5.setText("會計科目名字");
jLabel5.setBounds(new Rectangle(253, 276, 89, 16));
//設置編輯框控件屬性
jTextField1.setBounds(new Rectangle(348, 49, 159, 22));
jTextField2.setEditable(false);
jTextField2.setBounds(new Rectangle(348, 125, 159, 22));
jTextField3.setBounds(new Rectangle(348, 200, 159, 22));
jTextField4.setBounds(new Rectangle(348, 276, 159, 22));
//設置按鈕控件屬性
jButton1.setText("查詢");
jButton1.setActionCommand("search");
jButton1.setBounds(new Rectangle(517, 49, 68, 25));
jButton2.setText("創建");
jButton2.setActionCommand("create");
jButton2.setBounds(new Rectangle(194, 324, 62, 25));
jButton3.setText("修改");
jButton3.setActionCommand("update");
jButton3.setBounds(new Rectangle(260, 324, 62, 25));
jButton4.setText("刪除");
jButton4.setActionCommand("delete");
jButton4.setBounds(new Rectangle(326, 324, 62, 25));
jButton5.setText("確定");
jButton5.setActionCommand("ok");
jButton5.setEnabled(false);
jButton5.setBounds(new Rectangle(391, 324, 62, 25));
jButton6.setText("取消");
jButton6.setEnabled(false);
jButton6.setActionCommand("cancel");
jButton6.setBounds(new Rectangle(457, 324, 62, 25));
jButton7.setText("退出");
jButton7.setActionCommand("exit");
jButton7.setBounds(new Rectangle(523, 324, 62, 25));
jButton8.setText("顯示會計科目");
jButton8.setActionCommand("showAll");
jButton8.setBounds(new Rectangle(23, 324, 167, 25));
//設置滾動框控件屬性
jScrollPane1.setBounds(new Rectangle(23, 49, 189, 243));
jScrollPane1.getViewport().add(jTree1, null);
//為結構樹加入選擇接收器
jTree1.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
jTree1_valueChanged(e);
}
});
/*設置結構樹的屬性*/
//定義樹一次只能選擇一個節點
jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//定義樹的線條
jTree1.putClientProperty("JTree.lineStyle", "Angled");
//顯示總樹根的手柄
jTree1.setShowsRootHandles(true);
//為面板加入控件
contentPane.add(jLabel1, null);
contentPane.add(jScrollPane1, null);
contentPane.add(jButton1, null);
contentPane.add(jButton7, null);
contentPane.add(jButton8, null);
contentPane.add(jButton2, null);
contentPane.add(jButton3, null);
contentPane.add(jButton4, null);
contentPane.add(jButton5, null);
contentPane.add(jButton6, null);
contentPane.add(jTextField2, null);
contentPane.add(jTextField3, null);
contentPane.add(jTextField4, null);
contentPane.add(jTextField1, null);
contentPane.add(jLabel5, null);
contentPane.add(jLabel2, null);
contentPane.add(jLabel3, null);
contentPane.add(jLabel4, null);
//取得會計科目數組和顯示結構樹
this.showAllAccountName();
//設置窗口類的字體和為按鈕加入動作接收器
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);
//清空數組的內容
accountName = new String[0][3];
//清空集合內容
accountNameVector.clear();
//取得面板上的所有控件
Component[] components = contentPane.getComponents();
//創建臨時編輯框控件
JTextField tmpTextField = new JTextField();
for(int i = 0; i < components.length; i++){
components[i].setFont(dialog13);
if(components[i].getClass().getName().equals("javax.swing.JTextField")){
tmpTextField = (JTextField)components[i];
//清空編輯框的內容
tmpTextField.setText("");
}
}
}
//取得會計科目數組和顯示結構樹的方法
public void showAllAccountName(){
accountName = stockManagementData.getAllAccountName();
//清空集合的內容
accountNameVector.clear();
for(int i = 0; i < accountName.length; i++){
accountNameVector.addElement(accountName[i][0]);
}
//清空結構樹的內容
root.removeAllChildren();
//更新結構樹的顯示
treeModel.reload();
//顯示結構樹的內容
this.showAccountNameTree(0, root);
}
//通過遞歸算法顯示會計科目結構樹的方法
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]);
//為根節點加入樹節點
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 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;
}
}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[] accountNameArray = new String[3];
accountNameArray[0] = jTextField2.getText().trim();
accountNameArray[1] = jTextField3.getText().trim();
accountNameArray[2] = jTextField4.getText().trim();
if(accountNameArray[0].length() == 0){
JOptionPane.showMessageDialog(null, "會計科目名字不允許空值.");
return;
}
if(action.equals("create")){
//創建會計科目
int result = 0;
//int result = stockManagementData.createAccountName(accountNameArray);
if(result == 1){
//為結構樹加入會計科目
//listData1.addElement(accountNameArray[0]);
//更新會計科目數組
String[][] tempStrs = new String[accountName.length + 1][3];
System.arraycopy(accountName, 0, tempStrs, 0, accountName.length);
for(int i = 0; i < 3; i++){
tempStrs[accountName.length][i] = accountNameArray[i];
}
accountName = tempStrs;
//jList1.setSelectedIndex(listData1.size() -1);
}else{
JOptionPane.showMessageDialog(null, "會計科目創建失敗,請檢查該會計科目是否存在和值是否超出字段長度.");
}
}else if (action.equals("update")){
//更新會計科目
int result = 0;
//int result = stockManagementData.updateAccountName(accountNameArray);
if(result == 1){
int selectedIndex = 0;
// int selectedIndex = jList1.getSelectedIndex();
//更新會計科目數組
for(int i = 0; i < 3; i++){
accountName[selectedIndex][i] = accountNameArray[i];
}
}else{
JOptionPane.showMessageDialog(null, "會計科目更新失敗.");
}
}else if (action.equals("delete")){
//刪除會計科目
int result = 0;
//int result = stockManagementData.deleteAccountName(accountNameArray[0]);
if(result == 1){
int selectedIndex = 0;
//int selectedIndex = jList1.getSelectedIndex();
//刪除結構樹的數據
//listData1.removeElementAt(selectedIndex);
//更改數組的數據
String[][] tempStrs = new String[accountName.length -1][3];
int line = 0;
for(int i = 0; i < accountName.length; i++){
if(i == selectedIndex){
continue;
}else{
for(int j = 0; j < 3; j++){
tempStrs[line][j] = accountName[i][j];
}
line++;
}
}
accountName = tempStrs;
//清空編輯框的值
this.clearAccountName();
}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 + -