?? criterionsetpanel.java
字號(hào):
JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
delItemButton.setText("刪除項(xiàng)目");
buttonPanel.add(delItemButton);
final JButton updateMoneyButton = new JButton();
updateMoneyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int leftSelectedRow = leftTable.getSelectedRow();// 獲得選中的賬套
if (needSaveRow == -1 || needSaveRow == leftSelectedRow) {
int rightSelectedRow = rightTable.getSelectedRow();
if (rightSelectedRow == -1) {
JOptionPane.showMessageDialog(null, "現(xiàn)在已經(jīng)沒有任何項(xiàng)目信息!",
"友情提示", JOptionPane.INFORMATION_MESSAGE);
return;
} else {
updateItemMoney(leftSelectedRow, rightSelectedRow);
}
} else {
JOptionPane.showMessageDialog(null, "請(qǐng)先保存賬套: "
+ leftTable.getValueAt(needSaveRow, 1), "友情提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
updateMoneyButton.setText("修改金額");
buttonPanel.add(updateMoneyButton);
final JLabel rightLabel = new JLabel();
rightLabel.setPreferredSize(new Dimension(20, 20));
buttonPanel.add(rightLabel);
final JButton saveButton = new JButton();
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (needSaveRow == -1) {
JOptionPane.showMessageDialog(null, "當(dāng)前沒有需要保存的賬套!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
return;
} else {
leftTable.setRowSelectionInterval(needSaveRow, needSaveRow);
int rowCount = rightTable.getRowCount();
if (rowCount == 0) {
JOptionPane.showMessageDialog(null, "請(qǐng)為賬套“"
+ leftTable.getValueAt(needSaveRow, 1)
+ "”添加項(xiàng)目!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
addItem(needSaveRow);
return;
} else {
for (int i = 0; i < rowCount; i++) {
if (rightTable.getValueAt(i, 4).equals("0")) {
String info = "請(qǐng)為賬套“"
+ leftTable.getValueAt(needSaveRow, 1)
+ "”中的項(xiàng)目“"
+ rightTable.getValueAt(i, 1) + "”填寫“"
+ rightTable.getValueAt(i, 3) + "”金額!";
JOptionPane
.showMessageDialog(null, info, "友情提示",
JOptionPane.INFORMATION_MESSAGE);
rightTable.setRowSelectionInterval(i, i);
updateItemMoney(needSaveRow, i);
return;
}
}
}
dao.saveOrUpdateObject(reckoningV.get(needSaveRow));
HibernateSessionFactory.closeSession();
//
JOptionPane.showMessageDialog(null, "已經(jīng)成功保存賬套: "
+ leftTable.getValueAt(needSaveRow, 1) + "!",
"友情提示", JOptionPane.INFORMATION_MESSAGE);
needSaveRow = -1;
}
}
});
saveButton.setText("保存");
buttonPanel.add(saveButton);
final JSplitPane splitPane = new JSplitPane();
splitPane.setDividerLocation(300);
add(splitPane);
final JScrollPane leftScrollPane = new JScrollPane();
splitPane.setLeftComponent(leftScrollPane);
leftScrollPane.setPreferredSize(new Dimension(240, 0));
leftTableColumnV.add("序號(hào)");
leftTableColumnV.add("賬套名稱");
leftTable = new MTable(leftTableModel);
leftTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int selectedRow = leftTable.getSelectedRow();
if (selectedRow != lastSelectedRow) {
lastSelectedRow = selectedRow;
refreshItemAllRowValueV(selectedRow);
}
}
});
leftTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
leftScrollPane.setViewportView(leftTable);
final JScrollPane rightScrollPane = new JScrollPane();
splitPane.setRightComponent(rightScrollPane);
rightTableColumnV.add("序號(hào)");
rightTableColumnV.add("項(xiàng)目名稱");
rightTableColumnV.add("項(xiàng)目單位");
rightTableColumnV.add("項(xiàng)目類型");
rightTableColumnV.add("金額");
rightTable = new MTable(rightTableModel);
rightTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
rightScrollPane.setViewportView(rightTable);
final JPanel explainPanel = new JPanel();
add(explainPanel, BorderLayout.SOUTH);
explainPanel.setLayout(new GridBagLayout());
explainPanel.setBorder(new TitledBorder(null, "",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null));
explainPanel.setBackground(Color.WHITE);
final JLabel explainLabel = new JLabel();
explainLabel.setText("賬套說明:");
final GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.insets = new Insets(0, 0, 60, 0);
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
explainPanel.add(explainLabel, gridBagConstraints);
final JScrollPane explainScrollPane = new JScrollPane();
explainScrollPane.setPreferredSize(new Dimension(766, 80));
final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
gridBagConstraints_1.gridx = 1;
gridBagConstraints_1.gridy = 0;
explainPanel.add(explainScrollPane, gridBagConstraints_1);
textArea = new JTextArea();
textArea.setText(reckoningExplain);
textArea.setEditable(false);
textArea.setLineWrap(true);
explainScrollPane.setViewportView(textArea);
//
Iterator reckoningIt = dao.queryReckoning().iterator();
int reckoningNum = 1;
while (reckoningIt.hasNext()) {
TbReckoning reckoning = (TbReckoning) reckoningIt.next();
reckoningV.add(reckoning);
Vector<String> rowValueV = new Vector<String>();
rowValueV.add(reckoningNum++ + "");
rowValueV.add(reckoning.getName());
leftTableValueV.add(rowValueV);
}
leftTableModel.setDataVector(leftTableValueV, leftTableColumnV);
if (leftTable.getRowCount() > 0)
leftTable.setRowSelectionInterval(0, 0);// 默認(rèn)選中第一行(從a行到b行)
refreshItemAllRowValueV(0);
HibernateSessionFactory.closeSession();
}
public void refreshItemAllRowValueV(int row) {
rightTableValueV.removeAllElements();
if (reckoningV.size() > 0) {
TbReckoning reckoning = reckoningV.get(row);
textArea.setText(reckoning.getExplain());
Iterator<TbReckoningInfo> reckoningInfoIt = reckoning
.getTbReckoningInfos().iterator();
int reckoningInfoNum = 1;
while (reckoningInfoIt.hasNext()) {
TbReckoningInfo reckoningInfo = reckoningInfoIt.next();
Vector<String> reckoningInfoV = new Vector<String>();
reckoningInfoV.add(reckoningInfoNum++ + "");
reckoningInfoV.add(reckoningInfo.getTbAccountItem().getName());
reckoningInfoV.add(reckoningInfo.getTbAccountItem().getUnit());
reckoningInfoV.add(reckoningInfo.getTbAccountItem().getType());
reckoningInfoV.add(reckoningInfo.getMoney().toString());
rightTableValueV.add(reckoningInfoV);
}
} else {
textArea.setText("");
}
rightTableModel.setDataVector(rightTableValueV, rightTableColumnV);
if (rightTable.getRowCount() > 0)
rightTable.setRowSelectionInterval(0, 0);
}
public void addItem(int leftSelectedRow) {
AddAccountItemDialog addAccountItemDialog = new AddAccountItemDialog();
addAccountItemDialog.setBounds((width - 500) / 2, (height - 375) / 2,
500, 375);
addAccountItemDialog.setVisible(true);// 彈出添加項(xiàng)目對(duì)話框
//
JTable itemTable = addAccountItemDialog.getTable();// 獲得對(duì)話框中的表格對(duì)象
int[] selectedRows = itemTable.getSelectedRows();// 獲得選中行的索引
if (selectedRows.length > 0) {// 有新添加的項(xiàng)目
needSaveRow = leftSelectedRow;// 設(shè)置當(dāng)前賬套為需要保存的賬套
int defaultSelectedRow = rightTable.getRowCount();// 將選中行設(shè)置為新添加項(xiàng)目的第一行
TbReckoning reckoning = reckoningV.get(leftSelectedRow);// 獲得選中賬套的對(duì)象
for (int i = 0; i < selectedRows.length; i++) {// 通過循環(huán)向賬套中添加項(xiàng)目
String name = itemTable.getValueAt(selectedRows[i], 1)
.toString();// 獲得項(xiàng)目名稱
String unit = itemTable.getValueAt(selectedRows[i], 2)
.toString();// 獲得項(xiàng)目單位
Iterator<TbReckoningInfo> reckoningInfoIt = reckoning
.getTbReckoningInfos().iterator();// 遍歷賬套中的現(xiàn)有項(xiàng)目
boolean had = false;// 默認(rèn)在現(xiàn)有項(xiàng)目中不包含新添加的項(xiàng)目
while (reckoningInfoIt.hasNext()) {// 通過循環(huán)查找是否存在
TbAccountItem accountItem = reckoningInfoIt.next()
.getTbAccountItem();// 獲得已有的項(xiàng)目對(duì)象
if (accountItem.getName().equals(name)
&& accountItem.getUnit().equals(unit)) {
had = true;// 存在
break;// 跳出循環(huán)
}
}
if (!had) {// 如果沒有則添加
TbReckoningInfo reckoningInfo = new TbReckoningInfo();// 創(chuàng)建賬套信息對(duì)象
TbAccountItem accountItem = (TbAccountItem) dao
.queryAccountItemByNameUnit(name, unit);// 獲得賬套項(xiàng)目對(duì)象
accountItem.getTbReckoningInfos().add(reckoningInfo);// 建立從賬套項(xiàng)目對(duì)象到賬套信息對(duì)象的關(guān)聯(lián)
reckoningInfo.setTbAccountItem(accountItem);// 建立從賬套信息對(duì)象到賬套項(xiàng)目對(duì)象的關(guān)聯(lián)
reckoningInfo.setMoney(0);// 設(shè)置項(xiàng)目金額為0
reckoningInfo.setTbReckoning(reckoning);// 建立從賬套信息對(duì)象到賬套對(duì)象的關(guān)聯(lián)
reckoning.getTbReckoningInfos().add(reckoningInfo);// 建立從賬套對(duì)象到賬套信息對(duì)象的關(guān)聯(lián)
}
}
refreshItemAllRowValueV(leftSelectedRow);// 同步刷新右側(cè)的賬套項(xiàng)目表格
rightTable.setRowSelectionInterval(defaultSelectedRow,
defaultSelectedRow);// 設(shè)置新添加項(xiàng)目的第一行為選中行
addAccountItemDialog.dispose();// 銷毀添加項(xiàng)目對(duì)話框
}
}
public void updateItemMoney(int leftSelectedRow, int rightSelectedRow) {
String money = null;
done: while (true) {
money = JOptionPane.showInputDialog(null, "請(qǐng)?zhí)顚?quot;
+ rightTable.getValueAt(rightSelectedRow, 1) + "的"
+ rightTable.getValueAt(rightSelectedRow, 3).toString().trim() + "金額:",
"修改金額", JOptionPane.INFORMATION_MESSAGE);
if (money == null) {// 用戶單擊“取消”按鈕
break done;// 取消修改
} else {// 用戶單擊“確定”按鈕
if (money.equals("")) {// 未輸入金額,彈出提示對(duì)話框
JOptionPane.showMessageDialog(null, "請(qǐng)輸入金額!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
} else {// 輸入了金額
Pattern pattern = Pattern.compile("[1-9][0-9]{0,5}");// 金額必須在1——999999之間
Matcher matcher = pattern.matcher(money);// 通過正則表達(dá)式判斷是否符合要求
if (matcher.matches()) {// 符合要求
needSaveRow = leftSelectedRow;// 設(shè)置當(dāng)前賬套為需要保存的賬套
rightTable.setValueAt(money, rightSelectedRow, 4);// 修改項(xiàng)目金額
int nextSelectedRow = rightSelectedRow + 1;// 默認(rèn)存在下一行
if (nextSelectedRow < rightTable.getRowCount()) {// 存在下一行
rightTable.setRowSelectionInterval(nextSelectedRow,
nextSelectedRow);
}
//
String name = rightTable
.getValueAt(rightSelectedRow, 1).toString();// 獲得項(xiàng)目名稱
String unit = rightTable
.getValueAt(rightSelectedRow, 2).toString();// 獲得項(xiàng)目單位
TbReckoning reckoning = reckoningV.get(leftSelectedRow);// 獲得選中賬套的對(duì)象
Iterator reckoningInfoIt = reckoning
.getTbReckoningInfos().iterator();// 遍歷項(xiàng)目
while (reckoningInfoIt.hasNext()) {// 通過循環(huán)查找選中項(xiàng)目
TbReckoningInfo reckoningInfo = (TbReckoningInfo) reckoningInfoIt
.next();
TbAccountItem accountItem = reckoningInfo
.getTbAccountItem();
if (accountItem.getName().equals(name)
&& accountItem.getUnit().equals(unit)) {
reckoningInfo.setMoney(new Integer(money));// 修改金額
break;// 跳出循環(huán)
}
}
break done;// 修改完成
} else {// 不符合要求,彈出提示對(duì)話框
String infos[] = { "金額輸入錯(cuò)誤,請(qǐng)重新輸入!",
"金額必須為0——999999之間的整數(shù)!" };
JOptionPane.showMessageDialog(null, infos, "友情提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -