JAVA I tried to write this homework myself, but I have made a mess. I was able t
ID: 3647290 • Letter: J
Question
JAVA I tried to write this homework myself, but I have made a mess. I was able to build the frame and buttons, but getting them to actually work is my problem. I can't resolve the event listener. Maybe someone can just show me?Pizza class that extends JFrame
two radio buttons, in a group, with Crust title and border (thick, thin)
three check boxes, with Topppings title and border (pepperoni, mushroom, olives)
two JButtons (Order, Cancel)
main panel
grouped buttons instantiated and linked to panel
instantiate and add the JButtons to main panel
set visible frame
In the frame code an inner class event listener that implements the interface ActionListener when user clicks any buttons. Instantiate an object of this inner and register it to both JButtons.
When Order button pushed, it checks to make sure a Crust was chosen.
{
If thick or thin was pushed, use JOptionPane to display which crust was chosen and any/all/none topppings checkboxes chosen (all these options need to be messaged). It accepts the Order and clears all buttons of activity.
If a crust radio button was NOT pushed, use JOptionPane to say "try again". It does not accept the choice and clears all buttons of activity, ready for the next attempt.
}
When JButton Cancel is pushed, the JOptionPane displays total pizzas sold for the day, and Thick <number sold>, Thin <number sold>
Then is says "Enjoy your pizza" and ends.
The test application needs to be create an object of the frame (Pizza Store) and display it. The inner class should work independently of the test application.
graded on:
*ONLY ONE crust can be chosen at a time
*more than one toppping can be chosen
*when Order is pushed the JOptionPane throws error message if no crust chosen
*system cleans all boxes
*system gives order summary with JOptionPane and accepts pizza order if one crust was chosen
*system cleans all boxes
*when Cancel is pushed the JOptionPane gives order summary and then ends application
Explanation / Answer
well here is the pattern of putting event listener in an inner class An inner class is a class that is declared within another class: public class SimpleCalc { private class OperatorAction{ } } or example for you public class SimpleCalc{ public SimpleCalc() { //rest of code.. JButton addButton = new JButton("+"); addButton.setActionCommand("+"); OperatorAction subAction = new OperatorAction(1); addButton.addActionListener(subAction); JButton subButton = new JButton("-"); subButton.setActionCommand("-"); OperatorAction addAction = new OperatorAction(2); subButton.addActionListener(addAction); //rest of code.. } private class OperatorAction implements ActionListener { private int operator; public OperatorAction(int operation) { operator = operation; } public void actionPerformed(ActionEvent event) { currentCalc = Integer.parseInt(numberCalc.getText()); calcOperation = operator; } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.