This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Ok-I am doing homework out of a book and the instructions are to display an interface with 5 option buttons in a frame. When clicked, each button changes the background color of the frame. It proceeds to tell me to construct CheckboxGroup, use FlowLayaout and add the Checkboxes to the frame along with ItemListener. It says addWindowListener()method, write code for itemStateChanged() which uses the getState() method and nested if statements. --I am so stuck. I hate this book, it does not have any example close to this as to how to do it and I am so so stuck-PLEASE HELP!
Okay, the first thing you want to do is get this to compile. If you keep adding things while there are still errors, you will only compound the problems.
The biggest issue is that your code is mostly inside the static method main, and you're trying to call non-static methods from that context. Remember, in a static (class) context, you can't call non-static (instance) members because there's no way to know which instance you might mean (or even whether an instance exists). You're also trying to reference the instance "this," which (for the same reason) does not exist in a static context.
So my first advice is to move that code outside of main. What you have so far is basically getting a ColorButtons instance ready for use, so a reasonable (or at least convenient) place to put the code might be inside a ColorButtons constructor. Then you could simply use main to call the constructor.
The next issue preventing this from compiling is that your class says it implements WindowListener and ItemListener, but it's not providing implementation for the methods in those interfaces. My advice here is to remove the WindowListener for now, and just work on implementing the ItemListener. Check the API documentation to see what method needs to be implemented for this interface.
Then, when this compiles and you're ready to move forward, go one step at a time. Add a line or two, then recompile to make sure there are no errors. Don't keep adding things until the errors are resolved.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Oct 17, 2006
Thanks, still struggling-all of this is like jibberish to me. Do I need to import javax swing and JFrame for this?
The upper left frame lists packages. The default is "All Classes," which is probably best for now.
The lower left frame lists the contents of the package selected in the upper left frame. (Again, "All Classes" is good.)
The large frame on the right displays what's selected in the lower left frame.
So, for example, clicking on "JFrame" in the lower left frame displays the documentation for JFrame. At the top, you will see that JFrame is a class in the javax.swing package. So if you wanted to use JFrame, you would need to import javax.swing.JFrame.