卡在这个JFrame程序中

时间:2014-05-09 03:01:04

标签: java jframe

在我的java类的JFrame实验室工作。我的程序还没有完成,但我需要一些帮助,为什么男性和女性的JFrame在点击男性或女性JButton后没有显示。任何帮助将不胜感激。

这是我的计划:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class Lab4EC extends JFrame implements ActionListener
{
    private static final int Width = 500;
    private static final int Height = 200;

    private JLabel bodyweightJL,wristmeasurementJL,waistmeasurementJL,hipmeasurementJL,forearmJL,bodyfatJL;

    private JTextField bodyweightTF,wristmeasurementTF,waistmeasurementTF,hipmeasurementTF,forearmTF,bodyfatTF;

    private JButton maleJB,femaleJB,calculateJB,exitJB;

    double A1,A2,A3,A4,A5,a1,a2,b,B,bodyWeight,wristMeasurement,hipMeasurement,forearmMeasurement,waistMeasurement,fbodyFat,mbodyFat,fbodyFP,mbodyFP;

    Container pane = getContentPane();

    public Lab4EC ()
    {
        setTitle ("Body Fat Calculator");

        maleJB = new JButton ("Male");
        femaleJB = new JButton ("Female");

        pane.setLayout (new GridLayout (2,1));

        pane.add(maleJB);
        pane.add(femaleJB);

        maleJB.addActionListener(this);
        femaleJB.addActionListener(this);

        setSize(Width, Height);
        setVisible (true);
    }

    public void actionPerformed(ActionEvent e)
        {

        if(e.getActionCommand().equals("Male")){


         bodyweightJL = new JLabel ("Enter Body Weight: ", SwingConstants.RIGHT);
         waistmeasurementJL = new JLabel ("Enter Waist Measurement: ", SwingConstants.RIGHT);
         bodyfatJL = new JLabel ("Your Body Fat Percentage Is: ", SwingConstants.RIGHT);

         bodyweightTF = new JTextField(10);

         waistmeasurementTF = new JTextField(10);

         calculateJB = new JButton ("Calculate");

         pane.setLayout (new GridLayout (4,2));
         pane.remove(maleJB);
         pane.remove(femaleJB);
         pane.add(bodyweightJL);
         pane.add(waistmeasurementJL);
         pane.add(bodyfatJL);
         pane.add(calculateJB);
         pane.add(bodyweightTF);
         pane.add(waistmeasurementTF);
         pane.add(bodyfatTF);

         calculateJB.addActionListener(this);
         exitJB.addActionListener(this);

         setSize(Width, Height);
         setVisible (true);

         if(e.getActionCommand().equals("Calcualate")){
             a1 = (bodyWeight*1.082)+94.42;
             a2 = waistMeasurement*4.15;
             b = a1-a2;
             mbodyFat = bodyWeight - b;
             mbodyFP = mbodyFat*100/bodyWeight;

            }

        }
        else {

         bodyweightJL = new JLabel ("Enter Body Weight: ", SwingConstants.RIGHT);
         wristmeasurementJL = new JLabel ("Enter Wrist Measurement: ", SwingConstants.RIGHT);
         waistmeasurementJL = new JLabel ("Enter Waist Measurement: ", SwingConstants.RIGHT);
         hipmeasurementJL = new JLabel ("Enter Hip Measurement: ", SwingConstants.RIGHT);
         forearmJL = new JLabel ("Enter Forearm Measurement: ", SwingConstants.RIGHT);
         bodyfatJL = new JLabel ("Your Body Fat Percentage Is: ", SwingConstants.RIGHT);

         calculateJB = new JButton ("Calculate");

         bodyweightTF = new JTextField(10);
         wristmeasurementTF = new JTextField(10);
         waistmeasurementTF = new JTextField(10);
         hipmeasurementTF = new JTextField(10);
         forearmTF = new JTextField(10);
         bodyfatTF = new JTextField(10);

         pane.setLayout (new GridLayout (7,2));
         pane.remove(maleJB);
         pane.remove(femaleJB);
         pane.add(bodyweightJL);
         pane.add(wristmeasurementJL);
         pane.add(waistmeasurementJL);
         pane.add(hipmeasurementJL);
         pane.add(forearmJL);
         pane.add(bodyfatJL);
         pane.add(bodyweightTF);
         pane.add(wristmeasurementTF);
         pane.add(waistmeasurementTF);
         pane.add(hipmeasurementTF);
         pane.add(forearmTF);
         pane.add(bodyfatTF);
         pane.add(exitJB);

         calculateJB.addActionListener(this);
         exitJB.addActionListener(this);

         setSize(Width, Height);
         setVisible (true);
        }

       }



    public static void main (String[] args)
    {
        Lab4EC rectObject = new Lab4EC ();
    }

}

2 个答案:

答案 0 :(得分:1)

我会查看How to Use CardLayout

它允许您轻松交换框架中的面板。

答案 1 :(得分:0)

将代码放入调试器中,并查看第67和118行。

你真的需要做更多自己的功课,然后把问题转嫁给其他人为你解决。

请参阅此link以获取一些很好的建议。

相关问题