简体   繁体   中英

JButton shopping cart

Welcome everyone,

This is my first post on this website and I am also quite new to java so go easy on me :)

I am working on a project for my college the task is: "Design & Build a Java application based on a class linked to a swing (GUI) application. "

So I Decided to go for a shopping app that will allow you to choose a few games, add them to cart and then display the cart / edit it / remove items etc.

I am having problem with adding items to a cart using a JButton. Here is my code:

Driver:

public class Driver {

    public static void main (String[] args) {
        MainShop shop= new MainShop ("Welcome to my Shop");
    }
}

MainShop:

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

public class MainShop extends JFrame implements ActionListener, WindowListener {

    private Container content;
    private JLabel l1 = new JLabel ("Welcome to my Shop");
    private JLabel l2 = new JLabel("You want to browse for great games?");
    private JLabel l3 = new JLabel ("Choose one of the following 2 platforms: "); 
    private JLabel l4 = new JLabel ("XBox has no good games. Try PS4 :) ");
    private  PS4 dialog1 = null ;
    private XBox dialog2 = null;
    private JButton b1 = new JButton("PS4");
    private JButton b2 = new JButton("XBox");


    public MainShop (String str) {

        super(str);
        content = getContentPane();
        content.setLayout(new GridLayout(2,2));
        content.add(l1);
        content.add(l2);
        content.add(l3);
        content.add(l4);
        l4.setVisible(false);
        content.add(b1);
        b1.addActionListener(this);
        content.add(b2);
        b2.addActionListener(this);
        setSize(800, 150);
        content.setBackground(Color.white);
        this.addWindowListener(this);
        setVisible(true);
    }

    public void actionPerformed (ActionEvent e) {
        Object target = e.getSource();
        if (target == b2) {
            l4.setVisible(true);
        }

        if (target == b1) {
            l4.setVisible(false);
            this.setVisible(false);
            new PS4(this, "PS4");
            }
        }

    public void windowActivated(WindowEvent e){}
    public void windowClosed(WindowEvent e){}
    public void windowClosing(WindowEvent e){System.exit(0);}
    public void windowDeactivated(WindowEvent e){}
    public void windowDeiconified(WindowEvent e){}
    public void windowIconified(WindowEvent e){}
    public void windowOpened(WindowEvent e){}
}

PS4 class

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

 class PS4 extends JFrame implements ActionListener, WindowListener{

    private JPanel Center = new JPanel();
    private JPanel South = new JPanel ();
    private JPanel North = new JPanel ();
    private JPanel South1 = new JPanel();
    private JPanel South2 = new JPanel();
    private JPanel Center1 = new JPanel();
    private JPanel Center2 = new JPanel ();
    private JPanel Center3 = new JPanel ();

    private JLabel n1 = new JLabel ("PS4 Games");
    private JLabel c1desc = new JLabel ("Name");
    private JLabel li1 = new JLabel ("Like it?");
    private JLabel c2desc = new JLabel ("Description");
    private JLabel c3desc = new JLabel ("Price");
    private JButton basket = new JButton ("Proceed to checkout");
    private JButton CB = new JButton("Go Back");

    private JLabel g1 = new JLabel("Uncharted");
    private JButton b1 = new JButton("Add to Basket");
    private JLabel d1 = new JLabel (" Action-adventure ");
    private JLabel p1 = new JLabel ("59.99€");

    private JLabel g2 = new JLabel("Call of Duty");
    private JButton b2 = new JButton("Add to Basket");
    private JLabel d2 = new JLabel (" First Person Shooter ");
    private JLabel p2 = new JLabel ("69.99€");

    private JLabel g3 = new JLabel("Fifa 18");
    private JButton b3 = new JButton("Add to Basket");
    private JLabel d3 = new JLabel (" Sport ");
    private JLabel p3 = new JLabel ("69.99€");

    private JLabel g4 = new JLabel("Skyrim");
    private JButton b4 = new JButton("Add to Basket");
    private JLabel d4 = new JLabel (" Open World RPG ");
    private JLabel p4 = new JLabel ("49.99€");

    private Container content;
    private JFrame parent;

    public PS4 (JFrame p, String Str) {
         super(Str);
         parent = p;          
         getContentPane().add(North, BorderLayout.NORTH);
         North.add(n1);

        getContentPane().add(Center, BorderLayout.CENTER);
        Center.setLayout(new GridLayout(5,4));

        Center.add(c1desc);
        Center.add(c2desc);
        Center.add(c3desc);
        Center.add(li1);
        Center.add(g1);
        Center.add(d1);
        Center.add(p1);
        Center.add(b1); b1.addActionListener(this);
        Center.add(g2);
        Center.add(d2);
        Center.add(p2);
        Center.add(b2); b2.addActionListener(this);
        Center.add(g3);
        Center.add(d3);
        Center.add(p3);
        Center.add(b3); b3.addActionListener(this);
        Center.add(g4);
        Center.add(d4);
        Center.add(p4);
        Center.add(b4); b4.addActionListener(this);


        getContentPane().add(South, BorderLayout.SOUTH);
        South.setLayout(new GridLayout(1,2));
        South.add(South1);
        South.add(South2);
        South1.add(CB);
        CB.addActionListener(this);
        South2.add(basket); 
        basket.addActionListener(this);

        setSize(600,400);
        setVisible(true);
        this.addWindowListener(this);

     }

     public void actionPerformed(ActionEvent e) {

         Object target = e.getSource();
         if (target == b1) {
             // THIS IS WHERE I STRUGGLE
         }

         if (target == b2) {

         }

         if (target == b3) {

         }

         if(target == b4) {

         }

         if(target==CB) {
             this.setVisible(false);
             parent.setVisible(true);;
         }

         if (target == basket) {
             this.setVisible(false);
             new Checkout(this, "");
         }
     }

        public void windowActivated(WindowEvent e){}
        public void windowClosed(WindowEvent e){}
        public void windowClosing(WindowEvent e){System.exit(0);}
        public void windowDeactivated(WindowEvent e){}
        public void windowDeiconified(WindowEvent e){}
        public void windowIconified(WindowEvent e){}
        public void windowOpened(WindowEvent e){}

    }

So how do I Add the items I want to a new class by clicking the "Add to cart" JButton? The worst thing is that we have to present it in 2 weeks and it was given to us on our last class on Friday before eastern break. So we literally have 2 weeks to work on it without any help of our lecturers. Also, please do not worry about the code structure/layout etc as this will be done at the very end. Right now I'm trying to get the code working.

Now, I know its a long post so any suggestions would be appreciated If anyone needs screenshots just let me know and I will add them. Thanks!

I'm currently on my phone, so this answer will be brief for now, however I can help more if required when I get home.

Two options come to mind immediately.

  1. You could create an ArrayList called something like “basket” and add an item to the list, depending on which item's “add to basket” button you press.
  2. Create a custom object of your own, and use that as the shopping basket.

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