Multi-threading in Java. Table of Contents. Improve Article. Save Article. Like Article. Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. Previous Encapsulation in Java. Next Nested Interface in Java. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert.
Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Java. Most visited in School Programming. We use cookies to ensure you have the best browsing experience on our website. Start Your Coding Journey Now! Login Register. Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. Connect and share knowledge within a single location that is structured and easy to search.
I am trying not to pick up any bad habits while improving the way I write programs. Any suggestions on the following lines of code?
If you click the New Game button, you'll get a new window but the old one will still be there. You may want to provide a reset method that will both initialise a random number and also reset the comment labels to their default values. Then you can call reset both from your constructor and also when a game is done.
Or even when a new game is asked for. You can get rid of it at the class level. Try to find repeated parts of code, and think about how you can pull those out to a reusable method. Keep your code DRY. I usually like to make Swing components as self-contained as possible see examples below.
Therefore, for any UI-specific logic such as internal event listening I have the component your JFrame implement the listener interface e. If that triggers some type of application event, I'd fire off that type of event from within the actionPerformed method of the UI component.
The only time I extract an explicit separate class for Swing listeners is when they're generally useful across several UI components, which isn't real often. This helps to isolate the UI as being Swing-based; the rest of your application receives an application-level event and it doesn't need to know if it was from a Swing button, a web button, a service invocation, or whatever. You shouldn't be importing Swing interfaces into non-UI classes.
For trivial Swing applications like this, using inner classes to implement listeners is fine, and indeed it's a common practice. It just seems more clean to me at least to make a self-contained component out of it.
For large Swing applications, however, all those extra classes can eat up your PermGen memory. For convenience, the JFrame. But it's good to remember that you need to deal with the content pane. I also typically localise small-L the configuration of my internal components instead of spreading them out:.
Now I can reuse the button variable name for the next one. Same with labels. After you create a couple of panels or frames, you'll start to notice some common coding patterns you use. Michael Dunn. David Sharpe. I think Michael has already helped with why your program isn't working, so I'm not going to comment on that. However, I do wonder why you're calling "SwingUtilities.
Your program seems to work fine without it. It'll definitely slow you down if you use mysterious functions: "I don't know what it does, but it seems to be working. The API for that method is here , by the way. If you don't understand its description, that's a clue to not use it.
Thank you all for your help! I finally got the program working meaning that it actually recognizes the different numbers. I have only one more problem and I'm hoping someone can lead me in the right direction: I need to post how many guesses the person has attempted and I'm kind of at a loss as to how to do that. Any insight would be great, thanks! Well, you're using "private int number" to keep track of the "secret number," so you need something similar to keep track of the number of guesses.
You're using labels, e. Since " JLabel. The problem I'm having at least I think is I can't figure out where to put the JLabel so that the integer "Guesses" will actually move up. Here's what I have. Constructor portion: import java.
0コメント