📄 criterionsetpanel.java
字号:
JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
delItemButton.setText("删除项目");
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, "现在已经没有任何项目信息!",
"友情提示", JOptionPane.INFORMATION_MESSAGE);
return;
} else {
updateItemMoney(leftSelectedRow, rightSelectedRow);
}
} else {
JOptionPane.showMessageDialog(null, "请先保存账套: "
+ 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, "当前没有需要保存的账套!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
return;
} else {
leftTable.setRowSelectionInterval(needSaveRow, needSaveRow);
int rowCount = rightTable.getRowCount();
if (rowCount == 0) {
JOptionPane.showMessageDialog(null, "请为账套“"
+ leftTable.getValueAt(needSaveRow, 1)
+ "”添加项目!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
addItem(needSaveRow);
return;
} else {
for (int i = 0; i < rowCount; i++) {
if (rightTable.getValueAt(i, 4).equals("0")) {
String info = "请为账套“"
+ leftTable.getValueAt(needSaveRow, 1)
+ "”中的项目“"
+ 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, "已经成功保存账套: "
+ 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("序号");
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("序号");
rightTableColumnV.add("项目名称");
rightTableColumnV.add("项目单位");
rightTableColumnV.add("项目类型");
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);// 默认选中第一行(从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);// 弹出添加项目对话框
//
JTable itemTable = addAccountItemDialog.getTable();// 获得对话框中的表格对象
int[] selectedRows = itemTable.getSelectedRows();// 获得选中行的索引
if (selectedRows.length > 0) {// 有新添加的项目
needSaveRow = leftSelectedRow;// 设置当前账套为需要保存的账套
int defaultSelectedRow = rightTable.getRowCount();// 将选中行设置为新添加项目的第一行
TbReckoning reckoning = reckoningV.get(leftSelectedRow);// 获得选中账套的对象
for (int i = 0; i < selectedRows.length; i++) {// 通过循环向账套中添加项目
String name = itemTable.getValueAt(selectedRows[i], 1)
.toString();// 获得项目名称
String unit = itemTable.getValueAt(selectedRows[i], 2)
.toString();// 获得项目单位
Iterator<TbReckoningInfo> reckoningInfoIt = reckoning
.getTbReckoningInfos().iterator();// 遍历账套中的现有项目
boolean had = false;// 默认在现有项目中不包含新添加的项目
while (reckoningInfoIt.hasNext()) {// 通过循环查找是否存在
TbAccountItem accountItem = reckoningInfoIt.next()
.getTbAccountItem();// 获得已有的项目对象
if (accountItem.getName().equals(name)
&& accountItem.getUnit().equals(unit)) {
had = true;// 存在
break;// 跳出循环
}
}
if (!had) {// 如果没有则添加
TbReckoningInfo reckoningInfo = new TbReckoningInfo();// 创建账套信息对象
TbAccountItem accountItem = (TbAccountItem) dao
.queryAccountItemByNameUnit(name, unit);// 获得账套项目对象
accountItem.getTbReckoningInfos().add(reckoningInfo);// 建立从账套项目对象到账套信息对象的关联
reckoningInfo.setTbAccountItem(accountItem);// 建立从账套信息对象到账套项目对象的关联
reckoningInfo.setMoney(0);// 设置项目金额为0
reckoningInfo.setTbReckoning(reckoning);// 建立从账套信息对象到账套对象的关联
reckoning.getTbReckoningInfos().add(reckoningInfo);// 建立从账套对象到账套信息对象的关联
}
}
refreshItemAllRowValueV(leftSelectedRow);// 同步刷新右侧的账套项目表格
rightTable.setRowSelectionInterval(defaultSelectedRow,
defaultSelectedRow);// 设置新添加项目的第一行为选中行
addAccountItemDialog.dispose();// 销毁添加项目对话框
}
}
public void updateItemMoney(int leftSelectedRow, int rightSelectedRow) {
String money = null;
done: while (true) {
money = JOptionPane.showInputDialog(null, "请填写"
+ rightTable.getValueAt(rightSelectedRow, 1) + "的"
+ rightTable.getValueAt(rightSelectedRow, 3).toString().trim() + "金额:",
"修改金额", JOptionPane.INFORMATION_MESSAGE);
if (money == null) {// 用户单击“取消”按钮
break done;// 取消修改
} else {// 用户单击“确定”按钮
if (money.equals("")) {// 未输入金额,弹出提示对话框
JOptionPane.showMessageDialog(null, "请输入金额!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
} else {// 输入了金额
Pattern pattern = Pattern.compile("[1-9][0-9]{0,5}");// 金额必须在1——999999之间
Matcher matcher = pattern.matcher(money);// 通过正则表达式判断是否符合要求
if (matcher.matches()) {// 符合要求
needSaveRow = leftSelectedRow;// 设置当前账套为需要保存的账套
rightTable.setValueAt(money, rightSelectedRow, 4);// 修改项目金额
int nextSelectedRow = rightSelectedRow + 1;// 默认存在下一行
if (nextSelectedRow < rightTable.getRowCount()) {// 存在下一行
rightTable.setRowSelectionInterval(nextSelectedRow,
nextSelectedRow);
}
//
String name = rightTable
.getValueAt(rightSelectedRow, 1).toString();// 获得项目名称
String unit = rightTable
.getValueAt(rightSelectedRow, 2).toString();// 获得项目单位
TbReckoning reckoning = reckoningV.get(leftSelectedRow);// 获得选中账套的对象
Iterator reckoningInfoIt = reckoning
.getTbReckoningInfos().iterator();// 遍历项目
while (reckoningInfoIt.hasNext()) {// 通过循环查找选中项目
TbReckoningInfo reckoningInfo = (TbReckoningInfo) reckoningInfoIt
.next();
TbAccountItem accountItem = reckoningInfo
.getTbAccountItem();
if (accountItem.getName().equals(name)
&& accountItem.getUnit().equals(unit)) {
reckoningInfo.setMoney(new Integer(money));// 修改金额
break;// 跳出循环
}
}
break done;// 修改完成
} else {// 不符合要求,弹出提示对话框
String infos[] = { "金额输入错误,请重新输入!",
"金额必须为0——999999之间的整数!" };
JOptionPane.showMessageDialog(null, infos, "友情提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -