fork download
  1. import javax.swing.*;
  2. import javax.swing.border.EmptyBorder;
  3. import javax.swing.border.EtchedBorder;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.util.HashMap;
  8. import java.util.LinkedHashMap;
  9.  
  10. public class Ramen extends JFrame implements ActionListener {
  11.  
  12. private static final String TASTE_SYOYU = "醤油";
  13. private static final String TASTE_MISO = "味噌";
  14. private static final String TASTE_SIO = "塩";
  15. private static final String TOPPING_PORK = "チャーシュー";
  16. private static final String TOPPING_NEGI = "ネギ";
  17. private static final String TOPPING_MENMA = "メンマ";
  18.  
  19. private HashMap<String, AbstractButton> tastesAndToppings;
  20. private HashMap<String, Integer> prices;
  21. private JLabel fee, result, unit;
  22.  
  23. public static void main(String[] args) {
  24. Ramen ramen = new Ramen("ラーメンの代金");
  25. ramen.setVisible(true);
  26. }
  27.  
  28. Ramen(String title) {
  29. setTitle(title);
  30. setSize(350, 200);
  31. setLocationRelativeTo(null);
  32. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33.  
  34. JPanel panel = new JPanel();
  35. GridLayout layout = new GridLayout(3, 3);
  36. panel.setLayout(layout);
  37.  
  38. prices = new HashMap<>();
  39. prices.put(TASTE_SYOYU, 500);
  40. prices.put(TASTE_MISO, 600);
  41. prices.put(TASTE_SIO, 550);
  42. prices.put(TOPPING_PORK, 200);
  43. prices.put(TOPPING_NEGI, 100);
  44. prices.put(TOPPING_MENMA, 100);
  45.  
  46. tastesAndToppings = new LinkedHashMap<>();
  47. tastesAndToppings.put(TASTE_SYOYU, new JRadioButton(TASTE_SYOYU));
  48. tastesAndToppings.put(TASTE_MISO, new JRadioButton(TASTE_MISO));
  49. tastesAndToppings.put(TASTE_SIO, new JRadioButton(TASTE_SIO));
  50. tastesAndToppings.put(TOPPING_PORK, new JCheckBox(TOPPING_PORK));
  51. tastesAndToppings.put(TOPPING_NEGI, new JCheckBox(TOPPING_NEGI));
  52. tastesAndToppings.put(TOPPING_MENMA, new JCheckBox(TOPPING_MENMA));
  53.  
  54. ButtonGroup group = new ButtonGroup();
  55. group.add(tastesAndToppings.get(TASTE_SYOYU));
  56. group.add(tastesAndToppings.get(TASTE_MISO));
  57. group.add(tastesAndToppings.get(TASTE_SIO));
  58.  
  59. tastesAndToppings.forEach((key, value) -> {
  60. panel.add(value);
  61. value.addActionListener(this);
  62. });
  63.  
  64. fee = new JLabel("料金");
  65. result = new JLabel();
  66. unit = new JLabel("円");
  67.  
  68. result.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
  69. unit.setBorder(new EmptyBorder(0, 10, 0, 0));
  70.  
  71. result.setOpaque(true);
  72. result.setBackground(Color.WHITE);
  73.  
  74. panel.add(fee);
  75. panel.add(result);
  76. panel.add(unit);
  77.  
  78. tastesAndToppings.get(TASTE_SYOYU).doClick();
  79.  
  80. getContentPane().add(panel, BorderLayout.CENTER);
  81. }
  82.  
  83. public void actionPerformed(ActionEvent e) {
  84. int resultPrice = tastesAndToppings.values().stream()
  85. .filter(AbstractButton::isSelected)
  86. .mapToInt(value -> prices.get(value.getText()))
  87. .sum();
  88. result.setText(String.valueOf(resultPrice));
  89. }
  90.  
  91. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:10: error: class Ramen is public, should be declared in a file named Ramen.java
public class Ramen extends JFrame implements ActionListener {
       ^
1 error
stdout
Standard output is empty