need help with java program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • twin2003
    New Member
    • Aug 2007
    • 4

    need help with java program

    need help with inventory part 5 here is what I have to do
    Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item
    should display. the GUI using Java graphics classes.
    • Add a company logo to
    here is what i have so far

    Code:
    // created by Nicholas Baatz on July 24,2007
      import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    
    
    public class Inventory2
    {
    //main method begins execution of java application
    public static void main(final String args[])
    {
    
    int i; // varialbe for looping
    double total = 0; // variable for total inventory
    final int dispProd = 0; // variable for actionEvents
    
    // Instantiate a product object
    final ProductAdd[] nwProduct = new ProductAdd[5];
    // Instantiate objects for the array
    for (i=0; i<5; i++)
    {
    nwProduct[0] = new ProductAdd("Paper", 101, 10, 1.00, "Box");
    nwProduct[1] = new ProductAdd("Pen", 102, 10, 0.75, "Pack");
    nwProduct[2] = new ProductAdd("Pencil", 103, 10, 0.50, "Pack");
    nwProduct[3] = new ProductAdd("Staples", 104, 10, 1.00, "Box");
    nwProduct[4] = new ProductAdd("Clip Board", 105, 10, 3.00, "Two Pack");
    }
    
    
    
    for (i=0; i<5; i++)
    total += nwProduct.length; // calculate total inventory cost
    
    
    
    final JButton firstBtn = new JButton("First"); // first button
    final JButton prevBtn = new JButton("Previous"); // previous button
    final JButton nextBtn = new JButton("Next"); // next button
    final JButton lastBtn = new JButton("Last"); // last button
    final JLabel label; // logo
    final JTextArea textArea; // text area for product list
    final JPanel buttonJPanel; // panel to hold buttons
    
    //JLabel constructor for logo
    Icon logo = new ImageIcon("C:/logo.jpg"); // load logo
    label = new JLabel(logo); // create logo label
    label.setToolTipText("Company Logo"); // create tooltip
    
    
    buttonJPanel = new JPanel(); // set up panel
    buttonJPanel.setLayout( new GridLayout(1, 4)); //set layout
    // add buttons to buttonPanel
    buttonJPanel.add(firstBtn);
    buttonJPanel.add(prevBtn);
    buttonJPanel.add(nextBtn);
    buttonJPanel.add(lastBtn);
    
    
    textArea = new JTextArea(nwProduct[3]+"\n"); // create textArea for product display
    
    // add total inventory value to GUI
    textArea.append("\nTotal value of Inventory "+new java.text.DecimalFormat("$0.00").format(total)+"\n\n");
    textArea.setEditable(false); // make text uneditable in main display
    JFrame invFrame = new JFrame(); // create JFrame container
    invFrame.setLayout(new BorderLayout()); // set layout
    invFrame.getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER); // add textArea to JFrame
    invFrame.getContentPane().add(buttonJPanel, BorderLayout.SOUTH); // add buttons to JFrame
    invFrame.getContentPane().add(label, BorderLayout.NORTH); // add logo to JFrame
    invFrame.setTitle("Office Min Inventory"); // set JFrame title
    invFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // termination command
    //invFrame.pack();
    invFrame.setSize(400, 400); // set size of JPanel
    invFrame.setLocationRelativeTo(null); // set screem location
    invFrame.setVisible(true); // display window
    
    
    
    // assign actionListener and actionEvent for each button
    firstBtn.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    textArea.setText(nwProduct[0]+"\n");
    } // end firstBtn actionEvent
    
    }); // end firstBtn actionListener
    textArea.setText(nwProduct[4]+"n");
    
    // prevBtn.addActionListener(new ActionListener()
    // {
    // public void actionPerformed(ActionEvent ae)
    // {
    // dispProd = (nwProduct.length+dispProd-1) % nwProduct.length;
    // textArea.setText(nwProduct.display(dispProd)+"\n");
    // } // end prevBtn actionEvent
    // }); // end prevBtn actionListener
    
    
    
    
    
    } // end main
    
    } // end class Inventory2
    
    class Product
    {
    protected String prodName; // name of product
    protected int itmNumber; // item number
    protected int units; // number of units
    protected double price; // price of each unit
    protected double value; // value of total units
    
    
    public Product(String name, int number, int unit, double each) // Constructor for class Product
    {
    prodName = name;
    itmNumber = number;
    units = unit;
    price = each;
    
    } // end constructor
    
    public void setProdName(String name) // method to set product name
    {
    prodName = name;
    }
    
    public String getProdName() // method to get product name
    {
    return prodName;
    }
    
    public void setItmNumber(int number) // method to set item number
    {
    itmNumber = number;
    }
    
    public int getItmNumber() // method to get item number
    {
    return itmNumber;
    }
    
    public void setUnits(int unit) // method to set number of units
    {
    units = unit;
    }
    
    public int getUnits() // method to get number of units
    {
    return units;
    }
    
    public void setPrice(double each) // method to set price
    {
    price = each;
    }
    
    public double getPrice() // method to get price
    {
    return price;
    }
    
    public double calcValue() // method to set value
    {
    return units * price;
    }
    
    
    
    } // end class Product
    
    
    
    class ProductAdd extends Product
    {
    private String feature; // variable for added feature
    
    
    public ProductAdd(String name, int number, int unit, double each, String addFeat)
    {
    // call to superclass Product constructor
    super(name, number, unit, each);
    
    feature = addFeat;
    }// end constructor
    
    public void setFeature(String addFeat) // method to set added feature
    {
    feature = addFeat;
    }
    
    public String getFeature() // method to get added feature
    {
    return feature;
    }
    
    public double calcValueRstk() // method to set value and add restock fee
    {
    return units * price * 0.05;
    }
    
    public String toString()
    {
    return String.format("Product: %s\nItem Number: %d\nIn Stock: %d\nPrice: $%.2f\nType: %s\nTotal Value of Stock: $%.2f\nRestock Cost: $%.2f\n\n\n",
    getProdName(), getItmNumber(), getUnits(), getPrice(), getFeature(), calcValue(), calcValueRstk());
    }
    
    
    } // end class ProductAdd
    This code compiles and runds but cant get all my buttons to work any help would be nice thxs
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You haven't been specific enough with your problem. Which button doesn't work? What is it doing now vs what you want it to do?

    Comment

    Working...