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

 public class TestSwingListeners1 {

    private static int cnt1;
    private static int cnt2;
    public static void main(String[] args) {
    JFrame fr1 = new JFrame("Swing Window");
    Container cp;
    JButton bt1;
    JButton bt2;
    cnt1 = 0;
    cnt2 = 0;
    String scr = null;
    String wnr = null;
    JButton btOK, btCancel;
    fr1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fr1.setSize(300, 200);
    fr1.setResizable(false);
    cp = fr1.getContentPane();
    cp.setLayout(new GridLayout(5,1));
    btOK = new JButton("AC Milan");
    btCancel = new JButton("Real Madrid");
    JLabel lbl1 = new JLabel("Result: " + cnt1 + "X" + cnt2);
    JLabel lbl2 = new JLabel("Last Scorer: " + scr);
    JLabel lbl3 = new JLabel("Winner: " + wnr);
    cp.add(btOK);
    cp.add(btCancel);
    cp.add(lbl1);
    cp.add(lbl2);
    cp.add(lbl3);
    //lbl1.setText(displayText);

    btOK.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae) {
        //String displayText = "" + 1;
        cnt1++;     
        }
    });

    btCancel.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae) {
        cnt2++;
        }
    });
    fr1.show(); 
}

When I press the btOK button, I want cnt1 to increment and same with btCancel - increment cnt2 when it's pressed.

How to do it?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Haroyee
  • 11
  • 5

3 Answers3

2

You need to update the label using .setText() like:

btOK.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        cnt1++;
        lbl1.setText("Result: " + cnt1 + " X " + cnt2);
    }
}

And you need to change:

JLabel lbl1 = new JLabel("Result: " + cnt1 + "X" + cnt2);

to

final JLabel lbl1 = new JLabel("Result: " + cnt1 + "X" + cnt2);

so it becomes accessible from your ActionListener that is an inner class.

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • it tells me this : local variable lbl1 is accessed from within inner class; needs to be declared final – Haroyee Jul 21 '11 at 08:12
  • Thank you vary much sir, one more question, about updating these string (scr) for the last scorer and the winner how to update them ? – Haroyee Jul 21 '11 at 08:19
  • it worked thank you,, if I want to make the whole things in the center ,, which layout should I use ?? – Haroyee Jul 21 '11 at 08:36
2

You need to register ActionListener Read More

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Hey Guys, I tried the GridLayout(5,0) but it still in the left, what to do to make it in the center ?? – Haroyee Jul 21 '11 at 10:22
  • 1
    if you have got the answer of your current(this) question . mark the answer that helped you the most as accepted . and since you have another question to post create a new question. – jmj Jul 21 '11 at 10:30
1
public class TestSwingListeners1 implements ActionListener
{

    // ...

    btOK = new JButton("AC Milan");
    btOK.setActionCommand("OK");
    btOKaddActionListener(this);

    btCancel = new JButton("Real Madrid");
    btCancel.setActionCommand("Cancel");
    btCancel.addActionListener(this);

    // ...

    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("OK"))
        {
            lbl1.setText("Result: " + ++cnt1 + "X" + cnt2);
        }
        else if(e.getActionCommand().equals("Cancel"))
        {
            lbl1.setText("Result: " + cnt1 + "X" + ++cnt2);
        }
    }
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417