【问题标题】:ActionListener & a method can't "see" JTextFieldsActionListener & 一个方法不能“看到” JTextFields
【发布时间】:2013-06-28 15:50:25
【问题描述】:

代码的一些背景知识。我是一名学生,目前正在暑假,我想玩弄我的编程技能。我编写了一个简单的 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

【问题讨论】:

    标签: java swing scope actionlistener jtextfield


    【解决方案1】:

    这些变量是构造函数的局部变量。在构造函数之外声明 JLabels 和 JTextFields。也就是说,使它们成为GatesFiveByFive 类的私有实例字段。你的ActionListener 是一个内部类,所以它仍然可以访问外部类的那些字段。

    【讨论】:

    • 谢谢!我完全错过了!
    【解决方案2】:

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

    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;
    ...
    

    【讨论】:

    • 你的权利!我不敢相信我错过了!我在我的原始程序中这样做了,但是我将整个原始类复制/粘贴到我的新构造函数中,然后删除了不必要的代码。这使我的变量成为构造函数的本地变量!另外,感谢您的快速回复!我很感激!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多