For a school project, I need to make a simple paint application that can draw lines, ovals, and rectangles.
The assignment specifies that I need toolbar buttons and menu items for each type of shape.
I would like to go a little above and beyond, by making the buttons JToggleButtons
in the toolbar and the menu items JRadioButtonMenuItems
. Furthermore, I want it so that when you select one of the toolbar buttons, it deselects the other ones, selects the appropriate menu item, and deselects the other menu items. Same for selecting one of the menu items.
I know I can group any AbstractButton
with a ButtonGroup
, but I am not sure if this is the right way to go, because though it handles one "group" of buttons just fine, I am not sure it can handle two parallel groups.
Doing it without ButtonGroup
would mean in each of the 6 event listeners I would have to manually deselect the other buttons, and each pair would call the same code to set the shape type.
I could also make two ButtonGroup
s, one for the menu, one for the toolbar, but then I also have to duplicate shape type setting code.
In either situation, I also run the risk of the menu setting a button which sets a menu item which sets a button, ad infintum.
What is the best way to tackle this problem?
(Bonus points for being able to solve the problem with the Netbeans GUI designer; It's just easier)
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…