类别
类别
基本上,我想创建一个GUI,允许用户从给定列表中选择一个项目,然后显示添加项目的总额,即价格。所以,它将是项目的数量*价格。数量将由用户输入。到目前为止,这就是代码,我想修改代码,让用户输入数量,并在GUI中进行整体计算和显示。
我如何添加用户将在GUI中指定项目的数量?
类别Item
public class Item {
private String itemName;
private Integer itemPrice;
public Item(){}
public Item(String itemName, int itemPrice){
this.itemName = itemName;
this.itemPrice = itemPrice;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Integer getItemPrice() {
return itemPrice;
}
public void setItemPrice(Integer itemPrice) {
this.itemPrice = itemPrice;
}
@Override
public String toString(){
return this.itemName;
}
}
类别ShoppingCart
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.swing.*;
public class ShoppingCart extends JFrame {
private JList leftlist;
private JList rightlist;
private JButton addbutton;
private JButton totalbutton;
private JLabel totalLabel;
public ShoppingCart() {
super("Shopping Cart");
setLayout(new FlowLayout());
// preparing item list it might be coming from somewhere else
List<Item> itemList = new ArrayList<Item>();
itemList.add(new Item("Apple", 10));
itemList.add(new Item("Carrot", 40));
itemList.add(new Item("Cucumber", 50));
itemList.add(new Item("Iphone 6", 6000));
itemList.add(new Item("Galaxy S6", 1100));
itemList.add(new Item("BlackBerry", 1300));
itemList.add(new Item("HairDryer", 200));
itemList.add(new Item("Ironer", 300));
itemList.add(new Item("Vacuum Cleaner", 400));
leftlist = new JList(itemList.toArray());
leftlist.setVisibleRowCount(5);
leftlist
.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(leftlist));
addbutton = new JButton("ADD");
addbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//make sure you preserve the previously selected list items
int size = rightlist.getModel().getSize();
Set objects = new LinkedHashSet();
for (int i = 0; i < size; i++) {
objects.add(rightlist.getModel().getElementAt(i));
}
objects.addAll(Arrays.asList(leftlist.getSelectedValues()));
rightlist.setListData(objects.toArray());
}
});
add(addbutton);
rightlist = new JList();
rightlist.setVisibleRowCount(5);
rightlist.setFixedCellWidth(55);
rightlist.setFixedCellHeight(20);
add(new JScrollPane(rightlist));
totalbutton = new JButton("Total");
totalbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// iterate over item objects and calculate the total
int size = rightlist.getModel().getSize();
Integer totalAmount = 0;
for (int i = 0; i < size; i++) {
Object obj = leftlist.getModel().getElementAt(i);
Item item = (Item) obj;
totalAmount += item.getItemPrice();
}
//update the total Amount label
totalLabel.setText("Total Amount : " + totalAmount);
}
});
add(totalbutton);
totalLabel = new JLabel("Total Amount : ");
add(totalLabel);
}
public static void main(String[] args) {
ShoppingCart list = new ShoppingCart();
list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
list.setSize(300, 300);
list.setVisible(true);
}
}
这基本上是一个库存场景。。首先你应该提示用户从列表中选择一个项目,然后在焦点向外的价格字段应该根据产品名称自动获得文本。。用户将输入数量。。然后使用以下代码计算总价。。。
private void total_buttonActionPerformed(java.awt.event.ActionEvent evt) {
float price=Float.parseFloat(price_txt.getText());
float quantity=Float.parseFloat(quantity_txt.getText());
float sum=price*quantity;
total_txt.setText(""+sum);
}
//注意,对于库存,您还应该检查产品的可用性。。
private void quantity_txtFocusLost(java.awt.event.FocusEvent evt) {
int quantity=Integer.parseInt(quantity_txt.getText());
int available=Integer.parseInt(availability_txt.getText());
if(quantity>available)
{
JOptionPane.showMessageDialog(rootPane, "Entered quantity is unavailable.");
quantity_txt.requestFocus();
}
}