You are not logged in.

#1 2005-05-19 17:42:40

Registered: 2005-02-07
Posts: 25

Java Help on buttons with counter

public Keypad(Display myMonitor)
        // initialise instance variables
        monitor = myMonitor;
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(5,4));
        String [] buttonLabels={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve",
        JButton [] buttons = new JButton[buttonLabels.length];
        for(int i = 0; i < buttonLabels.length; i++)
            buttons[i] = new JButton( buttonLabels[ i ] );
            setFont(new Font("SansSerif",Font.BOLD,18));

I need help on using the buttons to have a counter for example when someone clicks  the button "One" 5 times it display something like this : [5] One. So far the buttons work, but I cannot get a counter associate with them. Big Help is extremely needed.  Also the buttons need to be reset to zero; I dont want the counter to keep increasing forever.

Thanks Cheikh


#2 2005-05-19 21:59:15

Registered: 2004-05-25
Posts: 49

Re: Java Help on buttons with counter

You should create two new integer variables.

count and lastPressed

let lastPressed be set to a different numeric value for each button you have (button1 = 0, button2 = 1, and etc)

you set lastPressed each time a button is pressed (in the handler) to its specific associated value, and then set count to 1. If lastPressed is = to the integer button index of the current button, then you pressed the button last time, so we increment count (count++).

I hope this helps. I might not be explaining it well enough.


if( lastPressed == 0 )
else // we're in button 0, and lastPressed != to it.
        count = 1;
lastPressed = 0;
// handle other stuff ,and return


Board footer

Powered by FluxBB