欢迎大家,
这是我在本网站上的第一篇文章,我也是Java的新文章,所以请轻松介绍我
我正在为我的大学做一个项目,任务是:"设计&建立基于链接到秋千的类的Java应用程序(GUI(申请。"
所以我决定去购买一个购物应用程序,该应用程序允许您选择几款游戏,将它们添加到购物车,然后显示购物车/编辑/删除项目等。
我在使用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周内展示它,并且在东部休息前的周五上班上给了我们。因此,我们实际上有2周的时间在没有讲师的任何帮助下进行工作。另外,请不要担心代码结构/布局等,因为这将在最后完成。现在,我正在尝试使代码正常工作。
现在,我知道这是一个很长的帖子,所以任何建议都将不胜感激如果有人需要屏幕截图,请告诉我,我会添加它们。谢谢!
我目前在手机上,所以这个答案现在很简短,但是如果我回家时我可以提供更多帮助。
立即想到两个选项。
- 您可以创建一个称为"篮子"的阵列列表,并在列表中添加一个项目,具体取决于您按哪个项目的"添加到篮子"按钮。
- 创建自己的自定义对象,并将其用作购物篮。