You are not logged in.
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",
"Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen","Twenty"};
JButton [] buttons = new JButton[buttonLabels.length];
for(int i = 0; i < buttonLabels.length; i++)
{
buttons[i] = new JButton( buttonLabels[ i ] );
buttons[i].addActionListener(this);
p.add(buttons[i]);
setFont(new Font("SansSerif",Font.BOLD,18));
}
add("Center",p);
}
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
Offline
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.
example:
if( lastPressed == 0 )
{
count++
}
else // we're in button 0, and lastPressed != to it.
{
count = 1;
}
lastPressed = 0;
// handle other stuff ,and return
Offline