gpt4 book ai didi

java - 将数据添加到 'Amount' 和 'Price' 列单元格时更新 JTable 中的 'Quantity' 列单元格

转载 作者:行者123 更新时间:2023-12-01 16:37:06 24 4
gpt4 key购买 nike

我创建了包含五列的 JTable。名称、单位、价格、数量和金额是列名称。使用 JText 字段将数据填充到表中。单击“添加”按钮时它可以正常工作。我编写了一个代码,使用“价格”列单元格值乘以“数量”列单元格值来获取“金额”。程序运行但计算未发生。帮助我解决这个问题非常感谢。提前致谢。

View-01

公共(public)类 UnitTable2 扩展 JFrame 实现 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。当您将 tableChanged 模型值提取修改为:

时,它将开始工作
double price = Double.parseDouble(model.getValueAt(row, 2).toString());
double quantity = Double.parseDouble(model.getValueAt(row, 3).toString());

但作为一个目标,这可能应该在单独的方法中完成,如果值为空或包含除 double 值以外的其他内容,则进行验证。

关于java - 将数据添加到 'Amount' 和 'Price' 列单元格时更新 JTable 中的 'Quantity' 列单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61932271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com