Java ActionListener&;方法可以';";见;JTextFields

Java ActionListener&;方法可以';";见;JTextFields,java,swing,scope,actionlistener,jtextfield,Java,Swing,Scope,Actionlistener,Jtextfield,关于代码的一点背景知识。我是一名正在放暑假的学生,我想玩玩我的编程技能。我编写了一个简单的GUI程序来计算举重运动员的营养需求。该方案充分发挥了作用 然后,我扩展了代码,创建了一个选项卡式窗格。程序的第三个选项卡是我最初的营养程序中的代码。现在代码不起作用了。特别是ActionListener的嵌套类和我编写的用于计算营养值的方法无法解析构造函数中的JTextField代码项。我可以删除ActionListener和actionTakenMethod,剩下的代码也可以工作(虽然不漂亮,但可以工作

关于代码的一点背景知识。我是一名正在放暑假的学生,我想玩玩我的编程技能。我编写了一个简单的GUI程序来计算举重运动员的营养需求。该方案充分发挥了作用

然后,我扩展了代码,创建了一个选项卡式窗格。程序的第三个选项卡是我最初的营养程序中的代码。现在代码不起作用了。特别是ActionListener的嵌套类和我编写的用于计算营养值的方法无法解析构造函数中的JTextField代码项。我可以删除ActionListener和actionTakenMethod,剩下的代码也可以工作(虽然不漂亮,但可以工作)

我非常感谢在这个问题上能得到的任何帮助。我会记住两件事。首先,我从原始程序中使用的代码是有效的,所以直到我将代码添加到选项卡式窗格中,这个问题才出现。第二,我还是一名本科生,所以我的代码可能不是最漂亮的!谢谢

此外,我的程序尚未完成,因此前两个选项卡中没有任何内容

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

public class GatesFiveByFive extends JFrame{

    public GatesFiveByFive(){

        //Title of window
        setTitle("Gates 5x5");

        //Size of window
        setSize(500,500);

        //Set Default Close Operation
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set Panel Visible
        setVisible(true);

        //JTabbedPane object declaration
        JTabbedPane jtp=new JTabbedPane();

        //creates the content pane
        getContentPane().add(jtp);

        //First Tab
        JPanel tab1=new JPanel();

        //Second Tab
        JPanel tab2=new JPanel();

        //Third Tab
        JPanel tab3=new JPanel();

        //Add Tab 1 to Content Pane
        jtp.addTab("     5x5     ",tab1);

        //Add Tab 2 to Content Pane
        jtp.addTab("     HST     ",tab2);

        //Add Tab 3 to Content Pane
        jtp.addTab(" Nutrition ",tab3);

        /**Adds content to the First Tab pane*/

        /**Adds content to the Second Tab pane*/

        /**Adds content to Third Tab pane*/

        JButton calculate;//display calculate button

        JLabel weight;
        JLabel weight1;
        JLabel totalCalories;
        JLabel protein;
        JLabel fats;
        JLabel carbs;

        JTextField weightField;
        JTextField totalCaloriesField;
        JTextField proteinField;
        JTextField fatsField;
        JTextField carbsField;

        //Create five labels
        weight=new JLabel("Enter your weight:");
        weight1=new JLabel("");
        totalCalories=new JLabel("Total Calories");
        protein=new JLabel("Protein");
        fats=new JLabel("Fats");
        carbs=new JLabel("Carbs");

        //Create five text fields
        weightField=new JTextField(20);
        totalCaloriesField=new JTextField(20);
        proteinField=new JTextField(20);
        fatsField=new JTextField(20);
        carbsField=new JTextField(20);

        //Create "calculate" button
        calculate=new JButton("Calculate");

        //Register "calculate" button
        calculate.addActionListener(new CalculateButtonListener());

        //Populate Tab 3
        tab3.setLayout(new GridLayout(6,2));
        tab3.add(weight);
        tab3.add(weight1);
        tab3.add(weightField);
        tab3.add(calculate);
        tab3.add(totalCalories);
        tab3.add(totalCaloriesField);
        tab3.add(protein);
        tab3.add(proteinField);
        tab3.add(fats);
        tab3.add(fatsField);
        tab3.add(carbs);
        tab3.add(carbsField);


    }//end constructor GatesFiveByFive

    /**Private inner class for event handler CalculateButtonListener*/
    private class CalculateButtonListener implements ActionListener{

        public void actionPerformed(ActionEvent e){

            actionTakenMethod(weightField.getText());     


        }//end actionPerformed method

    }//end CalculateButtonListener inner class

    /**method for calculating tab 3 stats*/      
    public void actionTakenMethod(String currentWeight){

        /**Convert String input to double for processing*/
        double x=Double.parseDouble(currentWeight);

        /**Calculations for finding macronutrient stats*/
        double cal=14.183*x+500; //total calories
        double pro=0.886*x; //protein
        double fat=0.39397*x; //fats
        double carb=1.7733175*x; //carbs

        /**Convert results of calculations back to string for display*/
        String calCal=Double.toString(cal);
        String proPro=Double.toString(pro);
        String fatFat=Double.toString(fat);
        String carbCarb=Double.toString(carb);

        /**Display results of calculation*/
        totalCaloriesField.setText(calCal);
        proteinField.setText(proPro);
        fatsField.setText(fatFat);
        carbsField.setText(carbCarb);

    }//end actionTakenMethod

    public static void main(String[]args){

        GatesFiveByFive gates=new GatesFiveByFive();

    }//end main

}// end GatesFiveByFive.java

这些变量是构造函数的局部变量。在构造函数外部声明
JLabel
s和
JTextField
s。也就是说,将它们设为
GatesFiveByFive
类的私有实例字段。您的
ActionListener
是一个内部类,因此它仍然可以访问外部类的那些字段。

这些变量是构造函数的本地变量。在构造函数外部声明
JLabel
s和
JTextField
s。也就是说,将它们设为
GatesFiveByFive
类的私有实例字段。您的
ActionListener
是一个内部类,因此它仍然可以访问外部类的那些字段。

您已经创建了构造函数的本地变量。您应该使它们成为实例变量,以便类中的任何方法都可以访问它们

public class GatesFiveByFive extends JFrame{
    JButton calculate;//display calculate button

    JLabel weight;
    JLabel weight1;
    JLabel totalCalories;
    JLabel protein;
    JLabel fats;
    JLabel carbs;

    JTextField weightField;
    JTextField totalCaloriesField;
    JTextField proteinField;
    JTextField fatsField;
    JTextField carbsField;
...

您已经创建了构造函数的本地变量。您应该使它们成为实例变量,以便类中的任何方法都可以访问它们

public class GatesFiveByFive extends JFrame{
    JButton calculate;//display calculate button

    JLabel weight;
    JLabel weight1;
    JLabel totalCalories;
    JLabel protein;
    JLabel fats;
    JLabel carbs;

    JTextField weightField;
    JTextField totalCaloriesField;
    JTextField proteinField;
    JTextField fatsField;
    JTextField carbsField;
...

你的权利!真不敢相信我错过了!我在我的原始程序中这样做了,但我将整个原始类复制/粘贴到我的新构造函数中,然后删除了不必要的代码。这使我的变量成为构造函数的局部变量!另外,感谢您的快速回复!我很感激!你的权利!真不敢相信我错过了!我在我的原始程序中这样做了,但我将整个原始类复制/粘贴到我的新构造函数中,然后删除了不必要的代码。这使我的变量成为构造函数的局部变量!另外,感谢您的快速回复!我很感激!谢谢我完全错过了!谢谢我完全错过了!