// *********************************** // OrderPanel.java // // Creates an interface for ordering // objects from a restaurant, using // radio buttons and a JButton. // // *********************************** import javax.swing.*; import java.awt.*; import java.awt.event.*; public class OrderPanel extends JPanel { // creating graphical components private JRadioButton pizza, beverage, mozzSticks, breadSticks, small, medium, large; private JLabel totalPrice; private JButton order; private static int count = 0; private static int total = 0; public static FoodItem orderUp = new FoodItem(); public OrderPanel() { totalPrice = new JLabel("Total "); order = new JButton("Order"); ButtonListener buttonListen = new ButtonListener(); order.addActionListener(buttonListen); // all the items available for order pizza = new JRadioButton("Pizza", true); pizza.setBackground(Color.red); beverage = new JRadioButton("Beverage"); beverage.setBackground(Color.red); mozzSticks = new JRadioButton("Mozzarella Sticks"); mozzSticks.setBackground(Color.red); breadSticks = new JRadioButton("Breadsticks"); breadSticks.setBackground(Color.red); // grouping order items ButtonGroup items = new ButtonGroup(); items.add(pizza); items.add(beverage); items.add(mozzSticks); items.add(breadSticks); // listening ItemListener itemListen = new ItemListener(); pizza.addActionListener(itemListen); beverage.addActionListener(itemListen); mozzSticks.addActionListener(itemListen); breadSticks.addActionListener(itemListen); // size of item to be ordered small = new JRadioButton("Small", true); small.setBackground(Color.red); medium = new JRadioButton("Medium"); medium.setBackground(Color.red); large = new JRadioButton("Large"); large.setBackground(Color.red); SizeListener sizeListen = new SizeListener(); small.addActionListener(sizeListen); medium.addActionListener(sizeListen); large.addActionListener(sizeListen); ButtonGroup sizes = new ButtonGroup(); sizes.add(small); sizes.add(medium); sizes.add(large); // adding all the radio buttons and the order button to the frame add(pizza); add(beverage); add(mozzSticks); add(breadSticks); add(small); add(medium); add(large); add(totalPrice); add(order); setBackground(Color.RED); setPreferredSize(new Dimension (400, 100)); } // listener for items to be ordered private class ItemListener implements ActionListener { public void actionPerformed(ActionEvent event) { Object source = event.getSource(); // sorting out what has been selected if (source == pizza) { orderUp.setItem("Pizza"); } else if (source == beverage) { orderUp.setItem("Beverage"); } else if (source == mozzSticks) { orderUp.setItem("Mozzarella Sticks"); } else if (source == breadSticks) { orderUp.setItem("Breadsticks"); } } } // listener for sizes private class SizeListener implements ActionListener { public void actionPerformed(ActionEvent event) { Object source = event.getSource(); // sorting out size selected if (source == small) { orderUp.setSize("Small"); } else if (source == medium) { orderUp.setSize("Medium"); } else if (source == large) { orderUp.setSize("Large"); } } } // listener for the Order button private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { int tempPrice = 0; // sets prices according to what the radio buttons have selected if (orderUp.getItem().equals("Pizza")) { if (orderUp.getSize().equals("Small")) { tempPrice = 7; } else if (orderUp.getSize().equals("Medium")) { tempPrice = 10; } else if (orderUp.getSize().equals("Large")) { tempPrice = 15; } } else if (orderUp.equals("Beverage")) { if (orderUp.getSize().equals("Small")) { tempPrice = 2; } else if (orderUp.getSize().equals("Medium")) { tempPrice = 3; } else if (orderUp.getSize().equals("Large")) { tempPrice = 4; } } else if (orderUp.equals("Mozzarella Sticks")) { if (orderUp.getSize().equals("Small")) { tempPrice = 5; } else if (orderUp.getSize().equals("Medium")) { tempPrice = 7; } else if (orderUp.getSize().equals("Large")) { tempPrice = 9; } } else if (orderUp.equals("Breadsticks")) { if (orderUp.getSize().equals("Small")) { tempPrice = 4; } else if (orderUp.getSize().equals("Medium")) { tempPrice = 6; } else if (orderUp.getSize().equals("Large")) { tempPrice = 8; } } // adds the price of the individual order to the total cost of the order total += tempPrice; totalPrice.setText("Total: " + total); count++; } } }
Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

Problems passing an object through private classes
Started by 9erNumber16, Feb 25 2011 09:01 PM
setpreferredsize
1 reply to this topic
#1
Posted 25 February 2011 - 09:01 PM
I keep getting a Null exception after trying to submit data entered by radio buttons. It appears the setters aren't working in my radio button listeners, and I'm not sure why.
#2
Posted 26 February 2011 - 12:30 AM
Never mind, figured it out.
Also tagged with one or more of these keywords: setpreferredsize
Language Forums →
Java →
Grid With MysqlStarted by Apprentice123, 30 Jul 2012 ![]() |
|
![]() |
||
Language Forums →
Java →
Really Easy Paintcomponent QuestionStarted by Cruel Hand, 07 Jul 2012 ![]() |
|
![]() |
||
Language Forums →
Java →
[Solved] Bouncing Ball HelpStarted by Cruel Hand, 17 Jun 2012 ![]() |
|
![]() |
||
Language Forums →
Java →
[Solved]Help Plotting A Function On A GraphStarted by Cruel Hand, 07 Jun 2012 ![]() |
|
![]() |
||
Language Forums →
Java →
Bar Graph HelpStarted by Cruel Hand, 06 Jun 2012 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download