JButton购物车

时间:2018-03-28 15:42:03

标签: java eclipse user-interface

欢迎大家,

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

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

所以我决定购买一个购物应用程序,允许您选择几个游戏,将它们添加到购物车,然后显示购物车/编辑/删除项目等。

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

驱动程序:

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

MainShop:

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

    }

PS4类

{{1}}

那么如何通过点击"添加到购物车"添加我想要的新课程? JButton的? 最糟糕的是,我们必须在2周内展示它,它是在星期五东部休息前的最后一堂课上给我们的。因此,在没有我们的讲师帮助的情况下,我们有两周的时间来完成它。 另外,请不要担心代码结构/布局等,因为这将在最后完成。现在我试图让代码正常工作。

现在,我知道它的帖子很长,所以任何建议都会受到赞赏 如果有人需要截图,请告诉我,我会添加它们。 谢谢!

1 个答案:

答案 0 :(得分:0)

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

立即想到两个选项。

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