I have a project that I am doing, but I cannot get my final answer to go into the text field that I want, it keeps coming out in a new terminal window. What did I do wrong??

import java.awt.Color; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.*;
//   A cash register totals up sales and computes change due.
public class CashRegister
{
    public static final double DOLLAR_VALUE = 1.00;
    public static final double QUARTER_VALUE = 0.25;
    public static final double DIME_VALUE = 0.1;
    public static final double NICKEL_VALUE = 0.05;
    public static final double PENNY_VALUE = 0.01;

    public double Dollars;
    public double Quarters;
    public double Dimes;
    public double Nickels;
    public double Pennies;

    private double purchase;
    private double payment;
    private static final int FRAME_WIDTH = 500; 
    private static final int FRAME_HEIGHT = 400;


    public static void main(String[] args) 
    {  
         JFrame frame = new JFrame(); 
         JPanel panel = new JPanel();

        final int FIELD_WIDTH = 5;

        final JLabel label = new JLabel("  Enter total payment amount, EX: 11.76");
        final JTextField totalCostField = new JTextField(FIELD_WIDTH);

        final JLabel labelDollars = new JLabel("  Enter dollars used if none enter 0, EX: 11");
        final JTextField totalPayDollars = new JTextField(FIELD_WIDTH);

        final JLabel labelQuarters = new JLabel("  Enter quarters used if none enter 0, EX: 2");
        final JTextField totalPayQuarters = new JTextField(FIELD_WIDTH);

        final JLabel labelDimes = new JLabel("  Enter dimes used if none enter 0, EX: 2");
        final JTextField totalPayDimes = new JTextField(FIELD_WIDTH);

        final JLabel labelNickels = new JLabel("  Enter nickels used if none enter 0, EX: 1");
        final JTextField totalPayNickels = new JTextField(FIELD_WIDTH);

        final JLabel labelPennies = new JLabel("  Enter pennies used if none enter 0, EX: 1");
        final JTextField totalPayPennies = new JTextField(FIELD_WIDTH);

        final JLabel labelChange = new JLabel("  This is your change ------>");
        final JTextField totalChange = new JTextField(FIELD_WIDTH);


         JButton buttonSmaller = new JButton("Click for change.");
         JButton buttonLarger = new JButton("Non-Functional at this time. ");

        frame.add(panel); 
        panel.setLayout(new GridLayout(8,1));

        panel.add(label);
        panel.add(totalCostField);

        panel.add(labelDollars);
        panel.add(totalPayDollars);

        panel.add(labelQuarters);
        panel.add(totalPayQuarters);

        panel.add(labelDimes);
        panel.add(totalPayDimes);

        panel.add(labelNickels);
        panel.add(totalPayNickels);

        panel.add(labelPennies);
        panel.add(totalPayPennies);


        panel.add(buttonSmaller);
        panel.add(buttonLarger);

        panel.add(labelChange);
        panel.add(totalChange);

        class totalCostListener implements ActionListener
            {
            public void actionPerformed(ActionEvent event)
                {    
                        double Cost = Double.parseDouble(totalCostField.getText());                     
                }
            }

        class totalPayDollars implements ActionListener
            { 
            public void actionPerformed(ActionEvent event)
                {      
                        double Dollars = Double.parseDouble(totalPayDollars.getText());                     
                }
            }

        class totalPayQuarters implements ActionListener
            { 
            public void actionPerformed(ActionEvent event)
                {      
                        double Quarters = Double.parseDouble(totalPayQuarters.getText());                   
                }
            }

        class totalPayDimes implements ActionListener
            { 
            public void actionPerformed(ActionEvent event)
                {      
                        double Dimes = Double.parseDouble(totalPayDimes.getText());                    
                }
            }

        class totalPayNickels implements ActionListener
            { 
            public void actionPerformed(ActionEvent event)
                {      
                        double Nickels = Double.parseDouble(totalPayNickels.getText());                    
                }
            }

            class totalPayPennies implements ActionListener
            { 
            public void actionPerformed(ActionEvent event)
                {      
                        double Pennies = Double.parseDouble(totalPayPennies.getText());                    
                }
            }

        class SmallerButtonListener implements ActionListener
            {  
            public void actionPerformed(ActionEvent event)
                {
                    double payment = (Double.parseDouble(totalPayDollars.getText())) 
                                         +(Double.parseDouble(totalPayQuarters.getText())* QUARTER_VALUE) 
                                         + (Double.parseDouble( totalPayDimes.getText())* DIME_VALUE)
                                         + (Double.parseDouble( totalPayNickels.getText())* NICKEL_VALUE)
                                         + (Double.parseDouble(totalPayPennies.getText())* PENNY_VALUE); 
                    double Cost = Double.parseDouble(totalCostField.getText()); 
                    double totalChange = Cost - payment;
                        System.out.println("The Change is: " + totalChange);

                    label.repaint();
                }
            }

        class LargerButtonListener implements ActionListener
            {   
            public void actionPerformed(ActionEvent event)
                { 
                    label.repaint();
                }
            }

            ActionListener listenertotalCost = new totalCostListener();
            totalCostField.addActionListener(listenertotalCost);

            ActionListener listenertotalPayDollars = new totalPayDollars();
            totalPayDollars.addActionListener(listenertotalPayDollars);

            ActionListener listenertotalPayQuarters = new totalPayQuarters();
            totalPayQuarters.addActionListener(listenertotalPayQuarters);

            ActionListener listenertotalPayDimes = new totalPayDimes();
            totalPayDimes.addActionListener(listenertotalPayDimes);

            ActionListener listenertotalPayNickels = new totalPayNickels();
            totalPayNickels.addActionListener(listenertotalPayNickels);

            ActionListener listenertotalPayPennies = new totalPayPennies();
            totalPayPennies.addActionListener(listenertotalPayPennies);

            ActionListener listenerSmaller = new SmallerButtonListener();
            buttonSmaller.addActionListener(listenerSmaller);

            ActionListener listenerLarger = new LargerButtonListener();
            buttonLarger.addActionListener(listenerLarger);

        // Show the frame 
            frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.setTitle("ColorChangerViewer");
            frame.setLocation(100,100);
            frame.setVisible(true); 
    }       
}

Edited 3 Years Ago by happygeek: fixed formatting

For future please use hash "#" sign in the toolbar to insert code

final JTextField totalChange = new JTextField(FIELD_WIDTH);

double totalChange = Cost - payment;

That is your problem, previously declared variable re-declared in defferent part of code

with quick variable update

final JTextField totalChangeJTF = new JTextField(FIELD_WIDTH);
double totalChange = Cost - payment;
...............................................
totalChangeJTF.setText(Double.toString(totalChange) );
This article has been dead for over six months. Start a new discussion instead.