0

I have a program to calculate the amount of paint needed to cover certain area. I have a button that that I have coded which is supposed to clear the text field but it is not working. I have used the exact same format in other programs and it has worked before but for some reason it is not working in this particular program. The error says cannot find symbol on the clearjbuttonactionperformed. Please help.

    /*
FileName: PaintCalculator
Name: Bhattarai
Date: October.2014
*/

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

public class PaintCalculator extends JFrame
{
// declarations
   Color black = new Color(0, 0, 0);
   Color white = new Color(255, 255, 255);
   Color light_gray = new Color(192, 192, 192);

   DecimalFormat currency;
   JTextField currencyJTextField;

   JLabel roomWidthJLabel;
   JTextField roomWidthJTextField;

   JLabel roomLengthJLabel;
   JTextField roomLengthJTextField;

   JLabel numberofGallonsJLabel;
   JTextField numberofGallonsJTextField;

   JLabel subTotalJLabel;
   JTextField subTotalJTextField;

   JLabel salesTaxJLabel;
   JTextField salesTaxJTextField;

   JLabel totalAmountJLabel;
   JTextField totalAmountJTextField;

   JButton enterJButton;
   JButton clearJButton;

   double roomWidth;
   double roomLength;
   double numberofGallons;
   double subTotal;
   double salesTax;
   double totalAmount;

 public PaintCalculator()
    {
            createUserInterface();
    }

    public void createUserInterface()
    {
    Container contentPane = getContentPane();
    contentPane.setBackground(white);
    contentPane.setLayout(null);

    // initialize components

    // input
    roomWidthJLabel = new JLabel ();
    roomWidthJLabel.setBounds (50, 20, 150, 20);
    roomWidthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    roomWidthJLabel.setText ("Enter Width of Room (Ft):");
    roomWidthJLabel.setForeground(black);
    roomWidthJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(roomWidthJLabel);

    roomWidthJTextField = new JTextField();
    roomWidthJTextField.setBounds(230, 20, 100, 20);
    roomWidthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    roomWidthJTextField.setHorizontalAlignment(JTextField.CENTER);
    roomWidthJTextField.setForeground(black);
    roomWidthJTextField.setBackground(white);
    roomWidthJTextField.setEditable(true);
    contentPane.add(roomWidthJTextField);

    roomLengthJLabel = new JLabel();
    roomLengthJLabel.setBounds(50, 70, 150, 20);
    roomLengthJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    roomLengthJLabel.setText("Enter Length of Room (Ft):");
    roomLengthJLabel.setForeground(black);
    roomLengthJLabel.setHorizontalAlignment(JLabel.CENTER);
    contentPane.add(roomLengthJLabel);

    roomLengthJTextField = new JTextField();
    roomLengthJTextField.setBounds(230, 70, 100, 20);
    roomLengthJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    roomLengthJTextField.setHorizontalAlignment(JTextField.CENTER);
    roomLengthJTextField.setForeground(black);
    roomLengthJTextField.setBackground(white);
    roomLengthJTextField.setEditable(true);
    contentPane.add(roomLengthJTextField);


    // outputs
    numberofGallonsJLabel = new JLabel();
    numberofGallonsJLabel.setBounds(50, 120, 150, 20);
    numberofGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    numberofGallonsJLabel.setText("Number of Gallons:");
    numberofGallonsJLabel.setForeground(black);
    numberofGallonsJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(numberofGallonsJLabel);

    numberofGallonsJTextField = new JTextField();
    numberofGallonsJTextField.setBounds(230, 120, 100, 20);
    numberofGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    numberofGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
    numberofGallonsJTextField.setForeground(black);
    numberofGallonsJTextField.setBackground(white);
    numberofGallonsJTextField.setEditable(false);
    contentPane.add(numberofGallonsJTextField);

    subTotalJLabel = new JLabel();
    subTotalJLabel.setBounds(50, 170, 150, 20);
    subTotalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    subTotalJLabel.setText("Sub Total:");
    subTotalJLabel.setForeground(black);
    subTotalJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(subTotalJLabel);

    subTotalJTextField = new JTextField();
    subTotalJTextField.setBounds(230, 170, 100, 20);
    subTotalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    subTotalJTextField.setHorizontalAlignment(JTextField.CENTER);
    subTotalJTextField.setForeground(black);
    subTotalJTextField.setBackground(white);
    subTotalJTextField.setEditable(false);
    contentPane.add(subTotalJTextField);

    salesTaxJLabel = new JLabel();
    salesTaxJLabel.setBounds(50, 220, 150, 20);
    salesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    salesTaxJLabel.setText("Sales Tax (6%):");
    salesTaxJLabel.setForeground(black);
    salesTaxJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(salesTaxJLabel);

    salesTaxJTextField = new JTextField();
    salesTaxJTextField.setBounds(230, 220, 100, 20);
    salesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    salesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
    salesTaxJTextField.setForeground(black);
    salesTaxJTextField.setBackground(white);
    salesTaxJTextField.setEditable(false);
    contentPane.add(salesTaxJTextField);

    totalAmountJLabel = new JLabel();
    totalAmountJLabel.setBounds(50, 270, 150, 20);
    totalAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    totalAmountJLabel.setText("Total Sales:");
    totalAmountJLabel.setForeground(black);
    totalAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
    contentPane.add(totalAmountJLabel);

    totalAmountJTextField = new JTextField();
    totalAmountJTextField.setBounds(230, 270, 100, 20);
    totalAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    totalAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
    totalAmountJTextField.setForeground(black);
    totalAmountJTextField.setBackground(white);
    totalAmountJTextField.setEditable(false);
    contentPane.add(totalAmountJTextField);

    // control
    enterJButton = new JButton();
    enterJButton.setBounds(50, 320, 100, 30);
    enterJButton.setFont(new Font("Default", Font.BOLD, 12));
    enterJButton.setText("Enter");
    enterJButton.setForeground(black);
    enterJButton.setBackground(white);
    contentPane.add(enterJButton);
    enterJButton.addActionListener(

        new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                enterJButtonActionPerformed(event);
            }
        }
    );

    clearJButton = new JButton();
    clearJButton.setBounds(230, 320, 100, 30);
    clearJButton.setFont(new Font("Default", Font.BOLD, 12));
    clearJButton.setText("Clear");
    clearJButton.setForeground(black);
    clearJButton.setBackground(white);
    contentPane.add(clearJButton);
    clearJButton.addActionListener(

  new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    clearJButtonActionPerformed(event);
                }
            }
    );


        // set properties of application’s window
        setTitle("Paint Calculator");             // set title
        setSize( 400, 400 );                  // set window size
        setVisible(true);                     // display window
        }

        // main method
        public static void main(String[] args)
        {
             PaintCalculator application = new PaintCalculator();
             application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             }
        public void enterJButtonActionPerformed(ActionEvent event)
        {
        getRoomWidthInput();
              }
              /* Try-Catch Method */
              public void getRoomWidthInput()
              {
                  try
                  {
                      roomWidth = Double.parseDouble(roomWidthJTextField.getText());
                      getRoomLengthInput();
                  }
                  catch(NumberFormatException exception)
                            {
                            JOptionPane.showMessageDialog(this,
                            "Please enter Room Width!",
                            "Number Format Error", JOptionPane.ERROR_MESSAGE );
                            roomWidthJTextField.setText("");
                            roomWidthJTextField.requestFocusInWindow();
                        }
                    }

                public void getRoomLengthInput()
            {
                try
                {
                    roomLength = Double.parseDouble(roomLengthJTextField.getText());
                    calculateNumberofGallons();
                }
                catch(NumberFormatException exception)
                {
                    JOptionPane.showMessageDialog(this,
                    "Please enter Room Length!",
                    "Number Format Error", JOptionPane.ERROR_MESSAGE );
                    roomLengthJTextField.setText("");
                    roomLengthJTextField.requestFocusInWindow();
                }
        }

        public void  calculateNumberofGallons()
        {                                       //process data
        final double Tax_Rate = .07;
        final double priceperGallon = 24.95;
        numberofGallons = (roomWidth * roomLength)/ 400;
        subTotal = numberofGallons * priceperGallon;
        salesTax = subTotal * Tax_Rate;
        totalAmount = subTotal + salesTax;

        //display results
        currency = new DecimalFormat("$0.00");

        numberofGallonsJTextField.setText("" + numberofGallons);
        subTotalJTextField.setText("" + currency.format(subTotal));
        salesTaxJTextField.setText("" + currency.format(salesTax));
        totalAmountJTextField.setText("" + currency.format(totalAmount));
    }
}
braX
  • 11,506
  • 5
  • 20
  • 33
  • 2
    So where's the code for `clearJButtonActionPerformed`? Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Oct 06 '14 at 00:03
  • 2
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. Have a look at [Why is it frowned upon to use a null layout in SWING?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) for more details – MadProgrammer Oct 06 '14 at 00:04
  • 2
    `I have used the exact same format in other programs and it has worked before` - well then compare your code to see what the difference is. Don't expect others to debug your code for you. Once way to do this is to create the frame with one test field and one button. Get the code to work then add another text field and get the code to work and so on. Do basic testing along the way and don't write the entire code before testing. Then is you have a simple question you have a reasonable amount of code to post. Your question has too much code for us to look at, most is irrelevant to the problem. – camickr Oct 06 '14 at 01:18

0 Answers0