有没有人知道为什么btnAdd.addActionListener()被调用两次,每当我只需要它一次时,说从TradeOutDialog返回对话框,刷新()被调用两次,有时我从TradeOutDialog点击取消,btnAdd.addActionListener()也被要求了。 不知道为什么。我希望它会发生一次。 感谢
public class TradeOut extends JPanel {
private JTable tblTradeOut = new JTable();
private JPanel residualPanel = new JPanel();
private JPanel mainPanel = new JPanel();
private JPanel p = new JPanel();
private JPanel btnPanel = new JPanel();
private JButton btnAdd = new JButton("新增");
//private JTextField residual;
private FormattedCurrencyTextField residual;
private BigDecimal residualValue;
private TradeIn ti;
private JLabel lblTotal = new JLabel("差价");
private TradeOutDialog dialog;
private List<TradeOutProduct> ltop = new ArrayList<>();
private Database db;
private BigDecimal total ;
DefaultTableModel model = new DefaultTableModel() {
String[] ColNames = new String[] {
//"追加品类","大类名称","数量","","追加价","销售员"
"SKU", "品类", "数量", "售价", "重量", "单位价", "总追加价"
};
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 3 || columnIndex == 4)
return java.lang.Integer.class;
return super.getColumnClass(columnIndex); //To change body of generated methods, choose Tools | Templates.
}
@Override
public int getColumnCount() {
return ColNames.length;
}
@Override
public String getColumnName(int column) {
return ColNames[column]; //To change body of generated methods, choose Tools | Templates.
}
};
public TradeOut(Database _db, TradeIn _ti, BigDecimal value) throws SQLException, ParseException
{
this.db = _db;
this.ti = _ti;
this.setLayout(new MigLayout("insets 0 0 0 0"));
mainPanel.setLayout(new MigLayout("insets 0 0 0 0"));
btnPanel.setLayout(new MigLayout("insets 0 0 0 0"));
btnPanel.add(btnAdd);
residualPanel.setLayout(new MigLayout("insets 0 0 0 0"));
MaskFormatter mask = new MaskFormatter("¥###########.##");
residual = new FormattedCurrencyTextField(mask);
total = new BigDecimal(0);
residual.setText(String.valueOf(value));
residualValue = value;
residualPanel.add(lblTotal);
residualPanel.add(residual, "width 20%");
residual.setEditable(false);
dialog = new TradeOutDialog(db, ti);
btnAdd.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
addTradeOutBtnActionPerformed(evt);
}
private void addTradeOutBtnActionPerformed(ActionEvent evt) {
if (evt.getSource() == btnAdd) {
dialog.setVisible(true);
refresh();
}
}
});
dialog.getAdd().addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
addItemBtnActionPerformed(evt);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "系统错误信息,请联络技术支援");
Logger.getLogger(TradeOut.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void addItemBtnActionPerformed(ActionEvent evt) throws SQLException {
TradeOutProduct top = new TradeOutProduct();
top.setProductID(String.valueOf(String.valueOf(dialog.getCategory()) + String.valueOf(dialog.getSubCategory()) + dialog.getProduct().trim()));
top.setSKUName(dialog.getSKUName().trim());
top.setCategoryID(dialog.getCategory());
top.setSubCategoryID(dialog.getSubCategory());
top.setQuantity(dialog.getTxtQuantity().intValue());
top.setWeight(dialog.getTxtWeight());
top.setUnitPrice(dialog.getTxtUnitPrice());
top.setTradeOutPrice(dialog.getTxtTradeOutPrice());
//top.setEmpID(dialog.getCbEmployeeID());
ltop.add(top);
dialog.setVisible(false);
}
});
p.setLayout(new MigLayout("insets 0 0 0 0"));
tblTradeOut.setModel(model);
tblTradeOut.setAutoCreateRowSorter(true);
TableColumnModel m = tblTradeOut.getColumnModel();
m.getColumn(3).setCellRenderer(NumberRenderer.getCurrencyRenderer());
m.getColumn(5).setCellRenderer(NumberRenderer.getCurrencyRenderer());
m.getColumn(6).setCellRenderer(NumberRenderer.getCurrencyRenderer());
mainPanel.add(btnPanel, "height 10%, width 100%, wrap");
mainPanel.add(new JScrollPane(tblTradeOut), "height 80%, width 100%, wrap");
mainPanel.add(residualPanel, "height 10%, width 100%");
this.add(mainPanel, "height 100%, width 100%");
}
public void refresh()
{
removeAllRows();
int x = 0;
BigDecimal atotal = BigDecimal.ZERO;
for (TradeOutProduct top : ltop)
{
BigDecimal TradeOutPrice = top.getTradeOutPrice();
int Quantity = top.getQuantity();
int Weight = top.getWeight();
BigDecimal UnitPrice = top.getUnitPrice();
// for non-golden products
if (Weight == 0 && UnitPrice.equals(BigDecimal.ZERO))
{
top.setTotal(TradeOutPrice.multiply(new BigDecimal(Quantity)));
}
else if (Quantity == 0 && TradeOutPrice.equals(BigDecimal.ZERO))
{
top.setTotal(UnitPrice.multiply(new BigDecimal(Weight) ));
}
model.addRow(new Object[]{
top.getProductID(),top.getSKUName(),Quantity, TradeOutPrice, Weight, UnitPrice, top.getTotal() });
/*((DefaultTableModel) tblTradeIn.getModel()).addRow(new java.util.Vector<String>(java.util.Arrays.asList(new String[]{getDate(), getTime(),tip.getProductID(),tip.getCategoryID(),String.valueOf(tip.getQuantity()),tip.getTradeInPrice(),tip.getTradeInPrice()*tip.getQuantity() }))); */
total = atotal.add(top.getTotal());
residualValue = residualValue.subtract(top.getTotal());
// the customer still has value to remain, retailer always want to sell product
if (residualValue.compareTo(BigDecimal.ZERO) >= 0){
residual.setText(String.valueOf(residualValue));
}
else
{
// the customer tradein price is too high, not acceptable
//residual.setText("須要補價 - " + String.valueOf(Math.abs(residualValue)));
residual.setMoney(residualValue.abs());
}
}
}
private void removeAllRows()
{
int rows = tblTradeOut.getRowCount();
for (int i = rows-1; i >= 0; i--) {
((DefaultTableModel)tblTradeOut.getModel()).removeRow(i);
tblTradeOut.revalidate();
}
}
public BigDecimal getTotal()
{
return total;
}
public JTextField getTotalTf()
{
return residual;
}
public String getProduct(int row)
{
return tblTradeOut.getValueAt(row, 0).toString();
}
public int getQuantity(int row)
{
return Integer.parseInt(tblTradeOut.getValueAt(row,2).toString());
}
public int getTradeOutPrice(int row)
{
return Integer.parseInt(tblTradeOut.getValueAt(row, 4).toString());
}
public String getEmp(int row)
{
return tblTradeOut.getValueAt(row, 5).toString();
}
public JTable getTable()
{
return tblTradeOut;
}
}