I have trying for a while now to access a variable in my main class:
public class Results extends JFrame {
public static void main(String[] args)
{
System.out.println(doble);
}}
which is inside an actionlistener, like this
public Results ()
{
// Create a JPanel for the buttons DOUBLE AND NOT DOUBLE
JPanel duplicate = new JPanel(
new FlowLayout(FlowLayout.CENTER));
JButton doblebutton = new JButton("DOUBLE");
doblebutton.addActionListener(new ActionListener(){
private int doble;
public void actionPerformed(ActionEvent ae){
doble++;
System.out.println("Doubles: " + doble);
}
});
}
I have tried like 5 ways to do it, but it doesn’t seem possible. Any thoughts?
Try moving your declaration of doble outside of the constructor so that it becomes a field, like this:
Some comments:
Hope this helps.