GUI Program Errors In Java

Hello All, I am working on a project and In my online Java class, I need to write a program that counts the number of mouse clicks on a button within a frame. Here is my code which I run on Interviewbit compiler:

import java.awt.*;
import java.awt.event.*;

public class option1 extends Frame {
    option1() {
        setTitle("Final Project Option 1");
        setSize(300,300);
        show();
    }
    public static void main(String[] args) {
        option1 test = new option1();

        int a = 0;
        String s1 = "" + a;

        Frame objFrame;
        Button objButton1;
        Label objLabel1;

        objFrame = new option1();
        objButton1 = new Button("Button");
        objLabel1 = new Label();

        objLabel1.setBounds(150,220,50,30);
        objButton1.setBounds(40,35,50,50);

        objLabel1.setText(s1);

        objButton1.addMouseListener(new MyMouseListener()); //line 29

        objFrame.add(objLabel1);
        objFrame.add(objButton1);
    }
    public class MyMouseListener extends MouseAdapter {
        public void mouseClicked(MouseEvent me) {
            a++; //line 36
        }
    }
}

While aggregating, I get two mistakes. One blunder is on line 29, which is “non-static variable this can’t be referred to from a static setting”, and the other is on line 36, which is “can’t track down image”.

So, what exactly am I doing wrong? I would appreciate responders telling exactly what I need to do to fix the problem, and avoiding using technical terms since I’m rather new to programming. Can anyone suggest me?

…well the first thing you’re doing wrong is asking for help with Java on the Unity forums.

Interviewbit doesn’t let me rename its file and complains about wrong name. I’m not going spend my time trying to figure how to wrestle this thing into submission. But.

In your code, on line 36 you’re trying to access “a” variable from a place where it does not exist. That’s why compilation fails at line 36. Your “a” exists only within void main(). You’re trying to access it from outside of void main() and on top of that from a very different class. Of course it is not going to fly. A variable is only accessible within the scope where it was declared.

At the moment it is possible that you do not “quite” understand what you’re doing, and the code is copy-pasted. You’ll need to take a step back, and start over from the beginning. If you have a book about java, with exercises, which starts with console “hello world” application, use that. Start from the first chapter, do the exercises.

Have fun.

Technically dealing with the mess called java plugins is occasionally a part of unity android experience but you’re correct this particular exercise is not related to unity and it is a wrong forum.

To explain a little more why this Q is bad for this site, Unity3D is a game engine using C#, not Java. It looks as if people ask normal programming questions, but they’re really about how to get their C# code to interact with Unity’s special set-up and API’s, plus how to program “game” stuff like jumping.

Obviously, C# and Java are similar languages, but they’re still different and many C# game coders won’t know the small important differences even if they wanted to help you here.

Others have already explained why this doesn’t belong here. Locking.