r/learnjava • u/Elifire12 • Dec 09 '20
I NEED HELP WITH MY CODE
So I was trying to code a small game with a simple gui. Don't worry about what the game is supposed to be about, I didn't even get close to that. I got stuck when trying to create a JButton that increased a counter... Here is my code:
import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class Window implements ActionListener {
private static JLabel moneyCounter;
long count = 0;
public static void window() {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Game");
frame.setLayout(null);
JButton button = new JButton("Button");
button.setSize(100, 30);
button.setBackground(Color.black);
button.setLocation(200, 235);
button.setVisible(true);
frame.add(button);
button.addActionListener((ActionListener)this);
moneyCounter = new JLabel("0");
moneyCounter.setSize(100, 30);
moneyCounter.setHorizontalAlignment(SwingConstants.CENTER);
moneyCounter.setLocation(200, 135);
frame.add(moneyCounter);
moneyCounter.setVisible(true);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
count++;
moneyCounter.setText(String.valueOf(count));
}}
This is the error message that I get: java: non-static variable this cannot be referenced from a static context
Also, I know what the problem is, and I know what I am doing wrong and why it doesn't work, but I don't know how to fix it! I can't just make the method a non-static method, because I have to access that method from the main method, and that wouldn't work if they weren't both static, and the main method is always static, so this one has to be static too, right? How can I fix this? Thanks in advance :)
1
u/Anowv Dec 09 '20
I haven't coded in Java in a while, so forgive me, but - do you need to instantiate the moneyCounter var with the JLabel preceding it like...JLabel moneyCounter = new JLabel("0");
Maybe that doesn't matter, I can't remember.
However, hopefully this next explanation makes sense.
You won't be manipulating variables from your class, you'll be manipulating variables from an instance OF your class. So you don't have access to count but you'd have access to moneyCounter.count as that's the instance/object you're working with.
This is similar to like
class Car{
int wheels = 4;
}
Car Volvo = new Car;
Volvo.wheels = 5;
System.out.print(wheels); // gives an error
System.out.print(Volvo.wheels); // 51
Dec 09 '20
[removed] — view removed comment
1
u/Elifire12 Dec 09 '20
I can't. I have to access it in the main method later, so that wouldn't work. Unless you know a way to access a non static method (window()) from a static method (main())... Do you know if that is possible?
1
u/Farpafraf Dec 09 '20
just put all the code that you'd put in main in a method of an object ex: App.start() and then call new App.start() in your main instead. In general leave the code outside of main
1
u/Elifire12 Dec 09 '20
I didn't really think about that. But what if I want to use different classes for everything to make my code easier to update later. I mean let's say my code is extremely long. Like super long. I am talking about stuff like thousands of lines. I can't just put it all in one class. What am I gonna do when I want to find the exact location of something to correct/improve it? I really wanted to use classes for practicing that, but if there is no other way, I guess I am going to have to do as you said.
2
u/Farpafraf Dec 09 '20
no wait that doesn't imply that: the same exact structure you would have used in main you could use in App.start() but now you are obviously also able to access the non static methods of the App class inside start().
Classes should be small: if that many lines are present in a single one you likely have a structural problem, imho you should start with something easier
1
u/Elifire12 Dec 09 '20
ooooohh now I understand what you mean... that is actually great, I think that just completely solved my problem! Thank you very much!
1
u/needrefactored Dec 09 '20 edited Dec 09 '20
You instantiate a Screen Object in your main method. This allows you to have access to the methods in that class from a static method.
So in your screen class, make your methods non-static. Then in your main method, do like Screen screen = new Screen();
Then you can call on the window method. screen.window()
This is the basic concept of Object oriented design. :)
1
Dec 09 '20
[removed] — view removed comment
1
u/Elifire12 Dec 09 '20
I am a newbie when it comes to coding. Could you please explain to me how that would work?
2
Dec 09 '20
[removed] — view removed comment
2
u/Elifire12 Dec 09 '20
I am not like a complete beginner, I have been coding for like 2 months, but maybe you're right, I am gonna put this project aside, and try to code something similar in the console. I have a question... to make something similar to that in the console, is there a way to have something like some sort of timer, constantly running in the background, that is just counting upwards every second, and I can just do other stuff in the meantime in the program. Is that possible in any way?
2
u/hadoken4555 Dec 09 '20
I think it your count variable. You are trying to access it inside your method, you might need to declare it static. Hope that help. Am not a java expert.