Jonas
Jonas

Reputation: 13

ActionListener not responding?

I am extremely new to Java code, however, I cannot find the answer to my problem anywhere else. I have a JFrame with 11 JButtons, each of which has called the .addActionListener(this); method. The first three and the last one respond like they are supposed to, however, JButtons buy4-buy10 do not trigger an ActionEvent. I have read over the code multiple times to eliminate typos, and Eclipse shows no errors, but the buttons still don't work.

import java.awt.FlowLayout;
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.JOptionPane;

public class MoneyMaker implements ActionListener {

    static JButton exit = new JButton("Exit");
    static JButton exitShop = new JButton("Exit Shop");
    static JButton buy2 = new JButton("Buy Newspapers - $40");
    static JButton buy3 = new JButton("Buy Car Wash - $250");
    static JButton buy4 = new JButton("Buy Donut Shop - $500");
    static JButton buy5 = new JButton("Buy Pizza Store - $800");
    static JButton buy6 = new JButton("Buy Furniture Store - $1000");
    static JButton buy7 = new JButton("Buy Car Dealer - $2500");
    static JButton buy8 = new JButton("Buy Whole Foods Market - $15000");
    static JButton buy9 = new JButton("Buy Silicon Valley - $50000");
    static JButton buy10 = new JButton("Buy Oil & Gas Company - $1000000");
    static int moneyint = 1;
    static JLabel moneyLabel = new JLabel("You have: $" + moneyint);
    static String itemBeingSold = "lemonade";
    static JButton sellItems = new JButton("Sell Items");
    static JButton openShop = new JButton("Upgrade Merchandise");
    static JLabel sellLabel = new JLabel("Currently selling: " + itemBeingSold);
    static JFrame ClickerFrame = new JFrame("MoneyMaker TM - How Rich Can You Get?");

    {
        ClickerFrame.setLayout(new FlowLayout());
        ClickerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ClickerFrame.setSize(475, 150);
        ClickerFrame.setLocation(500, 100);
        ClickerFrame.add(moneyLabel);
        ClickerFrame.add(sellLabel);
        ClickerFrame.add(sellItems);
        sellItems.addActionListener(this);
        ClickerFrame.add(openShop);
        openShop.addActionListener(this);
        ClickerFrame.add(exit);
        exit.addActionListener(this);
        ClickerFrame.setResizable(false);
        ClickerFrame.setVisible(false);
    }
    static JFrame shopFrame = new JFrame("The Shop TM - What Can You Buy?");

    {
        shopFrame.setLayout(new FlowLayout());
        shopFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        shopFrame.setSize(400, 250);
        shopFrame.setLocation(500, 100);
        shopFrame.setAlwaysOnTop(true);
        shopFrame.add(buy2);
        buy2.addActionListener(this);
        shopFrame.add(buy3);
        buy3.addActionListener(this);
        shopFrame.add(buy4);
        buy4.addActionListener(this);
        shopFrame.add(buy5);
        buy5.addActionListener(this);
        shopFrame.add(buy6);
        buy6.addActionListener(this);
        shopFrame.add(buy7);
        buy7.addActionListener(this);
        shopFrame.add(buy8);
        buy8.addActionListener(this);
        shopFrame.add(buy9);
        buy9.addActionListener(this);
        shopFrame.add(buy10);
        buy10.addActionListener(this);
        shopFrame.add(exitShop);
        exitShop.addActionListener(this);
        shopFrame.setResizable(false);
        shopFrame.setVisible(false);
    }

    public static void main(String[] args) {
        new MoneyMaker();
    }

    public MoneyMaker() {
        ClickerFrame.setVisible(true);
    }

    public void actionPerformed(ActionEvent arg0) {
        Object control = arg0.getSource();
        if (control == sellItems) {
            MoneyMakerClass.sell();
            moneyLabel.setText("You have: $" + moneyint);
        }
        if (control == openShop) {
            if (itemBeingSold.equals(MoneyMakerClass.item1)) {
            } else if (itemBeingSold.equals(MoneyMakerClass.item2)) {
                buy2.setEnabled(false);
            } else if (itemBeingSold.equals(MoneyMakerClass.item3)) {
                buy2.setEnabled(false);
                buy3.setEnabled(false);
            } else if (itemBeingSold.equals(MoneyMakerClass.item4)) {
                buy2.setEnabled(false);
                buy3.setEnabled(false);
                buy4.setEnabled(false);
            } else if (itemBeingSold.equals(MoneyMakerClass.item5)) {
                buy2.setEnabled(false);
                buy3.setEnabled(false);
                buy4.setEnabled(false);
                buy5.setEnabled(false);
            } else if (itemBeingSold.equals(MoneyMakerClass.item6)) {
                buy2.setEnabled(false);
                buy3.setEnabled(false);
                buy4.setEnabled(false);
                buy5.setEnabled(false);
                buy6.setEnabled(false);
            } else if (itemBeingSold.equals(MoneyMakerClass.item7)) {
                buy2.setEnabled(false);
                buy3.setEnabled(false);
                buy4.setEnabled(false);
                buy5.setEnabled(false);
                buy6.setEnabled(false);
                buy7.setEnabled(false);
            } else if (itemBeingSold.equals(MoneyMakerClass.item8)) {
                buy2.setEnabled(false);
                buy3.setEnabled(false);
                buy4.setEnabled(false);
                buy5.setEnabled(false);
                buy6.setEnabled(false);
                buy7.setEnabled(false);
                buy8.setEnabled(false);
            } else if (itemBeingSold.equals(MoneyMakerClass.item9)) {
                buy2.setEnabled(false);
                buy3.setEnabled(false);
                buy4.setEnabled(false);
                buy5.setEnabled(false);
                buy6.setEnabled(false);
                buy7.setEnabled(false);
                buy8.setEnabled(false);
                buy9.setEnabled(false);
            } else if (itemBeingSold.equals(MoneyMakerClass.item10)) {
                buy2.setEnabled(false);
                buy3.setEnabled(false);
                buy4.setEnabled(false);
                buy5.setEnabled(false);
                buy6.setEnabled(false);
                buy7.setEnabled(false);
                buy8.setEnabled(false);
                buy9.setEnabled(false);
                buy10.setEnabled(false);
            }
            shopFrame.setVisible(true);
        }
        if (control == exit) {
            System.exit(0);
        }
        if (control == exitShop) {
            shopFrame.setVisible(false);
        }
        if (control == buy2) {
            if (moneyint >= 40) {
                moneyint = moneyint - 40;
                itemBeingSold = "newspaper";
                buy2.setEnabled(false);
                JOptionPane.showMessageDialog(shopFrame, "You purchased newspapers!");
                sellLabel.setText("Currently selling: " + itemBeingSold);
                moneyLabel.setText("You have: $" + moneyint);
                shopFrame.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(shopFrame, "Not enough money!");
            }
        }
        if (control == buy3) {
            if (moneyint >= 250) {
                moneyint = moneyint - 250;
                itemBeingSold = "car washes";
                buy3.setEnabled(false);
                JOptionPane.showMessageDialog(shopFrame, "You purchased a car wash!");
                sellLabel.setText("Currently selling: " + itemBeingSold);
                moneyLabel.setText("You have: $" + moneyint);
                shopFrame.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(shopFrame, "Not enough money!");
            }
        }
        if (control == buy4) {
            if (moneyint >= 500) {
                moneyint = moneyint - 500;
                itemBeingSold = "donuts";
                buy4.setEnabled(false);
                JOptionPane.showMessageDialog(shopFrame, "You purchased a donut shop!");
                sellLabel.setText("Currently selling: " + itemBeingSold);
                moneyLabel.setText("You have: $" + moneyint);
                shopFrame.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(shopFrame, "Not enough money!");
            }
            if (control == buy5) {
                if (moneyint >= 800) {
                    moneyint = moneyint - 800;
                    itemBeingSold = "pizza";
                    buy5.setEnabled(false);
                    JOptionPane.showMessageDialog(shopFrame, "You purchased a pizzeria!");
                    sellLabel.setText("Currently selling: " + itemBeingSold);
                    moneyLabel.setText("You have: $" + moneyint);
                    shopFrame.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(shopFrame, "Not enough money!");
                }
            }
            if (control == buy6) {
                if (moneyint >= 1000) {
                    moneyint = moneyint - 1000;
                    itemBeingSold = "furniture";
                    buy6.setEnabled(false);
                    JOptionPane.showMessageDialog(shopFrame, "You purchased a furniture store!");
                    sellLabel.setText("Currently selling: " + itemBeingSold);
                    moneyLabel.setText("You have: $" + moneyint);
                    shopFrame.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(shopFrame, "Not enough money!");
                }
            }
            if (control == buy7) {
                if (moneyint >= 40) {
                    moneyint = moneyint - 2500;
                    itemBeingSold = "cars";
                    buy7.setEnabled(false);
                    JOptionPane.showMessageDialog(shopFrame, "You purchased a car dealer!");
                    sellLabel.setText("Currently selling: " + itemBeingSold);
                    moneyLabel.setText("You have: $" + moneyint);
                    shopFrame.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(shopFrame, "Not enough money!");
                }
            }
            if (control == buy8) {
                if (moneyint >= 15000) {
                    moneyint = moneyint - 15000;
                    itemBeingSold = "organic foods";
                    buy8.setEnabled(false);
                    JOptionPane.showMessageDialog(shopFrame, "You purchased Whole Foods Market!");
                    sellLabel.setText("Currently selling: " + itemBeingSold);
                    moneyLabel.setText("You have: $" + moneyint);
                    shopFrame.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(shopFrame, "Not enough money!");
                }
            }
            if (control == buy9) {
                if (moneyint >= 50000) {
                    moneyint = moneyint - 50000;
                    itemBeingSold = "cyber electronics";
                    buy9.setEnabled(false);
                    JOptionPane.showMessageDialog(shopFrame, "You purchased Silicon Valley!");
                    sellLabel.setText("Currently selling: " + itemBeingSold);
                    moneyLabel.setText("You have: $" + moneyint);
                    shopFrame.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(shopFrame, "Not enough money!");
                }
            }
            if (control == buy10) {
                if (moneyint >= 1000000) {
                    moneyint = moneyint - 1000000;
                    itemBeingSold = "oil & gas";
                    buy10.setEnabled(false);
                    JOptionPane.showMessageDialog(shopFrame, "You purchased an oil & gas company!");
                    sellLabel.setText("Currently selling: " + itemBeingSold);
                    moneyLabel.setText("You have: $" + moneyint);
                    shopFrame.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(shopFrame, "Not enough money!");
                }
            }
        }
    }
}

And one class:

public class MoneyMakerClass {

    static String item1 = "lemonade";
    static String item2 = "newspaper";
    static String item3 = "car washes";
    static String item4 = "donuts";
    static String item5 = "pizza";
    static String item6 = "furniture";
    static String item7 = "cars";
    static String item8 = "organic foods";
    static String item9 = "cyber electronics";
    static String item10 = "oil & gas";

    public static void sell() {
        if (MoneyMaker.itemBeingSold.equals(item1)) {
            MoneyMaker.moneyint = MoneyMaker.moneyint + 1;
        } else if (MoneyMaker.itemBeingSold.equals(item2)) {
            MoneyMaker.moneyint = MoneyMaker.moneyint + 10;
        } else if (MoneyMaker.itemBeingSold.equals(item3)) {
            MoneyMaker.moneyint = MoneyMaker.moneyint + 25;
        } else if (MoneyMaker.itemBeingSold.equals(item4)) {
            MoneyMaker.moneyint = MoneyMaker.moneyint + 40;
        } else if (MoneyMaker.itemBeingSold.equals(item5)) {
            MoneyMaker.moneyint = MoneyMaker.moneyint + 65;
        } else if (MoneyMaker.itemBeingSold.equals(item6)) {
            MoneyMaker.moneyint = MoneyMaker.moneyint + 100;
        } else if (MoneyMaker.itemBeingSold.equals(item7)) {
            MoneyMaker.moneyint = MoneyMaker.moneyint + 550;
        } else if (MoneyMaker.itemBeingSold.equals(item8)) {
            MoneyMaker.moneyint = MoneyMaker.moneyint + 1000;
        } else if (MoneyMaker.itemBeingSold.equals(item9)) {
            MoneyMaker.moneyint = MoneyMaker.moneyint + 10000;
        } else if (MoneyMaker.itemBeingSold.equals(item10)) {
            MoneyMaker.moneyint = MoneyMaker.moneyint + 100000;
        }
    }
}

Any help would be greatly appreciated!

Upvotes: 1

Views: 71

Answers (1)

Robin
Robin

Reputation: 36601

The problem is that your

if (control == buy4) {
...
}

misses the closing bracket. So what you actually have is

if ( control == buy1 ){
 ...
}
...
if ( control == buy4 ){
  if ( control == buy5 ){
   ...
  }
  if ( control == buy6 ){
   ...
  }
}

Correctly closing the if will fix the issue.

Apart from that:

  • You are using way too many static variables. There is no need for that
  • Why not move the creation code in your constructor instead of in an {} block
  • You should wrap your new MoneyMaker(); call in the main method in a SwingUtilities#invokeLater or similar (see Concurrency in Swing tutorial)

Upvotes: 1

Related Questions