这是一个BMI计算器。我必须分类实验4和实验室5(这是实验4的扩展)然而,我试图让JFrame(frame1)显示一旦我点击计算按钮(CalculateBMI),在尝试并更改它之后有些时候什么都没有用,我希望也许有人可以帮助我。
**LAB4**
import java.awt.*;
import javax.swing.*;
public class Lab4 extends JApplet
{
JButton calculateBMI, clear, resetapplet, displaystats, clearstats;
Image img1, img2, img3;
JLabel title, logo1, logo2, logo3, explain, showpic, empty1, empty2, welcome, BMIresult, genderOutput, height, weight, BMI, healthy, thanks;
JPanel north, west, center, east, south, west1, center1, south1, north2, west2, center2, east2, south2, center1_1;
JComboBox gender, feet, inches;
JFrame frame1 = new JFrame("BMI Calculation ");
JFrame frame2 = new JFrame ("BMI Calcuylation: User Story");
JTextArea txt1;
JTextField txtfield1, txtfield2;
Color violet = new Color(132, 28, 164);
Color darkRed = new Color(148, 15, 15);
Color lightRed = new Color(162, 101, 101);
public void init ()
{
setLayout( new BorderLayout());
doNorth();
doWest();
doCenter();
doEast();
doSouth();
frame1();
frame2();
setSize (975,500);
}
public void frame1()
{
frame1.setBounds(0, 250, 400, 300);
doWest1();
doCenter1();
doSouth1();
frame1.setVisible(false);
}
public void doWest1()
{
JPanel west1_1 = new JPanel(new FlowLayout());
img3 = getImage(getCodeBase(), "HealthyMale.png");
logo3 = new JLabel(new ImageIcon(img3));
west1_1.add(logo3);
frame1.add(west1_1, BorderLayout.WEST);
}
public void doCenter1()
{
center1_1 = new JPanel(new GridLayout(12,1));
empty1 = new JLabel("");
empty2 = new JLabel("");
welcome = new JLabel("Welcome Sally Jones");
BMIresult = new JLabel("Your BMI results are:");
genderOutput = new JLabel("You are: Female");
height = new JLabel("Your Height is: 5 feet and 3 inches");
weight = new JLabel("Your weight is: 122lbs");
BMI = new JLabel("Your Body Mass Index (BMI) is: 21.61");
healthy = new JLabel("You are: Healthy = BMI between 18.5-24.9");
thanks = new JLabel("Thank you for using our Java Applet");
center1_1.add(empty1);
center1_1.add(welcome);
center1_1.add(BMIresult);
center1_1.add(genderOutput);
center1_1.add(height);
center1_1.add(weight);
center1_1.add(BMI);
center1_1.add(empty2);
center1_1.add(healthy);
center1_1.add(thanks);
frame1.add(center1_1, BorderLayout.CENTER);
}
public void doSouth1()
{
south1 = new JPanel(new FlowLayout());
JButton exitFrame = new JButton("Exit Frame");
JButton storeResults = new JButton("Store Results");
south1.add(storeResults);
south1.add(exitFrame);
south1.setBackground(darkRed);
frame1.add(south1, BorderLayout.SOUTH);
}
public void frame2()
{
frame2.setBounds(450, 250, 525, 300);
doNorth2();
doCenter2();
doSouth2();
doEast2();
doWest2();
frame2.setVisible(false);
}
public void doNorth2()
{
north2 = new JPanel(new FlowLayout());
JLabel history2 = new JLabel ("BMI's Caltulated and Stored since the last 'Clear Stats'");
north2.add(history2);
north2.setBackground(lightRed);
frame2.add(north2, BorderLayout.NORTH);
}
public void doCenter2()
{
center2 = new JPanel(new GridLayout(6,1));
JPanel center2_1 = new JPanel(new FlowLayout());
JPanel center2_2 = new JPanel(new GridLayout(1,3));
JPanel center2_3 = new JPanel(new GridLayout(1,3));
JLabel username = new JLabel("User Name");
JLabel gender = new JLabel ("Gender");
JLabel BMIcalculation = new JLabel("BMI Calculation");
JLabel name = new JLabel("Sally Jones");
JLabel female = new JLabel ("Female");
JLabel calculation = new JLabel ("21.61");
center2_2.add(username);
center2_2.add(gender);
center2_2.add(BMIcalculation);
center2_1.add(center2_2);
center2.add(center2_1);
center2_3.add(name);
center2_3.add(female);
center2_3.add(calculation);
center2.add(center2_3);
center2.setBackground(lightRed);
center2_1.setBackground(lightRed);
center2_2.setBackground(lightRed);
center2.setBorder(BorderFactory.createLineBorder(Color.darkGray, 1));
center2_1.setBorder(BorderFactory.createLineBorder(Color.darkGray, 2));
frame2.add(center2, BorderLayout.CENTER);
}
public void doEast2()
{
JPanel east2 = new JPanel (new FlowLayout());
east2.setBackground(lightRed);
east2.setPreferredSize(new Dimension(10,20));
frame2.add(east2,BorderLayout.EAST);
}
public void doWest2()
{
JPanel west2 = new JPanel (new FlowLayout());
west2.setBackground(lightRed);
west2.setPreferredSize(new Dimension(10,20));
frame2.add(west2,BorderLayout.WEST);
}
public void doSouth2()
{
south2 = new JPanel(new FlowLayout());
JButton exitFrame = new JButton("Exit Frame");
south2.add(exitFrame);
south2.setBackground(darkRed);
frame2.add(south2, BorderLayout.SOUTH);
}
public void doNorth()
{
north = new JPanel(new FlowLayout());
img1 = getImage(getCodeBase(), "BMI2.png");
logo1 = new JLabel (new ImageIcon(img1));
//explanation part
JPanel flow = new JPanel(new GridLayout(2,1));
JPanel flow1 = new JPanel (new FlowLayout());
JPanel flow2 = new JPanel (new FlowLayout());
JLabel title = new JLabel("Let's Calculate Your Body Mass Index (BMI)");
title.setFont(new Font("MonoSpaced", Font.BOLD, 22));
title.setForeground(Color.RED);
flow.setBackground(Color.GRAY);
JTextArea subtitle = new JTextArea("The Body Mass Index (BMI) measures the weight status of your body in relation \n"
+ " to the fat. It is a simple tool that helps to calculate the amont of excess \n"
+ " body fat and the associated risk of carrying this extra weight. It can be aplied \n "
+ "to both men and women. ");
subtitle.setForeground(Color.BLACK);
subtitle.setEnabled(false);
subtitle.setDisabledTextColor(Color.BLACK);
flow1.add(title);
flow2.add(subtitle);
flow.add(flow1);
flow.add(flow2);
flow.getBackground();
img2 = getImage(getCodeBase(), "BMI1.jpeg");
logo2 = new JLabel (new ImageIcon(img2));
north.add(logo1);
north.add(flow);
north.add(logo2);
north.setBackground(violet);
add(north,BorderLayout.NORTH);
}
public void doWest()
{
JPanel west = new JPanel (new FlowLayout());
west.setBackground(violet);
west.setPreferredSize(new Dimension(150,100));
add(west,BorderLayout.WEST);
}
public void doEast()
{
JPanel east = new JPanel (new FlowLayout());
east.setBackground(violet);
east.setPreferredSize(new Dimension(150,100));
add(east,BorderLayout.EAST);
}
public void doCenter()
{
center = new JPanel(new GridLayout(5,1));
//third row (3-rd)
JPanel third = new JPanel(new FlowLayout());
JTextField select = new JTextField("Select to Show Pictures or not:");
select.setEnabled(false);
select.setFont(new Font("MonoSpaced", Font.BOLD, 15 ));
select.setDisabledTextColor(Color.BLACK);
third.add(select);
JRadioButton radio = new JRadioButton("On");
third.add(radio);
radio.setEnabled(true);
JRadioButton radio1 = new JRadioButton("Off");
third.add(radio1);
radio1.setEnabled(true);
JCheckBox box = new JCheckBox("Sounds On");
third.add(box);
box.setEnabled(true);
third.setBackground(violet);
//fourth row(4-th)
JPanel txt3 = new JPanel(new FlowLayout());
JLabel logo3 = new JLabel("Press 'Calculate BMI' or 'CLEAR'", Font.ITALIC);
logo3.setFont(new Font("Monospaced", Font.BOLD, 18));
logo3.setForeground(Color.RED);
txt3.add(logo3);
center.add(third);
center.add(txt3);
//Fifth row
JPanel fifth = new JPanel(new FlowLayout());
JLabel name = new JLabel("Your Name:");
txtfield1 = new JTextField(20);
txtfield1.setEnabled(true);
gender = new JComboBox();
gender.addItem("Select");
gender.addItem("Male");
gender.addItem("Female");
gender.setEnabled(true);
//Adding Component Together
fifth.add(name);
fifth.add(txtfield1);
fifth.add(gender);
center.add(fifth);
//Sixth Row
JPanel sixth = new JPanel(new FlowLayout());
feet = new JComboBox();
feet.addItem("Select Feet");
feet.addItem(1);
feet.addItem(2);
feet.addItem(3);
feet.addItem(4);
feet.addItem(5);
feet.addItem(6);
feet.addItem(7);
feet.addItem(8);
feet.setEnabled(true);
inches = new JComboBox();
inches.addItem("Select Inches");
inches.addItem(1);
inches.addItem(2);
inches.addItem(3);
inches.addItem(4);
inches.addItem(5);
inches.addItem(6);
inches.addItem(7);
inches.addItem(8);
inches.addItem(9);
inches.addItem(10);
inches.addItem(11);
inches.setEnabled(true);
JLabel weight = new JLabel("Your Weight:");
txtfield2 = new JTextField(5);
txtfield2.setEnabled(true);
sixth.add(feet);
sixth.add(inches);
sixth.add(weight);
sixth.add(txtfield2);
center.add(sixth);
//seventh row
JPanel seventh = new JPanel(new FlowLayout());
calculateBMI = new JButton ("Calculate BMI");
JButton clear = new JButton ("Clear");
seventh.add(calculateBMI);
seventh.add(clear);
center.add(seventh);
center.setBorder(BorderFactory.createLineBorder(Color.darkGray, 5));
center.setSize(500,300);
center.setBackground(violet);
add(center,BorderLayout.CENTER);
}
public void doSouth()
{
south = new JPanel(new FlowLayout());
//eighth row
JButton reset = new JButton("Reset Applet");
JButton display = new JButton("Display Stats");
JButton stats = new JButton("Clear Stats");
south.add(reset);
south.add(display);
south.add(stats);
south.setBackground(violet);
south.setPreferredSize(new Dimension(800,100));
add(south,BorderLayout.SOUTH);
}
}
**LAB5**
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
public class Lab_5 extends Lab4 implements ActionListener
{
double H1, H2, W1, BMI1;
String Height, Height2, Weight1, BMIndex;
Image healthymale, healthyfemale, obesefemale, obesemale, overweightmale, overweightfemale, underweightmale, underweightfemale;
public void init()
{
super.init();
healthy();
obese();
overWeight();
underWeight();
images();
JLabel BMIText = new JLabel("Your Body Mass Index (BMI) is:" + BMIndex );
Height = (String) feet.getSelectedItem();
H1 = Double.parseDouble(Height);
H1 = H1 * 12;
Height2 = (String) inches.getSelectedItem();
H2 = Double.parseDouble(Height2);
H2 = H1 + H2 ;
Weight1 = (String) txtfield2.getText();
W1 = Double.parseDouble(Weight1);
W1 = W1 *703;
BMI1 = W1/H2;
BMIndex = String.valueOf(BMI);
calculateBMI.addActionListener(this);
calculateBMI.setEnabled(true);
}
public void images()
{
healthymale = getImage(getCodeBase(), "HealthyMale.png");
obesemale = getImage(getCodeBase(), "ObeseMale.png");
overweightmale = getImage(getCodeBase(), "OverweightMale.png");
underweightmale = getImage(getCodeBase(), "UnderweightMale.png");
healthyfemale = getImage(getCodeBase(), "HealthyFemale.png");
obesefemale = getImage(getCodeBase(),"ObeseFemale.png");
overweightfemale = getImage(getCodeBase(),"OverweightFemale.png");
underweightfemale = getImage(getCodeBase(),"UnderWeightFemale.png");
}
public void healthy()
{
welcome = new JLabel ("welcome" + txtfield1);
genderOutput = new JLabel("You are:"+ gender);
height = new JLabel("Your Height is:" + feet + "Feet" + inches + "Inches" );
weight = new JLabel("Your weight is:" + txtfield2 +"lbs");
BMI = new JLabel("Your Body Mass Index (BMI) is:"+BMIndex);
healthy = new JLabel("You are: Healthy = BMI between 18.5-24.9");
}
public void obese()
{
welcome = new JLabel ("welcome" + txtfield1);
genderOutput = new JLabel("You are:"+ gender);
height = new JLabel("Your Height is:" + feet + "Feet" + inches + "Inches" );
weight = new JLabel("Your weight is:" + txtfield2 +"lbs");
BMI = new JLabel("Your Body Mass Index (BMI) is:"+BMIndex);
healthy = new JLabel("You are: Obese = BMI of 30 or Greater");
}
public void overWeight()
{
welcome = new JLabel ("welcome" + txtfield1);
genderOutput = new JLabel("You are:"+ gender);
height = new JLabel("Your Height is:" + feet + "Feet" + inches + "Inches" );
weight = new JLabel("Your weight is:" + txtfield2 +"lbs");
BMI = new JLabel("Your Body Mass Index (BMI) is:"+BMIndex);
healthy = new JLabel("You are: Over Weight = BMI between 25 - 29.9");
}
public void underWeight()
{
welcome = new JLabel ("welcome" + txtfield1);
genderOutput = new JLabel("You are:"+ gender);
height = new JLabel("Your Height is:" + feet + "Feet" + inches + "Inches" );
weight = new JLabel("Your weight is:" + txtfield2 +"lbs");
BMI = new JLabel("Your Body Mass Index (BMI) is:"+BMIndex);
healthy = new JLabel("You are: Under Weight = BMI lower than 18.5");
}
@Override
public void actionPerformed(ActionEvent arg0)
{
if
(BMI1 <= 18.5)
underWeight();
else if
(BMI1 > 18.5 && BMI1 <=24.9)
healthy();
else if
(BMI1 > 25 && BMI1 <= 29.9)
overWeight();
else if
(BMI1 > 30 )
obese();
Object obj = arg0.getSource();
if (obj == calculateBMI);
calculateBMI.getActionCommand();
frame1.setVisible(true);
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
如果您只想在calculateBMI按钮内显示frame1,请在Lab5中将ActionListener添加到执行计算的按钮,然后显示JFrame。简单的事情:
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class Lab5b extends Lab4 {
@Override
public void init() {
super.init();
// add an ActionListener to the button
calculateBMI.addActionListener(e -> {calcBmiAction(e);});
}
private void calcBmiAction(ActionEvent e) {
// TODO calculations for BMI here. I'll leave this for you to do.
// frame1 displayed:
frame1.setVisible(true); // that's all that's needed
}
}
您可能不应该创建您在Lab5课程中所做的所有组件,而应该更改现有组件的状态。
但也明白,这是你教练要做的糟糕的程序设计。