简体   繁体   中英

Trying to pass Variable in java from one class/Jframe to another but program complies and nothing happens?

Right so I have my ItemsPage Jframe Class and I'm trying to pass my TotalPrice variable to my CashPay so I can calculate the change. CashPrice runs and works but when I try run ItemsPage it does nothing I don't even get errors. I tried to remove that small section of trying to pass the variable to CashPay and it worked perfectly so I 100% know that's the problem. This section "public ItemsPage() { Pounds = "£"; //super();

    Scanner sc = new Scanner(System.in);
    CashPay sendTotalPrice = new CashPay();
    //System.out.println("Enter your amount");
    TotalPrice = sc.nextDouble();
    sendTotalPrice.printTotalPrice(TotalPrice);
    "

Here is complete code for both classes, I'm using GUI builder.

 import java.awt.BorderLayout; import java.awt.Checkbox; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JCheckBox; import java.util.Scanner; import javax.swing.WindowConstants; import javax.swing.SwingUtilities; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class ItemsPage extends javax.swing.JFrame { private JLabel Fruitslbl; private JLabel Snackslbl; private JCheckBox Appleschk; private JCheckBox Strawberrychk; private JButton Continuebtn; private static JButton Additemsbtn; private JCheckBox Smoothiechk; private JCheckBox Juicechk; private JCheckBox Sodachk; private JCheckBox Lambchk; private JCheckBox Chickenchk; private JCheckBox Chocolatechk; private JCheckBox Crispschk; private JCheckBox Orangeschk; private JLabel Drinkslbl; private JLabel Meatlbl; public double TotalPrice; private String Pounds; /** * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { ItemsPage inst = new ItemsPage(); inst.setLocationRelativeTo(null); inst.setVisible(true); //Additemsbtn.addActionListener( // new ActionListener() } }); } public ItemsPage() { Pounds = "£"; //super(); Scanner sc = new Scanner(System.in); CashPay sendTotalPrice = new CashPay(); //System.out.println("Enter your amount"); TotalPrice = sc.nextDouble(); sendTotalPrice.printTotalPrice(TotalPrice); } private void initGUI() { try { GridBagLayout thisLayout = new GridBagLayout(); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1}; thisLayout.rowHeights = new int[] {7, 7, 7, 7}; thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1}; thisLayout.columnWidths = new int[] {7, 7, 7, 7}; getContentPane().setLayout(thisLayout); this.setIconImage(new ImageIcon(getClass().getClassLoader().getResource("")).getImage()); Fruitslbl = new JLabel(); getContentPane().add(Fruitslbl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Fruitslbl.setText("Fruits"); Snackslbl = new JLabel(); getContentPane().add(Snackslbl, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Snackslbl.setText("Snacks"); Meatlbl = new JLabel(); getContentPane().add(Meatlbl, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Meatlbl.setText("Meat"); Drinkslbl = new JLabel(); getContentPane().add(Drinkslbl, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Drinkslbl.setText("Drinks"); Appleschk = new JCheckBox(); getContentPane().add(Appleschk, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 0, 0, 0), 0, 0)); Appleschk.setText("Apples"); Orangeschk = new JCheckBox(); getContentPane().add(Orangeschk, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Orangeschk.setText("Oranges"); Strawberrychk = new JCheckBox(); getContentPane().add(Strawberrychk, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Strawberrychk.setText("Strawberry"); Crispschk = new JCheckBox(); getContentPane().add(Crispschk, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Crispschk.setText("Crisps"); Chocolatechk = new JCheckBox(); getContentPane().add(Chocolatechk, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Chocolatechk.setText("Chocolate"); Chickenchk = new JCheckBox(); getContentPane().add(Chickenchk, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Chickenchk.setText("Chicken"); Lambchk = new JCheckBox(); getContentPane().add(Lambchk, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Lambchk.setText("Lamb"); Sodachk = new JCheckBox(); getContentPane().add(Sodachk, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Sodachk.setText("Soda"); Juicechk = new JCheckBox(); getContentPane().add(Juicechk, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Juicechk.setText("Juice"); Smoothiechk = new JCheckBox(); getContentPane().add(Smoothiechk, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Smoothiechk.setText("Smoothie"); Additemsbtn = new JButton(); getContentPane().add(Additemsbtn, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Additemsbtn.setText("Add Items"); Additemsbtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { //System.out.println("Additemsbtn.actionPerformed, event="+evt+TotalPrice); //TODO add your code for Additemsbtn.actionPerformed checkwhatIsSelected(); } }); Continuebtn = new JButton(); getContentPane().add(Continuebtn, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Continuebtn.setText("Complete Checkout"); Continuebtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { //System.out.println("Continuebtn.actionPerformed, event="+evt); //TODO add your code for Continuebtn.actionPerformed new CashPay().setVisible(true); // Main Form to show after the Login Form.. CashPay CashPay = new CashPay(); CashPay.printTotalPrice(TotalPrice); } }); pack(); this.setSize(841, 593); } catch (Exception e) { //add your error handling code here e.printStackTrace(); } } // private void handleJCheckbox(double Strawberrychk) { // if (Strawberrychk. == true) // TotalPrice += 1.99; // TODO Auto-generated method stub //return false;} public void checkwhatIsSelected() { if(Strawberrychk.isSelected()) { //is checked TotalPrice = TotalPrice + 1.99; System.out.println(" #################################################"); System.out.println(" Your Total Price is: " + Pounds + TotalPrice); System.out.println("Thank you for shopping with Jstore, please come again!"); System.out.println(" #################################################"); } if(Appleschk.isSelected()) { //is checked TotalPrice = TotalPrice + 0.87; System.out.println("Your Total Price is " + Pounds + TotalPrice); if(Orangeschk.isSelected()) { //is checked TotalPrice = TotalPrice + 1.99; System.out.println("Your Total Price is " + Pounds + TotalPrice); if(Crispschk.isSelected()) { //is checked TotalPrice = TotalPrice + 1.99; System.out.println("Your Total Price is " + Pounds + TotalPrice); } if(Lambchk.isSelected()) { //is checked TotalPrice = TotalPrice + 1.99; System.out.println("Your Total Price is " + Pounds + TotalPrice); } if(Chickenchk.isSelected()) { //is checked TotalPrice = TotalPrice + 1.99; System.out.println("Your Total Price is " + Pounds + TotalPrice); } if(Juicechk.isSelected()) { //is checked TotalPrice = TotalPrice + 1.99; System.out.println("Your Total Price is " + Pounds + TotalPrice); } } if(Smoothiechk.isSelected()) { //is checked TotalPrice = TotalPrice + 1.99; System.out.println("Your Total Price is " + Pounds + TotalPrice); } if(Sodachk.isSelected()) { //is checked TotalPrice = TotalPrice + 1.99; System.out.println("Your Total Price is " + Pounds + TotalPrice); } System.out.println(" #################################################"); System.out.println(" Your Total Price is: " + Pounds + TotalPrice); System.out.println("Thank you for shopping with Jstore, please come again!"); System.out.println(" #################################################"); /* else TotalPrice = TotalPrice + 0.00;*/ } /* } private void Strawberrychk() { // TODO Auto-generated method stub }*/ /*private void handleCheckbox1(Checkbox Appleschk) { if (Appleschk.getState()) TotalPrice += 0.99; else TotalPrice += 0.00; } private void handleCheckbox2(Checkbox Orangeschk) { if (Orangeschk.getState()) TotalPrice += 1.99; else TotalPrice += 0.00; } private void handleCheckbox3(Checkbox Crispschk) { if (Crispschk.getState()) TotalPrice += 0.89; else TotalPrice += 0.00; } private void handleCheckbox4(Checkbox Chocolateschk) { if (Chocolateschk.getState()) TotalPrice += 0.99; else TotalPrice += 0.00; }*/ } } 

Here is the CashPay Class Jframe

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.WindowConstants; import javax.swing.SwingUtilities; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class CashPay extends javax.swing.JFrame { private JLabel cashamountlbl; private JButton Calculatebtn; private JLabel CashChangelbl; private JTextField CashAmounttxt; private double ChangeLeft; /** * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { CashPay inst = new CashPay(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); ItemsPage callClass = new ItemsPage(); callClass.checkwhatIsSelected(); } //Converts input from textfield into double variable. public CashPay() { super(); initGUI(); //Receives variable from another class "ItemsPage" } public void printTotalPrice(double TotalPrice){ double CashInsert = Double.parseDouble(CashAmounttxt.getText()); ChangeLeft = CashInsert - TotalPrice; System.out.println (ChangeLeft); } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); getContentPane().setLayout(null); cashamountlbl = new JLabel(); getContentPane().add(cashamountlbl); cashamountlbl.setText("Enter Cash Amount"); cashamountlbl.setBounds(41, 65, 105, 34); cashamountlbl.setBackground(new java.awt.Color(128,0,255)); CashAmounttxt = new JTextField(); getContentPane().add(CashAmounttxt); CashAmounttxt.setBounds(215, 71, 87, 23); CashChangelbl = new JLabel(); getContentPane().add(CashChangelbl); CashChangelbl.setBounds(190, 117, 158, 109); CashChangelbl.setBackground(new java.awt.Color(255,128,64)); Calculatebtn = new JButton(); getContentPane().add(Calculatebtn); Calculatebtn.setText("Calculate Total"); Calculatebtn.setBounds(46, 203, 93, 23); Calculatebtn.addMouseListener(new MouseAdapter() { }); pack(); this.setSize(415, 319); } catch (Exception e) { //add your error handling code here e.printStackTrace(); } } } /*private void CashCal(){ if (CashInsert => RecievedTotalPrice)} }*/ 

Here are the changes I made to your code to make it work, it will help you but I would recommend into looking at some basic java tutorials so that the changes I made make some sense,

Several issues, first the first 4 lines of code refer to a console input, second, initGUI is not being called in main of ItemsPage() to show the main panel.

First change your ItemsPage() constructor, just call initGUI()

 public ItemsPage() 
  {
        Pounds = "£";
        initGUI();

    }

To call CashPay and use it to calculate the difference, all you need to do is construct it and pass the total price to it once the use click on Complete Checkout,

Continuebtn.setText("Complete Checkout");
Continuebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) 
{
 //System.out.println("Continuebtn.actionPerformed, event="+evt);
 //TODO add your code for Continuebtn.actionPerformed
 CashPay cashPay = new CashPay(TotalPrice);
 cashPay.setVisible(true);
}
});

Now, once the user selects all the items from the checkbox, you click on the Add Items button that will calculate the total amount, I found some errors in the method that was causing it not to calculate the total,

 public void checkwhatIsSelected() 
     {
         if(Strawberrychk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("   #################################################");
             System.out.println("       Your Total Price is: " + Pounds + TotalPrice);

             System.out.println("Thank you for shopping with Jstore, please come again!");
             System.out.println("   #################################################");
         }

         if(Appleschk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 0.87;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }   
       if(Orangeschk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         if(Crispschk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         if(Lambchk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         if(Chickenchk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         if(Juicechk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         if(Smoothiechk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         if(Sodachk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         System.out.println("   #################################################");
         System.out.println("       Your Total Price is: " + Pounds + TotalPrice);

         System.out.println("Thank you for shopping with Jstore, please come again!");
         System.out.println("   #################################################");

     }

Now, one of the main changes is how your CashPay is instantiated from the ItemsPage class, first you do not need main for it, so I removed main and just passed the totalprice value into the constructor, plus look below to see where printTotalPrices() is called, it now in the Calculatebtn actionListener

double TotalPrice = 0;

    public CashPay(double TotalPrice) {
        super();
        this.TotalPrice = TotalPrice;
        initGUI();
    }
    public void printTotalPrice(){
        double CashInsert = Double.parseDouble(CashAmounttxt.getText());
    System.out.println(TotalPrice+" <> "+CashInsert);
        ChangeLeft = CashInsert - TotalPrice;
        System.out.println (ChangeLeft);
    }
    private void initGUI() 
  {
        try 
    {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);

            cashamountlbl = new JLabel();
            getContentPane().add(cashamountlbl);
            cashamountlbl.setText("Enter Cash Amount");
            cashamountlbl.setBounds(41, 65, 105, 34);
            cashamountlbl.setBackground(new java.awt.Color(128,0,255));

            CashAmounttxt = new JTextField();
            getContentPane().add(CashAmounttxt);
            CashAmounttxt.setBounds(215, 71, 87, 23);

            CashChangelbl = new JLabel();
            getContentPane().add(CashChangelbl);
            CashChangelbl.setBounds(190, 117, 158, 109);
            CashChangelbl.setBackground(new java.awt.Color(255,128,64));

            Calculatebtn = new JButton();
            getContentPane().add(Calculatebtn);
            Calculatebtn.setText("Calculate Total");
            Calculatebtn.setBounds(46, 203, 93, 23);
            Calculatebtn.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent evt) 
                {
                  printTotalPrice();
                 } 
            });

            pack();
            this.setSize(415, 319);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }
    }

Here is the complete solution that compiles and works,

/*
*
*/

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import java.util.Scanner;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ItemsPage extends javax.swing.JFrame 
{
    private JLabel Fruitslbl;
    private JLabel Snackslbl;
    private JCheckBox Appleschk;
    private JCheckBox Strawberrychk;
    private JButton Continuebtn;
    private static JButton Additemsbtn;
    private JCheckBox Smoothiechk;
    private JCheckBox Juicechk;
    private JCheckBox Sodachk;
    private JCheckBox Lambchk;
    private JCheckBox Chickenchk;
    private JCheckBox Chocolatechk;
    private JCheckBox Crispschk;
    private JCheckBox Orangeschk;
    private JLabel Drinkslbl;
    private JLabel Meatlbl;
    public double TotalPrice;
    private String Pounds;



    /**
    * Auto-generated main method to display this JFrame
    */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ItemsPage inst = new ItemsPage();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public ItemsPage() 
  {
        Pounds = "£";
        initGUI();

    }

    private void initGUI() 
  {
        try 
    {
            GridBagLayout thisLayout = new GridBagLayout();
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
            thisLayout.rowHeights = new int[] {7, 7, 7, 7};
            thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
            thisLayout.columnWidths = new int[] {7, 7, 7, 7};
            getContentPane().setLayout(thisLayout);
            this.setIconImage(new ImageIcon(getClass().getClassLoader().getResource("")).getImage());

            Fruitslbl = new JLabel();
            getContentPane().add(Fruitslbl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Fruitslbl.setText("Fruits");

            Snackslbl = new JLabel();
            getContentPane().add(Snackslbl, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Snackslbl.setText("Snacks");

            Meatlbl = new JLabel();
            getContentPane().add(Meatlbl, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Meatlbl.setText("Meat");

            Drinkslbl = new JLabel();
            getContentPane().add(Drinkslbl, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Drinkslbl.setText("Drinks");

            Appleschk = new JCheckBox();
            getContentPane().add(Appleschk, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 0, 0, 0), 0, 0));
            Appleschk.setText("Apples");

            Orangeschk = new JCheckBox();
            getContentPane().add(Orangeschk, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Orangeschk.setText("Oranges");

            Strawberrychk = new JCheckBox();
            getContentPane().add(Strawberrychk, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Strawberrychk.setText("Strawberry");


            Crispschk = new JCheckBox();
            getContentPane().add(Crispschk, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Crispschk.setText("Crisps");



            Chocolatechk = new JCheckBox();
            getContentPane().add(Chocolatechk, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Chocolatechk.setText("Chocolate");

            Chickenchk = new JCheckBox();
            getContentPane().add(Chickenchk, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Chickenchk.setText("Chicken");

            Lambchk = new JCheckBox();
            getContentPane().add(Lambchk, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Lambchk.setText("Lamb");

            Sodachk = new JCheckBox();
            getContentPane().add(Sodachk, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Sodachk.setText("Soda");

            Juicechk = new JCheckBox();
            getContentPane().add(Juicechk, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Juicechk.setText("Juice");

            Smoothiechk = new JCheckBox();
            getContentPane().add(Smoothiechk, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Smoothiechk.setText("Smoothie");

            Additemsbtn = new JButton();
            getContentPane().add(Additemsbtn, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Additemsbtn.setText("Add Items");
            Additemsbtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    //System.out.println("Additemsbtn.actionPerformed, event="+evt+TotalPrice);
                    //TODO add your code for Additemsbtn.actionPerformed
                    checkwhatIsSelected();
                }
            });

            Continuebtn = new JButton();
            getContentPane().add(Continuebtn, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            Continuebtn.setText("Complete Checkout");
            Continuebtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) 
        {
                    //System.out.println("Continuebtn.actionPerformed, event="+evt);
                    //TODO add your code for Continuebtn.actionPerformed
                    CashPay cashPay = new CashPay(TotalPrice);
          cashPay.setVisible(true);
                }
            });

            pack();
            this.setSize(841, 593);
        } 
    catch (Exception e) 
    {
            //add your error handling code here
            e.printStackTrace();
        }

    }


    // private void handleJCheckbox(double Strawberrychk) {



        // if (Strawberrychk. == true)
        //   TotalPrice += 1.99;
        // TODO Auto-generated method stub
        //return false;}

     public void checkwhatIsSelected() 
     {
         if(Strawberrychk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("   #################################################");
             System.out.println("       Your Total Price is: " + Pounds + TotalPrice);

             System.out.println("Thank you for shopping with Jstore, please come again!");
             System.out.println("   #################################################");
         }

         if(Appleschk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 0.87;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }   
       if(Orangeschk.isSelected())
         {
                 //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
     }
         if(Crispschk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         if(Lambchk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         if(Chickenchk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         if(Juicechk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         if(Smoothiechk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         if(Sodachk.isSelected())
         {
             //is checked 
             TotalPrice = TotalPrice + 1.99;
             System.out.println("Your Total Price is " + Pounds + TotalPrice);
         }
         System.out.println("   #################################################");
         System.out.println("       Your Total Price is: " + Pounds + TotalPrice);

         System.out.println("Thank you for shopping with Jstore, please come again!");
         System.out.println("   #################################################");

     }
}



import java.awt.event.MouseAdapter;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CashPay extends javax.swing.JFrame {

    private JLabel cashamountlbl;
    private JButton Calculatebtn;
    private JLabel CashChangelbl;
    private JTextField CashAmounttxt;
    private double ChangeLeft;

    double TotalPrice = 0;

    public CashPay(double TotalPrice) {
        super();
        this.TotalPrice = TotalPrice;
        initGUI();
    }
    public void printTotalPrice(){
        double CashInsert = Double.parseDouble(CashAmounttxt.getText());
    System.out.println(TotalPrice+" <> "+CashInsert);
        ChangeLeft = CashInsert - TotalPrice;
        System.out.println (ChangeLeft);
    }
    private void initGUI() 
  {
        try 
    {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);

            cashamountlbl = new JLabel();
            getContentPane().add(cashamountlbl);
            cashamountlbl.setText("Enter Cash Amount");
            cashamountlbl.setBounds(41, 65, 105, 34);
            cashamountlbl.setBackground(new java.awt.Color(128,0,255));

            CashAmounttxt = new JTextField();
            getContentPane().add(CashAmounttxt);
            CashAmounttxt.setBounds(215, 71, 87, 23);

            CashChangelbl = new JLabel();
            getContentPane().add(CashChangelbl);
            CashChangelbl.setBounds(190, 117, 158, 109);
            CashChangelbl.setBackground(new java.awt.Color(255,128,64));

            Calculatebtn = new JButton();
            getContentPane().add(Calculatebtn);
            Calculatebtn.setText("Calculate Total");
            Calculatebtn.setBounds(46, 203, 93, 23);
            Calculatebtn.addActionListener(new ActionListener() 
      {
                public void actionPerformed(ActionEvent evt) 
        {
                  printTotalPrice();
        } 
            });

            pack();
            this.setSize(415, 319);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM