简体   繁体   English

试图将Java中的Variable从一个类/ Jframe传递到另一类/ Jframe,但是程序符合要求,什么也没发生?

[英]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. 正确,所以我有ItemsPage Jframe类,并且试图将TotalPrice变量传递给CashPay,以便可以计算出更改。 CashPrice runs and works but when I try run ItemsPage it does nothing I don't even get errors. CashPrice可以运行并且可以运行,但是当我尝试运行ItemsPage时却什么也没做,甚至没有出错。 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. 我试图删除试图将变量传递给CashPay的那一小部分,它工作得很好,所以我100%知道这就是问题所在。 This section "public ItemsPage() { Pounds = "£"; //super(); 本节“ public ItemsPage(){磅=“£”; // 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. 这是这两个类的完整代码,我正在使用GUI构建器。

 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 这是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, 这是我对代码进行的更改以使其正常运行,它将为您提供帮助,但是我建议您看一些基本的Java教程,以便使我所做的更改有意义,

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. 有几个问题,首先是代码的前4行引用了控制台输入,其次,在ItemsPage()的main中未调用initGUI来显示主面板。

First change your ItemsPage() constructor, just call initGUI() 首先更改您的ItemsPage()构造函数,只需调用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, 要调用CashPay并使用它来计算差额,您需要做的就是构造它,并将使用后的总价传递给它,单击完成结帐,

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 现在,主要的更改之一是如何从ItemsPage类实例化CashPay,首先您不需要main,因此我删除了main,只是将totalprice值传递到了构造函数中,此外还请看下面的内容以查看printTotalPrices()被调用,现在在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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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