Advertisement
Guest User

CoffeePanel

a guest
Jul 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. /**
  5. The CoffeePanel class allows the user to select coffee.
  6. */
  7.  
  8. public class CoffeePanel extends JPanel
  9. {
  10. // The following constants are used to indicate
  11. // the cost of coffee.
  12. public final double NO_COFFEE = 0.0;
  13. public final double REGULAR_COFFEE = 1.25;
  14. public final double DECAF_COFFEE = 1.25;
  15. public final double CAPPUCCINO = 2.00;
  16.  
  17. private JRadioButton noCoffee; // To select no coffee
  18. private JRadioButton regularCoffee; // To select regular coffee
  19. private JRadioButton decafCoffee; // To select decaf
  20. private JRadioButton cappuccino; // To select cappuccino
  21. private ButtonGroup bg; // Radio button group
  22.  
  23. /**
  24. Constructor
  25. */
  26.  
  27. public CoffeePanel()
  28. {
  29. // Create a GridLayout manager with
  30. // four rows and one column.
  31. setLayout(new GridLayout(4, 1));
  32.  
  33. // Create the radio buttons.
  34. noCoffee = new JRadioButton("None");
  35. regularCoffee = new JRadioButton("Regular coffee", true);
  36. decafCoffee = new JRadioButton("Decaf coffee");
  37. cappuccino = new JRadioButton("Cappuccino");
  38.  
  39. // Group the radio buttons.
  40. bg = new ButtonGroup();
  41. bg.add(noCoffee);
  42. bg.add(regularCoffee);
  43. bg.add(decafCoffee);
  44. bg.add(cappuccino);
  45.  
  46. // Add a border around the panel.
  47. setBorder(BorderFactory.createTitledBorder("Coffee"));
  48.  
  49. // Add the radio buttons to the panel.
  50. add(noCoffee);
  51. add(regularCoffee);
  52. add(decafCoffee);
  53. add(cappuccino);
  54. }
  55.  
  56. /**
  57. getCoffeeCost method
  58. @return The cost of the selected coffee.
  59. */
  60.  
  61. public double getCoffeeCost()
  62. {
  63. double coffeeCost = 0.0;
  64.  
  65. if (noCoffee.isSelected())
  66. coffeeCost = NO_COFFEE;
  67. else if (regularCoffee.isSelected())
  68. coffeeCost = REGULAR_COFFEE;
  69. else if (decafCoffee.isSelected())
  70. coffeeCost = DECAF_COFFEE;
  71. else if (cappuccino.isSelected())
  72. coffeeCost = CAPPUCCINO;
  73.  
  74. return coffeeCost;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement