我创建了一个有五列的JTable,其中名称、单位、价格、数量&;金额是列名。Name, Unit, Price, Quantity & Amount是列名,使用JText Fields将数据填入表中。点击 "ADD "按钮后工作正常,我写了一段代码,用 "Price "列的单元格值乘以 "Quantity "列的单元格值得到 "Amount"。程序运行,但计算没有发生。帮助我解决这个问题是非常感激。先谢谢你了。
public class UnitTable2 extends JFrame implements TableModelListener {
private JFrame frame;
private JTable table;
private JTextField txtName;
private JTextField txtUImp;
private JTextField txtPImp;
private JTextField txtUMetric;
private JTextField txtPMetric;
private JTextField txtQty;
private JComboBox<String> cmbUType;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UnitTable2 window = new UnitTable2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public UnitTable2() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 649, 288);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 526, 181);
frame.getContentPane().add(scrollPane);
table = new JTable();
Object[] columns = { "Name", "Unit", "Price", "Quantity", "Amount" };
DefaultTableModel model = new DefaultTableModel();
scrollPane.setViewportView(table);
model.setColumnIdentifiers(columns);
table.setModel(model);
model.addTableModelListener(this);
JLabel lblName = new JLabel("Name");
lblName.setBounds(10, 201, 96, 13);
frame.getContentPane().add(lblName);
txtName = new JTextField();
txtName.setBounds(10, 224, 96, 19);
frame.getContentPane().add(txtName);
txtName.setColumns(10);
JLabel lblUImp = new JLabel("Unit Imperial");
lblUImp.setBounds(121, 201, 91, 13);
frame.getContentPane().add(lblUImp);
txtUImp = new JTextField();
txtUImp.setBounds(116, 224, 96, 19);
frame.getContentPane().add(txtUImp);
txtUImp.setColumns(10);
JLabel lblPImp = new JLabel("Price Imperial");
lblPImp.setBounds(222, 201, 96, 13);
frame.getContentPane().add(lblPImp);
txtPImp = new JTextField();
txtPImp.setBounds(222, 224, 96, 19);
frame.getContentPane().add(txtPImp);
txtPImp.setColumns(10);
JLabel lblUMetric = new JLabel("Unit Metric");
lblUMetric.setBounds(330, 201, 94, 13);
frame.getContentPane().add(lblUMetric);
txtUMetric = new JTextField();
txtUMetric.setBounds(330, 224, 96, 19);
frame.getContentPane().add(txtUMetric);
txtUMetric.setColumns(10);
JLabel lblPMetric = new JLabel("Price Metric");
lblPMetric.setBounds(434, 201, 102, 13);
frame.getContentPane().add(lblPMetric);
txtPMetric = new JTextField();
txtPMetric.setBounds(434, 224, 96, 19);
frame.getContentPane().add(txtPMetric);
txtPMetric.setColumns(10);
JLabel lblQty = new JLabel("Quantity");
lblQty.setBounds(540, 201, 120, 13);
frame.getContentPane().add(lblQty);
txtQty = new JTextField();
txtQty.setBounds(540, 224, 96, 19);
frame.getContentPane().add(txtQty);
txtQty.setColumns(10);
JButton btnAdd = new JButton("ADD");
Object[] row = new Object[4];
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String type = (String) cmbUType.getSelectedItem();
if (type.equals("Imperial")) {
row[0] = txtName.getText();
row[1] = txtUImp.getText();
row[2] = Double.parseDouble(txtPImp.getText());
row[3] = Double.parseDouble(txtQty.getText());
} else {
row[0] = txtName.getText();
row[1] = txtUMetric.getText();
row[2] = Double.parseDouble(txtPMetric.getText());
row[3] = Double.parseDouble(txtQty.getText());
}
model.addRow(row);
}
});
btnAdd.setBounds(546, 45, 85, 21);
frame.getContentPane().add(btnAdd);
cmbUType = new JComboBox<>();
cmbUType.setModel(new DefaultComboBoxModel<String>(new String[] { "Imperial", "Metric" }));
cmbUType.setBounds(546, 8, 85, 21);
frame.getContentPane().add(cmbUType);
JButton btnDelete = new JButton("DELETE");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = table.getSelectedRow();
if (i >= 0) {
model.removeRow(i);
} else {
JOptionPane.showMessageDialog(null, "Please Select Item to Delete");
}
}
});
btnDelete.setBounds(546, 76, 85, 21);
frame.getContentPane().add(btnDelete);
}
// price multiply with quantity and set to amount
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE) {
int row = e.getFirstRow();
int column = e.getColumn();
if (column == 2 || column == 3) {
TableModel model = table.getModel();
double price = ((Double) model.getValueAt(row, 2)).doubleValue();
double quantity = ((Double) model.getValueAt(row, 3)).doubleValue();
Double value = (price * quantity);
model.setValueAt(value, row, 4);
}
}
}
}
事实上,计算 是否 发生。你看不到更新,因为会抛出一个异常--因为你正试图从TableModel中获取一个Double,而这个Double包含了Strings。当你修改你的tableChanged模型值提取为时,它将开始工作。
double price = Double.parseDouble(model.getValueAt(row, 2).toString());
double quantity = Double.parseDouble(model.getValueAt(row, 3).toString());
但作为一个目标,这可能应该在一个单独的方法中完成,如果值是空的,或者包含双倍值以外的其他内容,则需要验证。