gpt4 book ai didi

java - 将单选按钮组添加到屏幕

转载 作者:行者123 更新时间:2023-12-02 06:14:36 24 4
gpt4 key购买 nike

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

}

最佳答案

您没有将按钮添加到 UI。

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

关于java - 将单选按钮组添加到屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com