/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package retailsalecalc;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
*
* @author Shawna Rowe
* Prg 421
* 12/17/2012
* Yi Mi
* University of Phoenix
*/
public class RetailSaleCalc extends JFrame {
private JPanel retailPanel; //panel to hold components
private JTextField itemNumber; //text field for item number
private JLabel itemNumberLabel; //label for text field item numnber
private JList nameBox; //combo box for list of departments
private JLabel deptLabel; //label for department combo box
private JTextField originalPrice; //text field for original price
private JLabel originalPriceLabel; //label for original price text field
private JTextField percentOff; //text field for percentage sale discount
private JLabel percentOffLabel; //label for percentage discount
private JButton addButton;
private JButton computeButton;
static JTextArea listitems;
private JLabel displayLabel;
Double salePrice;
static JPanel displayPanel;
static String sale;
private JLabel saleTextField;
final int width = 1450, //window width
height = 900; //window height
public RetailSaleCalc()
{
//call the JFrame Constructor and pass the title
super ("Retail Sales Calculator");
//set size of window
setSize(width, height);
//set layout
setLayout(new GridLayout(2, 1));
//specify what happens when window is closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//build panel and add to frame
buildPanel();
displayPanel();
//add the panels
add (retailPanel);
add (displayPanel);
pack();
//display the window with panel
retailPanel.setVisible(true);
displayPanel.setVisible(true);
}
private void buildPanel(){
//create components
//create the panel to hold components
retailPanel = new JPanel();
//create custom panels
retailPanel.setLayout(new GridLayout(5, 3, 50, 100));
//create the label, and text field for item number
itemNumberLabel = new JLabel("Enter the item number: ");
itemNumber = new JTextField(8);
//create the label and combo box for departments
deptLabel = new JLabel("Choose the department for the item: ");
String[] names = { "Womens", "Mens", "Childrens", "Toys", "Electronics"};
nameBox = new JList(names);
//create the label and text field for original price
originalPriceLabel = new JLabel("Enter the item's original price: ");
originalPrice = new JTextField(10);
//create label and text field for percentage off
percentOffLabel = new JLabel("Enter the 2 digit percent off for sale: ");
percentOff = new JTextField(2);
//add the item to the list button
addButton = new JButton("Add this item to calculate sale price.");
addButton.addActionListener(new AddButtonListener());
//compute all items once done
computeButton = new JButton("Done adding items COMPUTE NOW.");
computeButton.addActionListener(new CalcButtonListener());
//add components to panel
retailPanel.add(nameBox);
retailPanel.add(deptLabel);
retailPanel.add(itemNumberLabel);
retailPanel.add(itemNumber);
retailPanel.add(originalPriceLabel);
retailPanel.add(originalPrice);
retailPanel.add(percentOffLabel);
retailPanel.add(percentOff);
retailPanel.add(addButton);
retailPanel.add(computeButton);
}
private class AddButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent f)
{
String itemname = itemNumber.getText();
String original = originalPrice.getText();
String percentage = percentOff.getText();
String dept =(String) nameBox.getSelectedValue();
//get the input info and display results in list
listitems = new JTextArea(6, 10);
listitems.setText(dept);
listitems.setText(itemname);
listitems.setText(original);
listitems.setText(percentage);
//display message if user has not completed all fields
}
}
private class CalcButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
//get userinput
String itemname = itemNumber.getText();
String original = originalPrice.getText();
String percentage = percentOff.getText();
String dept =(String) nameBox.getSelectedValue();
//when calc button is pushed convert text entered into int
//after conversion compute the sale price
double g = Double.parseDouble(original);
double p = Double.parseDouble(percentage);
salePrice =(g-(g*p));
double toBeString = salePrice;
String fromDouble ="" + toBeString;
fromDouble = sale;
//update display to reflect sale price
listitems = new JTextArea(6, 10);
listitems.setText(dept);
listitems.setText(itemname);
listitems.setText(original);
listitems.setText(percentage);
}
}
private void displayPanel()
{
displayPanel = new JPanel();
displayPanel.setLayout(new GridLayout(5, 3, 5, 10));
displayPanel.add(listitems);
displayLabel = new JLabel("Items you have added to compute sale prices.");
saleTextField = new JLabel("Sale price is: " + sale);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new RetailSaleCalc().setVisible(true);
}
}
errors:
run:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1090)
at java.awt.Container.add(Container.java:410)
at retailsalecalc.RetailSaleCalc.displayPanel(RetailSaleCalc.java:196)
at retailsalecalc.RetailSaleCalc.<init>(RetailSaleCalc.java:59)
at retailsalecalc.RetailSaleCalc.main(RetailSaleCalc.java:222)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

New Topic/Question
Reply


MultiQuote


|