Ok, i have been working on a simplistic game for about an hour now. This is what i have so far:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Game extends JApplet
{
    private int APPLET_WIDTH = 500, APPLET_HEIGHT= 100;
    private int money;
    private int owned;
    private JLabel labelMoney;
    private JLabel labelOwned;
    private JButton Buy;
    private JButton Sell;
    private int cost = 100;
    private int sellPrice = 50;

    public void init ()
    {
    money = 500;
    owned = 1;

    Buy = new JButton ("Buy");
    Buy.addActionListener (new BuyButtonListener());

    Sell = new JButton ("Sell");
    Sell.addActionListener (new SellButtonListener());

    labelMoney = new JLabel ("Money: " + Integer.toString (money));
    labelOwned = new JLabel ("Owned: " + Integer.toString (owned));

    Container cp = getContentPane();
    cp.setBackground (Color.lightGray);
    cp.setLayout (new FlowLayout());
    cp.add (Buy);
    cp.add (Sell);
    cp.add (labelMoney);
    cp.add (labelOwned);

    setSize (APPLET_WIDTH, APPLET_HEIGHT);
}
    private class BuyButtonListener implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
            money -= cost;
            ++owned;
            labelMoney.setText ("Money: " + Integer.toString (money));
            labelOwned.setText ("Owned: " + Integer.toString (owned));
                if(money >= cost)
                {
                    Buy.setEnabled (true);
                }
            repaint ();
                if(money < cost)
                {
                    Buy.setEnabled (false);
                }
            repaint ();
        }
    }
    private class SellButtonListener implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
            money += sellPrice;
            --owned;
            labelMoney.setText ("Money: " + Integer.toString (money));
            labelOwned.setText ("Owned: " + Integer.toString (owned));
                if(owned >= 0)
                {
                    Sell.setEnabled (true);
                }
            repaint ();
                if(owned <= 0)
                {
                    Sell.setEnabled (false);
                }
            repaint ();
        }
    }
}

Basically, as you buy and sell these objects i want the buy and sell buttons disabling and enabling depending on whether you have any objects or not. at this point the buttons will disable, but never re-enable themselves.

I have tried If else for the if statements i have in, but they did not work either. So i tried separate if statements with a repaint (); between them, but no luck.

You don't need to repaint() for setting buttons enabled or disabled.

if(owned >= 0)
{
Sell.setEnabled (true);
}

if(owned <= 0)
{
Sell.setEnabled (false);
}

You need to consider the case when owned is equal to 0. If owned = 0, then both of your if statements are going to get executed which might be a source of error. Is it?

Hmm, that was originally

if(owned > 0)
{
Sell.setEnabled (true);
}
 
if(owned < 1)
{
Sell.setEnabled (false);
}

Even now that i have changed it the buttons still do not re-enable.

An update to the program, i have added a "Job" button, when you click it you are granted money, rather than starting with cash.

If you click the Job button you must wait ten seconds to click it again, and there is a timer that shows how much time until the next job is available.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;

public class VoteCounter extends JApplet
{
    private int APPLET_WIDTH = 500, APPLET_HEIGHT= 100;
    private int money;
    private int owned;
    private JLabel labelMoney;
    private JLabel labelOwned;
    private JLabel labelJobs;
    private JLabel labelTime;
    private JButton Buy;
    private JButton Sell;
    private JButton DoJob;
    private int cost = 100;
    private int sellPrice = 50;
    private int jobs = 15;
    private int delay = 1000;
    private int time = 10;

    public void init ()
    {
    money = 0;
    owned = 1;

    Buy = new JButton ("Buy");
    Buy.addActionListener (new BuyButtonListener());

    Sell = new JButton ("Sell");
    Sell.addActionListener (new SellButtonListener());
    
    DoJob = new JButton ("Do Job");
    DoJob.addActionListener (new DoJobButtonListener());

    labelMoney = new JLabel ("Money: " + Integer.toString (money));
    labelOwned = new JLabel ("Owned: " + Integer.toString (owned));
    labelJobs = new JLabel ("Jobs Left: " + Integer.toString (jobs));
    labelTime = new JLabel ("Time until next job: " + Integer.toString (time));

    Container cp = getContentPane();
    cp.setBackground (Color.lightGray);
    cp.setLayout (new FlowLayout());
    cp.add (Buy);
    cp.add (Sell);
    cp.add (DoJob);
    cp.add (labelMoney);
    cp.add (labelOwned);
    cp.add (labelJobs);
    cp.add (labelTime);
    setSize (APPLET_WIDTH, APPLET_HEIGHT);
}

    private class BuyButtonListener implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
            money -= cost;
            ++owned;
            labelMoney.setText ("Money: " + Integer.toString (money));
            labelOwned.setText ("Owned: " + Integer.toString (owned));
                if(money >= cost)
                {
                    Buy.setEnabled (true);
                }
            repaint ();
                if(money < cost)
                {
                    Buy.setEnabled (false);
                }
            repaint ();
        }
    }
    private class SellButtonListener implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
            money += sellPrice;
            --owned;
            labelMoney.setText ("Money: " + Integer.toString (money));
            labelOwned.setText ("Owned: " + Integer.toString (owned));
                if(owned > 0)
                {
                    Sell.setEnabled (true);
                }
            repaint ();
                if(owned < 1)
                {
                    Sell.setEnabled (false);
                }
            repaint ();
        }
    }
    private class DoJobButtonListener implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
            new Timer(delay, taskPerformer).start();
            money += 100;
            --jobs;
            labelMoney.setText ("Money: " + Integer.toString (money));
            labelJobs.setText ("Jobs Left: " + Integer.toString (jobs));
            DoJob.setEnabled (false);
            repaint ();
        }
    }
    ActionListener taskPerformer = new ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {
            --time;
            labelTime.setText ("Time until next job: " + Integer.toString(time));
            repaint ();
            if(time == 0)
            {
                time += 10;
                DoJob.setEnabled (true);
            }
        }
    };
}

Some bugs include:
+Each time the Job button is pressed the timer goes down once per second faster.

+The buy and sell buttons still will not re-enable

+Your money, and how many objects you own can still go negative

+The timer never stops

If anyone has any comments to make about how i did things, or possible fixes for the bugs, any comments are welcome.

Don't create a NEW timer every time in DoJobButtonListener. Your timer does not speed up. Your TIMERS (plural) EACH fire once a second or whatever. Ten timers, each firing once a second, means a count from 10 to 0 in one second. You need ONE timer, created during initialization. If you want to stop, start, restart, change the delay, etc., use one of the many functions the Timer class provides, often with those exact names.

http://java.sun.com/javase/6/docs/api/javax/swing/Timer.html

Regarding allowing variables to go negative, make sure you INITIALIZE the buttons as enabled or disabled properly. You start with no money, so "Buy" shouldn't be enabled to begin with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.