删除按钮实际上并没有从arraylist中删除项目...我做错了什么?

时间:2014-11-09 01:20:04

标签: java swing user-interface arraylist import

我正在尝试让我的GUI中的删除按钮工作,但它不想以它需要的方式工作。它应该从列表中删除我当前选择的数组列表项,但它只是移动到下一个项目。

我做错了什么?

产品

import java.util.ArrayList;

public class product {

@SuppressWarnings("all")
ArrayList <extendedDVD> product() {
     ArrayList<extendedDVD> dvd = new ArrayList<extendedDVD>(); //New Array Constructor
     dvd.add(new extendedDVD("The Change Up", 12, 11.86, 10452));  //Index
     dvd.add(new extendedDVD("Wall-E", 15, 9.05, 10356));     //Index
     dvd.add(new extendedDVD("Flubber", 13, 7.01, 13698));      //Index
     dvd.add(new extendedDVD("Daddy Day Care", 14, 9.15, 15496));   //Index
     return dvd;
}

public static void main(String [] args) //Main Method
{
    GUI mainbox = new GUI();
    mainbox.setVisible(true);
}//end main method
}// end class Product

DVD

class DVD {

String dvdTitle; //Variable
int dvdStock;    //Variable
double dvdPrice; //Variable
int dvdItem;     //Variable

public DVD(String title, int stock, double price, int item) //Method Constructor
{
    dvdTitle = title;
    dvdStock = stock;
    dvdPrice = price;
    dvdItem = item;
} //end four-argument constructor

public DVD() //Method
{
    dvdTitle = ""; //Value Type
    dvdStock = 0;  //Value Type
    dvdPrice = 0.0; //Value Type
    dvdItem = 0;  //Value Type
} //end four-argument constructor

// set DVD name
public void setDvdTitle(String title) //Method
{
    dvdTitle = title;
} //end method  setDvdTitle

//return DVD Title
public String getDvdTitle() //Method
{
    return dvdTitle;
} //end method getDvdTitle

//set DVD Stock
public void setDvdStock(int stock) //Method
{
    dvdStock = stock;
} //end method setDvdStock

//return DvdStock
public int getDvdStock() //Method
{
    return dvdStock;
} //end method getDvdStock

public void setDvdPrice(double price) //Method
{
    dvdPrice = price;
} //end method setDvdPrice

//return dvdPrice
public double getDvdPrice() //Method
{
    return dvdPrice;
} //end method getDvdPrice

public void setDvdItem(int item) //Method
{
    dvdItem = item;
} //end method setdvdItem

//return DVD item
public int getDvdItem() //Method
{
    return dvdItem;
} //end  method getDvdItem

//calculate inventory value
//    v v v v v v v 
public double value() //Method
{
    return dvdPrice * dvdStock;
} //end method value

/*    public double valueTotal(extendedDVD[] item) //Method
{
double iTotal = 0.0;
for (int i = 0; i < item.length; i++) // "for" Argument
{
iTotal = iTotal + item[i].value();
}
return iTotal;
} //end method value*/

public int compareTo(DVD d) //Method
{
    int lastCmp = dvdTitle.compareTo(d.dvdTitle);
    return (lastCmp != 0 ? lastCmp : dvdTitle.compareTo(d.dvdTitle));
}// end method value

} //end class DVD

extendedDVD

class extendedDVD extends DVD {
public extendedDVD(String title, int stock, double price, int item) //Method
{
    super(title, stock, price, item);
} // end method value

public double restockFee() //Method
{
    return getDvdPrice() * .05;
}//end method value

public double totalRestock() //Method
{
    return restockFee() + getDvdPrice();
} // end method value

}// end class extendedDVD 

GUI

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;

@SuppressWarnings("serial")
public class GUI extends JFrame {

int Index = 0;
private final JTextPane textTitle;
private final JTextPane textItem;
private final JTextPane textStock;
private final JTextPane textPrice;
private final JTextPane textFee;
private final JTextPane textTpro;

product mainBox = new product();

public GUI() {
                setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Margaret\\workspace\\FinalGUI\\ResImgs\\CenterLogo.png"));
    getContentPane().setBackground(Color.DARK_GRAY);
    setTitle("UniMedia:        DVD Inventory");
    setSize(900, 450);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    getContentPane().setLayout(null);

    JButton btnFirst = new JButton("");
    btnFirst.setToolTipText("Go to First DVD");
    btnFirst.setIcon(new ImageIcon("C:\\Users\\Margaret\\workspace\\FinalGUI\\ResImgs\\FirstArrow.png"));
    btnFirst.setContentAreaFilled(false);
    btnFirst.setFocusPainted(false);
    btnFirst.setBorderPainted(false);
    btnFirst.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent click) {
            Index = 0;
            setValues();//refreshes values to new index
        }
    });
    btnFirst.setBounds(10, 360, 38, 40);
    getContentPane().add(btnFirst);

    JButton btnPrevious = new JButton("");
    btnPrevious.setIcon(new ImageIcon("C:\\Users\\Margaret\\workspace\\FinalGUI\\ResImgs\\PreviousArrow.png"));
    btnPrevious.setContentAreaFilled(false);
    btnPrevious.setFocusPainted(false);
    btnPrevious.setBorderPainted(false);
    btnPrevious.setToolTipText("Go to Previous DVD");
    btnPrevious.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent click) {
            if(Index == 0) {
                Index = mainBox.product().size() - 1;
                setValues();//refreshes values to new index
            } else {
                Index--;//index - 1
                setValues();//refreshes values to new index
            }
        }
    });
    btnPrevious.setBounds(135, 359, 38, 40);
    getContentPane().add(btnPrevious);

    JButton btnNext = new JButton("");
    btnNext.setToolTipText("Go to Next DVD");
    btnNext.setIcon(new ImageIcon("C:\\Users\\Margaret\\workspace\\FinalGUI\\ResImgs\\Next Arrow.png"));
    btnNext.setContentAreaFilled(false);
    btnNext.setFocusPainted(false);
    btnNext.setBorderPainted(false);
    btnNext.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent click) {
            if(Index == mainBox.product().size() - 1) {
                Index = 0;
                setValues();//refreshes values to new index
            } else {
                Index++;//index + 1
                setValues();//refreshes values to new index
            }
        }
    });
    btnNext.setBounds(209, 359, 38, 40);
    getContentPane().add(btnNext);

    JButton btnLast = new JButton("");
    btnLast.setToolTipText("Go to Last DVD");
    btnLast.setIcon(new ImageIcon("C:\\Users\\Margaret\\workspace\\FinalGUI\\ResImgs\\LastArrow.png"));
    btnLast.setContentAreaFilled(false);
    btnLast.setFocusPainted(false);
    btnLast.setBorderPainted(false);
    btnLast.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent click) {
            Index = mainBox.product().size() - 1;
            setValues();//refreshes values to new index
        }
    });
    btnLast.setBounds(353, 360, 34, 35);
    getContentPane().add(btnLast);

    JLabel lblNewLabel = new JLabel("Title:");
    lblNewLabel.setForeground(new Color(0, 206, 209));
    lblNewLabel.setFont(new Font("Segoe UI", Font.ITALIC, 13));
    lblNewLabel.setBounds(602, 23, 46, 14);
    getContentPane().add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Item #:");
    lblNewLabel_1.setForeground(new Color(0, 206, 209));
    lblNewLabel_1.setFont(new Font("Segoe UI", Font.ITALIC, 13));
    lblNewLabel_1.setBounds(602, 58, 46, 14);
    getContentPane().add(lblNewLabel_1);

    JLabel lblNewLabel_2 = new JLabel("Stock:");
    lblNewLabel_2.setForeground(new Color(0, 206, 209));
    lblNewLabel_2.setFont(new Font("Segoe UI", Font.ITALIC, 13));
    lblNewLabel_2.setBounds(602, 93, 46, 14);
    getContentPane().add(lblNewLabel_2);

    JLabel lblNewLabel_3 = new JLabel("Price:");
    lblNewLabel_3.setForeground(new Color(0, 206, 209));
    lblNewLabel_3.setFont(new Font("Segoe UI", Font.ITALIC, 13));
    lblNewLabel_3.setBounds(602, 127, 46, 14);
    getContentPane().add(lblNewLabel_3);

    textTitle = new JTextPane();
    textTitle.setFont(new Font("Eras Light ITC", Font.ITALIC, 15));
    textTitle.setForeground(Color.WHITE);
    textTitle.setBackground(Color.DARK_GRAY);
    textTitle.setEditable(false);
    textTitle.setBounds(746, 15, 124, 20);
    getContentPane().add(textTitle);

    textItem = new JTextPane();
    textItem.setForeground(Color.WHITE);
    textItem.setFont(new Font("Segoe UI Light", Font.PLAIN, 15));
    textItem.setBackground(Color.DARK_GRAY);
    textItem.setEditable(false);
    textItem.setBounds(746, 52, 124, 20);
    getContentPane().add(textItem);

    textStock = new JTextPane();
    textStock.setForeground(Color.WHITE);
    textStock.setFont(new Font("Segoe UI Light", Font.PLAIN, 15));
    textStock.setBackground(Color.DARK_GRAY);
    textStock.setEditable(false);
    textStock.setBounds(746, 87, 124, 20);
    getContentPane().add(textStock);

    textPrice = new JTextPane();
    textPrice.setForeground(Color.WHITE);
    textPrice.setFont(new Font("Segoe UI Light", Font.PLAIN, 15));
    textPrice.setBackground(Color.DARK_GRAY);
    textPrice.setEditable(false);
    textPrice.setBounds(746, 121, 124, 20);
    getContentPane().add(textPrice);

    JLabel lblNewLabel_4 = new JLabel("Restock Fee : 5%");
    lblNewLabel_4.setForeground(new Color(0, 206, 209));
    lblNewLabel_4.setFont(new Font("Segoe UI", Font.ITALIC, 13));
    lblNewLabel_4.setBounds(602, 183, 110, 14);
    getContentPane().add(lblNewLabel_4);

    JLabel lblNewLabel_5 = new JLabel("Total:");
    lblNewLabel_5.setForeground(new Color(0, 206, 209));
    lblNewLabel_5.setFont(new Font("Segoe UI", Font.ITALIC, 13));
    lblNewLabel_5.setBounds(602, 232, 46, 14);
    getContentPane().add(lblNewLabel_5);

    textFee = new JTextPane();
    textFee.setForeground(Color.WHITE);
    textFee.setFont(new Font("Segoe UI Light", Font.PLAIN, 15));
    textFee.setBackground(Color.DARK_GRAY);
    textFee.setEditable(false);
    textFee.setBounds(761, 183, 109, 20);
    getContentPane().add(textFee);

    textTpro = new JTextPane();
    textTpro.setForeground(Color.WHITE);
    textTpro.setFont(new Font("Segoe UI Light", Font.PLAIN, 15));
    textTpro.setBackground(Color.DARK_GRAY);
    textTpro.setEditable(false);
    textTpro.setBounds(761, 232, 109, 20);
    getContentPane().add(textTpro);

    JLabel lblTotalInventoryValue = new JLabel("Total Inventory Value ->");
    lblTotalInventoryValue.setForeground(Color.LIGHT_GRAY);
    lblTotalInventoryValue.setFont(new Font("Segoe UI", Font.ITALIC, 11));
    lblTotalInventoryValue.setBounds(685, 382, 139, 29);
    getContentPane().add(lblTotalInventoryValue);

    setValues();
    /* Set outside of the other method to avoid infinite adding cycle with next/previous/last/first buttons */
    //textTinv.setText(String.format("$%.2f", valueTotal()));

    JButton btnTotalInv = new JButton("");
    btnTotalInv.setIcon(new ImageIcon("C:\\Users\\Margaret\\workspace\\FinalGUI\\ResImgs\\TotalInventory.png"));
    btnTotalInv.setContentAreaFilled(false);
    btnTotalInv.setFocusPainted(false);
    btnTotalInv.setBorderPainted(false);
    btnTotalInv.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            JOptionPane.showMessageDialog(null, "The Inventory Total Is:     " + String.format("$%.2f",valueTotal()),null, JOptionPane.INFORMATION_MESSAGE);
        }
    });
    btnTotalInv.setBounds(824, 371, 46, 40);
    getContentPane().add(btnTotalInv);

    JLabel lblNewLabel_6 = new JLabel("");
    lblNewLabel_6.setIcon(new ImageIcon("C:\\Users\\Margaret\\workspace\\FinalGUI\\ResImgs\\CenterLogo.png"));
    lblNewLabel_6.setBounds(108, 58, 100, 100);
    setVisible(true);
    getContentPane().add(lblNewLabel_6);

    JLabel lblNewLabel_7 = new JLabel("");
    lblNewLabel_7.setIcon(new ImageIcon("C:\\Users\\Margaret\\workspace\\FinalGUI\\ResImgs\\UniMedia.gif"));
    lblNewLabel_7.setBounds(59, 0, 200, 214);
    getContentPane().add(lblNewLabel_7);

    JLabel lblUnimedia = new JLabel("UniMedia");
    lblUnimedia.setFont(new Font("Eras Demi ITC", Font.ITALIC, 39));
    lblUnimedia.setBounds(69, 202, 253, 58);
    getContentPane().add(lblUnimedia);

    JButton btnSave = new JButton("");
    btnSave.setContentAreaFilled(false);
    btnSave.setFocusPainted(false);
    btnSave.setBorderPainted(false);
    btnSave.setIcon(new ImageIcon("C:\\Users\\Margaret\\workspace\\FinalGUI\\ResImgs\\SaveData.png"));
    btnSave.setBounds(845, 280, 25, 31);
    btnSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        try{    
            cFile x = new cFile();
            x.openFile();
            x.addRecords();
            //x.closeFile();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "COULD NOT CREATE FILE OR DIRECTORY!",null,JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    getContentPane().add(btnSave);

    JButton btnEdit = new JButton("");
    btnEdit.setIcon(new ImageIcon("C:\\Users\\Margaret\\workspace\\FinalGUI\\ResImgs\\EditData.png"));
    btnEdit.setContentAreaFilled(false);
    btnEdit.setFocusPainted(false);
    btnEdit.setBorderPainted(false);
    btnEdit.setBounds(735, 280, 25, 31);
    getContentPane().add(btnEdit);

    JButton btnDelete = new JButton("");
    btnDelete.setIcon(new ImageIcon("C:\\Users\\Margaret\\workspace\\FinalGUI\\ResImgs\\Delete.png"));
    btnDelete.setContentAreaFilled(false);
    btnDelete.setFocusPainted(false);
    btnDelete.setBorderPainted(false);
    btnDelete.setBounds(697, 283, 25, 23);
    btnDelete.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         JOptionPane.showMessageDialog(null,"Entry Deleted!");                      //THIS CODE ISNT WORKING!!!
        while (Index == 0) {
             try{
             if (Index >= 0){
                    //Index = mainBox.product().remove(Index);
                    Index = Index -1;
                    Index = mainBox.product().size() - 1;
                    setValues();
            }else 
                        JOptionPane.showMessageDialog(null,"ENTRY COULD NOT BE DELETED!");
            }catch (Exception ex){
            JOptionPane.showMessageDialog(null,"Error Compiling! Fix your Code!");
            }
           }
     }
    });

    getContentPane().add(btnDelete);

    JLabel lblSearch = new JLabel("Search:");
    lblSearch.setFont(new Font("Segoe UI Light", Font.ITALIC, 11));
    lblSearch.setForeground(new Color(32, 178, 170));
    lblSearch.setHorizontalAlignment(SwingConstants.CENTER);
    lblSearch.setBounds(376, 23, 46, 14);
    getContentPane().add(lblSearch);

    JButton btnSearch = new JButton("Search");
    btnSearch.addActionListener(new ActionListener() {
        Scanner a = new Scanner(System.in);
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showInputDialog(null,"Enter DVD Title");
            a.nextLine();

        }
    });
    btnSearch.setBounds(433, 20, 89, 23);
    getContentPane().add(btnSearch);
 }

public void setValues() {
    extendedDVD col = mainBox.product().get(Index);

    textTitle.setText(String.valueOf(col.getDvdTitle()));
    textItem.setText(String.valueOf(col.getDvdItem()));
    textPrice.setText(String.format("$%.2f", col.getDvdPrice()));
    textStock.setText(String.valueOf(col.getDvdStock()));

    textFee.setText(String.format("$%.2f", col.restockFee()));
    textTpro.setText(String.format("$%.2f", col.value()));
}

private double valueTotal() {
    double tVal = 0;
    for (extendedDVD col : mainBox.product()) {
        tVal += col.value();
    }
    return tVal;
}
}

0 个答案:

没有答案