0

所以我正在创建一个计算 BMR 和 TDEE 的计算器。我在运行代码时遇到了问题,因为除了我的单选按钮组之外,所有内容都显示出来了。谁能告诉我我需要添加什么/我做错了什么才能让它们出现在屏幕上?

   private JRadioButton female, male, sedentary, lightlyActive, modActive, veryActive, extrActive;

   private JButton calculate;

   public BMRGUI()

   {

      heightLabel = new JLabel ("Height (in.) ");

      weightLabel = new JLabel ("Weight (lbs) ");

      ageLabel = new JLabel ("Age (yrs)");

      female = new JRadioButton ("Female");
      female.setMnemonic(KeyEvent.VK_C); 
      female.setActionCommand("Female");
      female.setSelected(true);

      male = new JRadioButton ("Male");
      male.setMnemonic(KeyEvent.VK_C); 
      male.setActionCommand("Male");

      ButtonGroup group = new ButtonGroup();
      group.add(female);
      group.add(male);

      female.addActionListener(this);
      male.addActionListener(this);

      sedentary = new JRadioButton ("Sedentary");
      sedentary.setMnemonic(KeyEvent.VK_C); 
      sedentary.setActionCommand("Sedentary");
      sedentary.setSelected(true);

      lightlyActive = new JRadioButton ("Lightly active");
      lightlyActive.setMnemonic(KeyEvent.VK_C); 
      lightlyActive.setActionCommand("Lightly active");

      modActive = new JRadioButton ("Moderately active");
      modActive.setMnemonic(KeyEvent.VK_C); 
      modActive.setActionCommand("Moderately active");

      veryActive = new JRadioButton ("Very active");
      veryActive.setMnemonic(KeyEvent.VK_C); 
      veryActive.setActionCommand("Very active");

      extrActive = new JRadioButton ("Extremely active");
      extrActive.setMnemonic(KeyEvent.VK_C); 
      extrActive.setActionCommand("Extremely active");

      ButtonGroup group2 = new ButtonGroup();
      group2.add(sedentary);
      group2.add(lightlyActive);
      group2.add(modActive);
      group2.add(veryActive);
      group2.add(extrActive);
      sedentary.addActionListener((ActionListener) this);
      lightlyActive.addActionListener((ActionListener) this);
      modActive.addActionListener((ActionListener) this);
      veryActive.addActionListener((ActionListener) this);
      extrActive.addActionListener((ActionListener) this);

      BMRLabel = new JLabel ("Your BMR is ");

      bmrResultLabel = new JLabel (" ");

      bmrMeaningLabel = new JLabel(" ");

      TDEELabel = new JLabel ("Your TDEE is ");

      tdeeResultLabel = new JLabel (" ");

      tdeeMeaningLabel = new JLabel(" ");

      height = new JTextField ();

      weight = new JTextField ();

      age = new JTextField ();

      calculate = new JButton ("Calculate");

      BMRListener Listener = new BMRListener();

      calculate.addActionListener(Listener);

      setPreferredSize (new Dimension(WIDTH, HEIGHT));

      setBackground (Color.white);



      add(heightLabel);

      add(height);

      add(weightLabel);

      add(weight);

      add(ageLabel);

      add(age);

      add(calculate);

      add(BMRLabel);

      add(bmrResultLabel);

      add(bmrMeaningLabel);

      add(TDEELabel);

      add(tdeeResultLabel);

      add(tdeeMeaningLabel);

   }
4

2 回答 2

2

您没有将按钮添加到 UI。

AButtonGroup是管理一组按钮的非可视化助手类,用于确保只选择一个按钮。

于 2014-02-06T20:44:35.800 回答
0

使用以下命令添加 RadioButtons 组:

add(group2);
于 2014-02-06T20:31:07.350 回答