这是我的问题。我必须创建一个具有3列(饮料,价格,数量)的Jtable
,然后,我必须创建一个按钮“ add”和一个“ total”按钮,并带有JOptionPane
作为结果总计。
这是一个视觉效果:
enter image description here
我希望创建一个包含价格*数量的“总计”按钮(带有JOptionPane)。但是,我不知道该怎么办。
这是我的代码。
J框架
private void jButtonAjoutActionPerformed(java.awt.event.ActionEvent evt) {
String tmp = JOptionPane.showInputDialog(this, "Enter your drink please : ", "Title", JOptionPane.QUESTION_MESSAGE);
float p = Float.parseFloat(JOptionPane.showInputDialog(this, "Enter your price please :", "Title", JOptionPane.QUESTION_MESSAGE));
int quant = Integer.parseInt(JOptionPane.showInputDialog(this, "Enter your quantity please :", "Title", JOptionPane.QUESTION_MESSAGE));
if (tmp != null)
{
myModel.addElement(new Drink(tmp, p, quant));
} //(tmp != null)
}
private void jButtonSumActionPerformed(java.awt.event.ActionEvent evt) {
// Button total
float result = 0;
for(int y = 0; y < jTable1.getRowCount(); y++)
{
result = Float.parseFloat(jTable1.getValueAt(y, 1).toString());
}
}
TableModelDrink
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public class TableModelDrinks extends AbstractTableModel {
private ArrayList<Drink> myList = new ArrayList <>();
@Override
public int getRowCount() {
return myList.size();
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public Object getValueAt(int i, int j) {
switch (j)
{
case 0: return myList.get(i).getName();
case 1: return myList.get(i).getPrice();
case 2: return myList.get(i).getQuantity();
default: return null;
}
}
@Override
public String getColumnName(int j)
{
switch (j)
{
case 0: return "Drink";
case 1: return "Price";
case 2: return "Quantity";
default: return null;
}
}
public void addElement(Drink elt)
{
myList.add(elt);
this.fireTableDataChanged();
}
public void setValueAt(Object value, int rowIndex, int colIndex) {
switch (colIndex) {
case 0:
myList.get(rowIndex).setName((String) value);
break;
case 1:
myList.get(rowIndex).setPrice(Float.parseFloat((String ) value));
break;
case 2:
myList.get(rowIndex).setQuantity(Integer.parseInt((String ) value));
break;
}
fireTableRowsUpdated(rowIndex, rowIndex);
}
}
喝
public class Drink {
private String name;
private float price;
private int quantity;
public Drink(String name, float price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
谢谢你的帮助
最佳答案
这简单。只需将以下方法添加到您的TableModelDrinks
public Drink drinkAt(int row) {
return myList.get(row);
}
然后,您可以按照以下方式实现sum方法:
private void jButtonSumActionPerformed(java.awt.event.ActionEvent evt) {
TableModelDrinks model = (TableModelDrinks) jTable1.getModel();
float result = 0;
for (int i = 0; i < model.getRowCount(); i++) {
Drink d = model.drinkAt(i);
result += d.getPrice() * d.getQuantity();
}
NumberFormat format = NumberFormat.getCurrencyInstance();
JOptionPane.showMessageDialog(jTable1, "Your total price is: " + format.format(result), "Price", JOptionPane.INFORMATION_MESSAGE);
}
关于java - Swing :JTable列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50154668/