GUI.java
package ccc;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GUI implements ActionListener {
JFrame f;
JPanel bg, s, a;
JLabel ca, £, cav, co, cov, co2, pr, error;
JTextField tf;
JButton b1, b2;
public GUI(){
f = new JFrame();
bg = new JPanel();
bg.setSize(650, 500);
bg.setBackground(Color.darkGray);
bg.setLayout(null);
bg.setLocation(0, 0);
s = new JPanel();
s.setSize(180, 445);
s.setBackground(Color.white);
s.setLayout(null);
s.setLocation(10, 10);
a = new JPanel();
a.setSize(430, 445);
a.setBackground(Color.white);
a.setLayout(null);
a.setLocation(202, 10);
ca = new JLabel("CASH:");
ca.setSize(70,25);
ca.setLocation(10, 25);
£ = new JLabel("£");
£.setSize(10, 25);
£.setLocation(80, 25);
cav = new JLabel("128.23");
cav.setSize(80, 25);
cav.setLocation(90, 25);
co = new JLabel("COCAINE:");
co.setSize(70, 25);
co.setLocation(10, 50);
cov = new JLabel("6 units");
cov.setSize(70, 25);
cov.setLocation(80, 50);
co2 = new JLabel("COCAINE");
co2.setSize(70, 25);
co2.setLocation(10, 25);
pr = new JLabel("£39.95");
pr.setSize(60, 25);
pr.setLocation(90, 25);
tf = new JTextField();
tf.setSize(70, 25);
tf.setLocation(160, 25);
b1 = new JButton("BUY");
b1.setSize(70, 25);
b1.setLocation(270, 25);
b1.addActionListener(this);
b2 = new JButton("SELL");
b2.setSize(70, 25);
b2.setLocation(350, 25);
b2.addActionListener(this);
error = new JLabel();
error.setSize(200, 25);
error.setLocation(50, 50);
s.add(ca);
s.add(£);
s.add(cav);
s.add(co);
s.add(cov);
a.add(co2);
a.add(pr);
a.add(tf);
a.add(b1);
a.add(b2);
a.add(error);
bg.add(s);
bg.add(a);
f.add(bg);
f.setSize(650, 500);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1){
Buy a = new Buy();
a.Buy();
}
if (e.getSource() == b2){
}
}
}
Buy.java
package ccc;
public class Buy extends GUI {
String pri, qua, cas, uni, aaa;
double price, quantity, cash, units, total, deduction;
public Buy(){
pri = pr.getText();
price = Double.parseDouble(pri);
qua = tf.getText();
quantity = Double.parseDouble(qua);
cas = cav.getText();
cash = Double.parseDouble(cas);
uni = cov.getText();
units = Double.parseDouble(uni);
total = price * quantity;
if (cash >= total){
deduction = cash - total;
aaa = String.valueOf(deduction);
cav.setText(aaa);
}
else if (cash < total){
error.setText("Sorry, you don't currently have the funds");
}
}
}
Hi folks,
I am currently learning java and wanted to give myself a project that was both challenging and fun. I decided to build a game that I remember playing when I was a kid called dopewars.
This is my second attempt at this game. When I began my first attempt, all went well. After a short while my source code began to fill wildly out of control until I could continue no more as I kept getting lost within mountains of code.
I then decided to begin again, only this time I wanted to seperate the gui from the logic (2 different .java files). This is where my problem lies. Previously this would work fine. Since seperating my java files the functionality has stopped.
When I press jbutton b1, my program is supposed to take the price value of cocaine and the units value entered into the jtextfield by the user, perform a calculation by accessing a method within Buy.java, and then update the appropriate JLabels within the s jpanel of GUI.java.
For example, user x wants to buy cocaine at the price indicated, so he enters a value representing the quantity he would like. He then presses the buy button which ultimately deducts the money from his pocket which is shown on the left side of the program window by using a method within the Buy class.
I hope you can understand my explanation and I hope to hear from you soon. Thanks.in advance. My Source code is below.