Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
my java program won't run?
it compiles, it just doesn't run. I type the command "java TTT" in the command line and it just sits there and does absolutely nothing. I think my while loop is still infinite- I was having the same issue a few days ago but can't get rid of it. I set pretty much the entire frame properties inside the while loop so it wouldn't be infinite. Any help?
in line 25, after I call the frame to open, I set frame.setVisible( true );
3 Answers
- tLv 59 years agoFavorite Answer
I rewrote your code. Please don't take the changes personally. I rewrote it in my style, which you don't have to like, so just take what you do like and tweak it until it works for you.
Please try to learn form it as well; copy-pasting is easy, but not good in the long term. If you read the code you receive from other people and understand it, you will become a better programmer.
Some of the larger changes:
- Major rearranging of the code's structure.
Almost all of the code was originally inside the method changeBkColor(), and there was one heavily abused class which chose to remain anonymous. In the future, try to break your program's logic into smaller methods which do no more and no less than their name suggests. And if your anonymous classes get so large, you might as well give them a name.
- Got rid of the seventy-odd lines of hard-coded hell.
If you ever find yourself writing something more than once, there is almost certainly a better way of doing it. Make a method, use a loop, and never say the same thing twice unless you can't avoid it. If it's not compromising elegance or clarity, be lazy – hard coding is only for people who don't know any better or are too lazy to be lazy.
I'll be more than happy to answer any questions you have about my code and fix any bugs that might crop up. Either email me or add 'Additional Details' to the question.
- green meklarLv 79 years ago
The first thing I notice is that no GUI elements ever appeared on the screen, and that the program sucks up 272 megabytes of memory over a span of time after it starts (this is quite a lot of memory and I suspect you didn't intend to use that much). Now let's take a look at the code and see what's going on here...
Okay, it does appear that that while loop is spinning infinitely. Um...I'm not entirely sure why you expected anything else to happen. When is the while loop supposed to exit? For that matter, it looks to me as if the contents of the while loop merely set up the GUI, so why is that code in a while loop at all? Under what conditions would you want the GUI to be set up multiple times in a row?
I also noticed that the condition of the while loop (go) is the negation of the condition of the if statement following it (!go). Unless some sort of multithreaded behavior were modifying the value of go at unpredictable times, it seems that, if the while loop is correct, then the if statement is redundant. Is that what you intended? You might want to think over your program logic and how you're trying to implement it here.
- Link HLv 69 years ago
You need to set the visible property in the frame. For some queer reason, the default is not-visible.