将单选按钮组添加到屏幕

时间:2014-02-06 20:27:20

标签: java swing user-interface jradiobutton buttongroup

所以我正在创建一个计算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);

   }

2 个答案:

答案 0 :(得分:2)

您没有将按钮添加到用户界面。

ButtonGroup是一个非可视帮助类,它管理一组按钮,用于确保只选择一个按钮。

答案 1 :(得分:0)

使用以下方法添加RadioButtons组:

add(group2);