I\'m making a game, and I have a Main Menu that works perfectly. When I select one of the options, it brings up another Menu in a new window. However in this new window, t
If you've searched on this problem at all, you'll see that it almost always means that the component being listened to doesn't have focus. 90% of the time the solution is to use Key Bindings.
Your other problem is that you're comparing Strings ==
. You don't want to do this. Use the equals or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of
if (fu == "bar") {
// do something
}
do,
if (fu.equals("bar")) {
// do something
}
or,
if (fu.equalsIgnoreCase("bar")) {
// do something
}
You're also
paint(...)
directly, something you should almost never do. paint(...)
method which you also should avoid instead of drawing in a JPanel's (or other JComponent) paintComponent(...)
method.You will want to go through the Swing tutorials before going much further to learn from the pros.