저는 Till 시스템을 가지고 있으며 GUI 측면의 항목 테이블 (JTable)에 항목을 추가하려고합니다. 또한 각 JButton을 클릭 할 때 순서대로 실행하고 싶습니다. 내 문제는 각 JButton에 대해 충분히 청취자 메서드를 동적으로 만들 수 없으므로 항목이 순서에 따라 추가되거나 삭제되는지 여부에 따라 업데이트됩니다. 그것은 JTextField에서 누를 때마다 각 항목을 합계하고 있지만 올바른 소수 자릿수 2로 반올림하지 않습니다. DecimalFormat을 사용해 보았는데 제대로 작동하지 않았습니다. 여기 내 수신기 방법 중 하나입니다. 이 메서드가 제대로 작동하고 동적으로 만들 수 있다면 나머지 리스너 메서드에도 동일한 원리를 적용 할 수 있습니다. 누구든지 올바른 방향으로 나를 가리킬 수 있다면 매우 감사 할 것입니다. 감사합니다.어떻게하면 actionListener 메서드를 동적으로 만들 수 있습니까?
나는 순서를 편집하는 경우 있도록도 충분히 동적 갖고 싶어// Small Americano ActionListener
americanoSmall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String query = "select ProductName, Price from product where ProductID = 24";
java.sql.PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
// Loop through the ResultSet and transfer in the Model
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
int colNo = rsmd.getColumnCount();
while (rs.next()) {
Object[] objects = new Object[colNo];
for (int i = 0; i < colNo; i++) {
objects[i] = rs.getObject(i + 1);
}
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(objects);
Stock s = new Stock();
total = total + s.getItemPrice(24);
totalTF.setText(String.valueOf(total));
totalTF.setText(String.format("%.2f", total));
}
TableModel model = (DefaultTableModel) table.getModel();
table.setModel(model);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
이것을 고려하면 총계는'TableModel'의 정보를 반영하기 때문에 데이터베이스를 업데이트 할 때 집계를 업데이트하는 대신'TableModel'이 변경 될 때 집계를 업데이트하십시오 - 이것은 의무를 수행하고 데이터베이스에서 별도로 업데이트를 분리하여 더욱 유연하고 쉽게 테스트 할 수 있습니다. – MadProgrammer
* "DecimalFormat을 사용해 보았지만 제대로 작동하지 않았습니다."* - 어떻게 작동하지 않았습니까? 투입물은 무엇이었으며, 산출물은 무엇이었으며 예상 된 결과와 어떻게 다른가? 어떻게 사용 했습니까? 돈을 다룰 때'BigDecimal'이나'long'을 사용하는 편이 낫지 만, 그럴 가치가있는 것은 – MadProgrammer
입니다. DecimalFormat df = new DecimalFormat ("#. 00"); totalTF.setText (Double.toString (df.format (total))); – Movillian