1

Hello I have been stuck on how to make a button display a random number.this is where I am right now. I cant figure out where the random number generator code would go. If I put it before the ActionListener if will just post right beside the button instead of appearing when the button is pressed. It keeps giving me the error message

Error: Cannot refer to the non-final local variable num1 defined in an enclosing scope.

Please refer the below code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.util.Random;

public class myTest {

    public static void main(String[] args) {

        Random generator = new Random();
        int num1;

        final JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        num1 = generator.nextInt(101);
        System.out.println("the random number is:" +num1);

          JButton button1 = new JButton("Push Me!");

        frame.add(panel);
        panel.add(button1);
        frame.setVisible(true);

        button1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
           num1 = generator.nextInt(101);
        System.out.println("the random number is:" +num1);
            }
        });

    }

}
Vishal
  • 549
  • 1
  • 4
  • 21
  • 1
    Possible duplicate of [Cannot refer to a non-final variable inside an inner class defined in a different method](http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-differen) – Pradeep Simha Oct 07 '15 at 03:54

2 Answers2

2

You can call setText on the JButton. Also, don't forget to set a default close operation. You can't reference num1 from the outer scope. I think you wanted something like

Random generator = new Random();
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();

int num1 = generator.nextInt(101);
System.out.println("the random number is:" + num1);

JButton button1 = new JButton(String.format("Push Me! %d", num1));
frame.add(panel);
panel.add(button1);
frame.pack();
frame.setVisible(true);

button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        int num1 = generator.nextInt(101);
        System.out.println("the random number is:" + num1);
        button1.setText(String.format("Push Me! %d", num1));
    }
});
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

The error relates to the fact that num1 is declared as a local variable with the context of main, but which may be changed at some point in the future.

Have a look at Referencing non-final variable: why does this code compile? for more of an explanation

Instead, since it's not need until the ActionListener is called, just make it available to the ActionListener itself

button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        int num1 = generator.nextInt(101);
        System.out.println("the random number is:" + num1);
    }
});
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366