繁体   English   中英

JButton购物车

[英]JButton shopping cart

欢迎大家,

这是我在该网站上的第一篇文章,我对Java也很陌生,所以请放轻松:)

我正在为我的大学从事一个项目,任务是:“基于链接到swing(GUI)应用程序的类设计和构建Java应用程序。”

因此,我决定去一个购物应用程序,它将允许您选择一些游戏,将它们添加到购物车中,然后显示购物车/对其进行编辑/删除物品等。

我在使用JButton将商品添加到购物车时遇到问题。 这是我的代码:

司机:

public class Driver {

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

总店:

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类

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){}

    }

那么,如何通过单击“添加到购物车” JButton将我想要的项目添加到新类中? 最糟糕的是,我们必须在两周内展示它,并且它是在东部休息之前的星期五的最后一堂课上给我们的。 因此,从字面上看,我们需要2周的时间来进行工作,而无需任何老师的帮助。 另外,请不要担心代码结构/布局等,因为这将在最后完成。 现在,我正在尝试使代码正常工作。

现在,我知道这是一篇很长的文章,所以任何建议都将不胜感激如果有人需要截图,请告诉我,我将添加它们。 谢谢!

我目前正在用手机打电话,所以这个答案现在很简短,但是到家后,如果需要的话,我可以提供更多帮助。

立即想到两个选择。

  1. 您可以创建一个名为“ basket”之类的ArrayList并将一个项目添加到列表中,具体取决于您按下哪个项目的“添加到购物篮”按钮。
  2. 创建您自己的自定义对象,并将其用作购物篮。

暂无
暂无

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

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