JFrame工作了一段时间然后停止工作

时间:2018-03-12 04:40:01

标签: java jframe

弹出我的JFrame窗口,我可以输入一些值并点击约3次,然后停止允许我点击并基本上只是崩溃。除非我终止程序并再次运行,否则我无法对其执行任何操作。然后问题重演了。我相当积极,我没有无限循环运行。 这是代码: 目录类:

def quickSort(lst):
   if len(lst) <= 1: 
      return lst
   smaller = [x for x in lst[1:] if x < lst[0]]
   larger = [x for x in lst[1:] if x >= lst[0]]
   lst0 = lst[0]
   del lst # lst is freed up now
   smaller = quickSort(smaller) # original smaller freed after this
   larger = quickSort(larger) # original larger freed too
   return smaller + [lst[0]] + larger

项目类:

import java.util.ArrayList;

public class Catalog {

private String name;

private ArrayList<Item> list = new ArrayList<Item>();

public Catalog(String name) {
    this.name = name;
}

public void add(Item item) {
    this.list.add(item);
}

public int size() {
    return list.size();
}

public Item get(int index) {
    return list.get(index);
}

public String getName() {
    return name;
}
}

ItemOrder类:

public class Item {

private String name;
private double price;
private int bulkQuantity;
private double bulkPrice;
public Item(String name, double price) {
    if(price<0) {
        throw new IllegalArgumentException("Price cannot be negative");
    }
    this.name = name;
    this.price = price;
}
public Item(String name, double price, int bulkQuantity, double bulkPrice) {
    if(price<0 || bulkPrice<0 || bulkQuantity<0) {
        throw new IllegalArgumentException("Price or bulkPrice or 
bulkQuantity cannot be negative");
    }
    this.name = name;
    this.bulkQuantity = bulkQuantity;
    this.price = price;
    this.bulkPrice = bulkPrice;
}

public double priceFor(int quantity) {
    if(quantity<0) {
        throw new IllegalArgumentException("Quantity cannot be negative");
    }
    if(quantity>=bulkQuantity) {
        int numberOfBulkQuantities = 0;
        for(int i = quantity-bulkQuantity; i>=0; i-=bulkQuantity) {
            numberOfBulkQuantities++;
            quantity-=bulkQuantity;
        }
        return numberOfBulkQuantities*bulkPrice + quantity*price;
    }
    return quantity*price;
}

public String toString() {
    if(bulkPrice>0) {
        return "" + name + ", $" + price + " (" + bulkQuantity + " for $" + 
bulkPrice + ")";
    }
    return "" + name + ", $" + price;
}

}

ShoppingCart 类:

public class ItemOrder {

private int quantity;
private Item item;
public ItemOrder(Item item, int quantity) {
    this.item = item;
    this.quantity = quantity;
}
public double getPrice() {
    return item.priceFor(quantity);
}
public Item getItem() {
    return item;
}
}

ShoppingFrame类:

import java.util.ArrayList;

public class ShoppingCart {

private ArrayList<ItemOrder> list;
private double discount = 1;
public ShoppingCart() {
    list = new ArrayList<ItemOrder>();
}
public void add(ItemOrder item) {
    for(int i = 0; i < list.size(); i++) {

if(list.get(i).getItem().toString().equals(item.getItem().toString())){ 
            list.remove(i);
        }
    }
    list.add(item);
}
public void setDiscount(boolean discount) {
    if(discount) {
        this.discount = 0.9;
    }
}
public double getTotal() {
    double totalPrice = 0.0;
    for(int i = 0; i<list.size(); i++) {
        totalPrice+=list.get(i).getPrice();
    }
    return totalPrice*discount;
}
}

ShoppingMain Class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class ShoppingFrame extends JFrame {
private ShoppingCart items;
private JTextField total;

public ShoppingFrame(Catalog products)      {
    // create frame and order list
    setTitle(products.getName());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    items = new ShoppingCart();

    // set up text field with order total
    total = new JTextField("$0.00", 12);
    total.setEditable(false);
    total.setEnabled(false);
    total.setDisabledTextColor(Color.BLACK);
    JPanel p = new JPanel();
    p.setBackground(Color.blue);
    JLabel l = new JLabel("order total");
    l.setForeground(Color.YELLOW);
    p.add(l);
    p.add(total);
    add(p, BorderLayout.NORTH);

    p = new JPanel(new GridLayout(products.size(), 1));
    for (int i = 0; i < products.size(); i++)
        addItem(products.get(i), p);
    add(p, BorderLayout.CENTER);

    p = new JPanel();
   add(makeCheckBoxPanel(), BorderLayout.SOUTH);

    // adjust size to just fit
    pack();
}

// Sets up the "discount" checkbox for the frame
private JPanel makeCheckBoxPanel() {
    JPanel p = new JPanel();
    p.setBackground(Color.blue);
    final JCheckBox cb = new JCheckBox("qualify for discount");
    p.add(cb);
    cb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            items.setDiscount(cb.isSelected());
            updateTotal();
        }
    });
    return p;
}

// adds a product to the panel, including a textfield for user input of
// the quantity
private void addItem(final Item product, JPanel p) {
    JPanel sub = new JPanel(new FlowLayout(FlowLayout.LEFT));
    sub.setBackground(new Color(0, 180, 0));
    final JTextField quantity = new JTextField(3);
    quantity.setHorizontalAlignment(SwingConstants.CENTER);
    quantity.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            updateItem(product, quantity);
            quantity.transferFocus();
        }
    });
    quantity.addFocusListener(new FocusAdapter() {
        public void focusLost(FocusEvent e) {
            updateItem(product, quantity);
        }
    });
    sub.add(quantity);
    JLabel l = new JLabel("" + product);
    l.setForeground(Color.white);
    sub.add(l);
    p.add(sub);
}

// When the user types a new value into one of the quantity fields,
// parse the input and update the ShoppingCart.  Display an error
// message if text is not a number or is negative.
private void updateItem(Item product, JTextField quantity) {
    int number;
    String text = quantity.getText().trim();
    try {
        number = Integer.parseInt(text);
    } catch (NumberFormatException error) {
        number = 0;
    }
    if (number <= 0 && text.length() > 0) {
        Toolkit.getDefaultToolkit().beep();
        quantity.setText("");
        number = 0;
    }
    items.add(new ItemOrder(product, number));
    updateTotal();
}

// reset the text field for order total
private void updateTotal() {
    double amount = items.getTotal();
    total.setText(NumberFormat.getCurrencyInstance().format(amount));
}
}

请帮忙。

1 个答案:

答案 0 :(得分:1)

所以,经过一些调整,你的问题就在这里......

public double priceFor(int quantity) {
    if (quantity < 0) {
        throw new IllegalArgumentException("Quantity cannot be negative");
    }
    if (quantity >= bulkQuantity) {
        int numberOfBulkQuantities = 0;
        for (int i = quantity - bulkQuantity; i >= 0; i -= bulkQuantity) {
            numberOfBulkQuantities++;
            quantity -= bulkQuantity;
        }
        return numberOfBulkQuantities * bulkPrice + quantity * price;
    }
    return quantity * price;
}

更具体地说,这里......

for (int i = quantity - bulkQuantity; i >= 0; i -= bulkQuantity) {
    numberOfBulkQuantities++;
    quantity -= bulkQuantity;
}

bulkQuantity0时,由于i -= 0i,您会陷入无限循环,因此循环将永不退出

所以,稍微改变一下......

if (quantity >= bulkQuantity && bulkQuantity > 0) {
    int numberOfBulkQuantities = 0;
    for (int i = quantity - bulkQuantity; i >= 0; i -= bulkQuantity) {
        numberOfBulkQuantities++;
        quantity -= bulkQuantity;
    }
    return numberOfBulkQuantities * bulkPrice + quantity * price;
}

将允许程序继续运行