weight calculator
Question:
Write the ideal weight calculator so that height in inches is entered by using a slider. Use the approximate formula:
W = H2 / 30 , for female
W = H2 / 28 , for male
where W is the ideal weight in pounds,
H is the height in inches
Set an action command for each radio button using setActionCommand(String).
Add an action listener for each button using addActionListener().
Add a change listener for the slider using addChangeListener().
The ideal weight should be displayed in a text field when the user changes either a radio button or the slider.
Pick initial settings for the buttons and slider. When the program starts up display the ideal weight for those settings.
| Attachment | Size |
|---|---|
| cal.jpg | 19.58 KB |





public class IdealWeight {
private JFrame frame;
private JTextField idealField = new JTextField();
private JRadioButton maleRadioBtn;
private JSlider slider;
private String MALE = """;
private String FEMALE = "女";
private String SEX_LABEL = "您的性别";
private String HEIGH_LABEL ="您的身高";
private String IDEAL_WEIGHT = "理想"重";
final private String MALE_EN = "Male";
final private String FEMALE_EN = "Female";
final private String SEX_LABEL_EN = "Your gender";
final private String HEIGH_LABEL_EN ="Your height in Inches";
final private String IDEAL_WEIGHT_EN = "Ideal weight";
private boolean isEn = false;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
IdealWeight window = new IdealWeight("en");
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public IdealWeight(String local){
if(local != null && local.equalsIgnoreCase("en")){
MALE = MALE_EN;
FEMALE = FEMALE_EN;
SEX_LABEL = SEX_LABEL_EN;
HEIGH_LABEL = HEIGH_LABEL_EN;
IDEAL_WEIGHT = IDEAL_WEIGHT_EN;
isEn = true;
}
initialize();
}
/**
* Create the application.
*/
public IdealWeight() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 350, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new VerticalFlowLayout(VerticalFlowLayout.LEFT));
JPanel topPanel = new JPanel(new VerticalFlowLayout(FlowLayout.CENTER));
JPanel labPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,40,5));
labPanel.add(new JLabel(SEX_LABEL));
labPanel.add(new JLabel(HEIGH_LABEL));
ButtonGroup btnGroup = new ButtonGroup();
maleRadioBtn = new JRadioButton(MALE);
JRadioButton femaleRadioBtn = new JRadioButton(FEMALE);
btnGroup.add(maleRadioBtn);
btnGroup.add(femaleRadioBtn);
maleRadioBtn.setSelected(true);
JPanel sexPanel = new JPanel(new GridLayout(2,1));
sexPanel.add(maleRadioBtn);
ChangeListernerHandler changeListener = new ChangeListernerHandler();
maleRadioBtn.addChangeListener(changeListener);
femaleRadioBtn.addChangeListener(changeListener);
sexPanel.add(femaleRadioBtn);
JPanel contentPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,40,5));
contentPanel.add(sexPanel);
if(isEn)
slider = new JSlider(SwingConstants.VERTICAL,40,80,58);
else
slider = new JSlider(SwingConstants.VERTICAL,155,190,170);
slider.setPaintLabels(true);
slider.setPaintTicks(true);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(1);
slider.setSnapToTicks(true);
slider.setToolTipText(slider.getValue()+"");
contentPanel.add(slider);
slider.addChangeListener(changeListener);
topPanel.add(labPanel);
topPanel.add(contentPanel);
JPanel btmPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
btmPanel.add(new JLabel(IDEAL_WEIGHT));
btmPanel.add(idealField);
idealField.setPreferredSize(new Dimension(50, 20));
frame.getContentPane().add(topPanel);
frame.getContentPane().add(btmPanel);
setIdealField();
}
class ChangeListernerHandler implements ChangeListener{
public void stateChanged(ChangeEvent e){
if(e.getSource() == slider){
slider.setToolTipText(slider.getValue()+"");
}
setIdealField();
}
}
private void setIdealField(){
String idealStr = "";
if(isEn){
int p = maleRadioBtn.isSelected()? 28 : 30;
int h = slider.getValue();
double w = Math.ceil(h*h*100/p+0.5);
idealStr = w/100+"";
}else{
int p = maleRadioBtn.isSelected()? 90 : 100;
double weight = (slider.getValue() - p) * 0.8;
idealStr = Math.ceil(weight*100+0.5)/100 + "";
}
idealField.setText(idealStr);
}
}