0

I am learning Java. I am painstakingly learning the fundamentals of Java GUI. I made a "counter" program, which increments a variable by 1 every time you click a button.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
    
public class guitry implements ActionListener {
    int counter = 0;

    public guitry() {
        JFrame frame = new JFrame("FrameDemo");
        JPanel panel = new JPanel(new FlowLayout(1));
        JLabel label = new JLabel("Clicked " + counter + " times");
        JButton button = new JButton("CLick me");
        panel.add(button);
        panel.add(label);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
        System.out.println(panel.getLayout());
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        counter++;
        System.out.println("clicked");
        label.setText("Clicked " + counter + " times");
    }

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

}

This code fails by "cannot find symbol" at line 27 in the action listener.

label.setText("Clicked " + counter + " times");

After stumbling upon an example that approaches the same task as me, it does this in the declarations so the action listener works:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
    
public class guitry implements ActionListener {
    int counter = 0;
    JLabel label; <----------------------------------------

    public guitry() {
        JFrame frame = new JFrame("FrameDemo");
        JPanel panel = new JPanel(new FlowLayout(1));
        label = new JLabel("Clicked " + counter + " times"); <----------------------
        JButton button = new JButton("CLick me");
        panel.add(button);
        panel.add(label);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
        System.out.println(panel.getLayout());
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        counter++;
        System.out.println("clicked");
        label.setText("Clicked " + counter + " times");
    }

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

}

I am absolutely gobsmacked as to why this works and am very lost. Thanks in advance for any answers.

jhamon
  • 3,453
  • 3
  • 25
  • 37
  • 1
    you should read a tutorial/course about variable scope. – jhamon Jan 27 '21 at 08:52
  • 3
    When you declare something inside of a method it is only visible inside of that method. If you declare `label` inside of `guitry()` method it won't be visible inside of `actionPerformed` method. – Amongalen Jan 27 '21 at 08:52
  • Wow, can't believe I thought it was something more complicated. Thanks guys I'm gonna close this now. – Inquisitive Jan 27 '21 at 08:56
  • You already figured that you need to make *counter* a **field** of your class, because it is supposed to be used in more than one of the methods. Why are you now surprised that **label** (that is used in two methods as well), has to follow the same rules?! – GhostCat Jan 27 '21 at 08:57
  • 1
    @Inquisitive You can use an IDE. It will give you some ideas how you have to correct errors. – Reporter Jan 27 '21 at 09:05

0 Answers0