"Firstly, add ActionListeners to each of the JMenuItem objects. For example, when you select “Square” from the menu, the ActionListener attached to the ‘Square’ menu item must create an instance of your ‘Square’ class. These ActionListeners will be added as code within the constructor of your MyFrame class."
Could someone please walk me through how to do this?
Once the square is selected i need to then use my Jslider to some how work the area of the square, (given that the jslider value is one side of the square)
Seeing as though this looks like homework, I won't give you the specifics, but just a guide...
Following through your instructions, you'll first need to create a class that implements ActionListener
, and implement the 'actionPerformed()' method as described in the documentation at http://docs.oracle.com/javase/6/docs/api/java/awt/event/ActionListener.html
Then you're instructed that this should detect what was clicked, and then create the appropriate Object
. So, as per the Square instruction of your question, your actionPerformed()
method would do something like this...
JMenuItem clickedMenu = (JMenuItem)e.getSource();
if (clickedMenu.getText().equals("Square")){
Square square = new Square();
}
You would need to add additional if-else
statements for the different menu items.
Finally, it says that you need to add the ActionListener
s to your MyFrame
class, so it should be something like this...
JMenuItem menuItem = new JMenuItem("Square");
menuItem.addActionListener(new MyActionListener());
Its just a matter of following through your instructions 1 piece at a time. If you get stuck at any point, try referring to the Java API documentation, or search for help here on StackOverflow.