Java 使用get和set在类之间移动数据

Java 使用get和set在类之间移动数据,java,Java,好的,我对getter和setter感到困惑(对Java来说也是新手)。我正在处理的代码将在一个容器中包含多个类。这些类是UserInput、Totals、Greeting和复选框。我遇到的问题是UserInput面板接受用户的输入,数据应该从JTextFields发送到Totals面板中的免费字段。我一直在尝试为回车键使用一个动作侦听器,但没有用 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.even

好的,我对getter和setter感到困惑(对Java来说也是新手)。我正在处理的代码将在一个容器中包含多个类。这些类是UserInput、Totals、Greeting和复选框。我遇到的问题是UserInput面板接受用户的输入,数据应该从JTextFields发送到Totals面板中的免费字段。我一直在尝试为回车键使用一个动作侦听器,但没有用

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.*; 
import javax.swing.border.TitledBorder;


/**
*
* User input panel 
* first panel
* has JTextFields for input of date for the hw/h, hours, and gallons
**/

public class UserInput extends JFrame 
{
    //variables/attributes
    public double kwh;
    public double hours;
    public double gallons;
    public double kFinal;
    public double hFinal;
    public double gFinal;
     String i;   //to hold the string from the textbox

     //JFrame componants
    private final JTextField kwhFieldU;
    private final JTextField hoursFieldU;
    private final JTextField gallonsFieldU;
    private final JLabel kwhLabel;
    private final JLabel hoursLabel;
    private final JLabel gallonsLabel;
    private final JPanel panel;
    TextFieldHandler handler = new TextFieldHandler();
/**
 *constructor
 */
    public UserInput()
    {

        super("User Input");


        // create the Grid
        setLayout (new GridLayout (3,2));

        //create the fields
        kwhFieldU = new JTextField("Enter Kw/H", 15);
        hoursFieldU = new JTextField("Enter Hours", 15);
        gallonsFieldU = new JTextField("Enter Gallons", 15);

        //create the labels
        kwhLabel = new JLabel ("Kw/H");
        hoursLabel = new JLabel ("Hours");
        gallonsLabel = new JLabel ("Gallons");




        //create the panel
        panel = new JPanel();



      kwhFieldU.addActionListener (handler);
      hoursFieldU.addActionListener (handler);
      gallonsFieldU.addActionListener (handler);


        //Add a border
        TitledBorder titled;
        titled = BorderFactory.createTitledBorder("User Input");

        //Add the tiles into the grid
        add(kwhFieldU);
        add(kwhLabel);
        add(hoursFieldU);
        add(hoursLabel);
        add(gallonsFieldU);
        add(gallonsLabel);

        //Add panel to context frame
        add(panel);

        //display the window
        setVisible(true);
    }    




    //The get method to retrieve the user inputs
    Scanner keyboard = new Scanner(System.in);


//get and set methods to store and retrieve date within the variables
    //for kwh, hours, and gallons
/**
 *
 * @return
 */


/**
 *
 * @param k
 */
public void setKwh(double k)
    {
        kwh = k;
    }

public double getKwh()
    {
        return kwh;
    }


//get and set methods for hours

/**
 *
 * @param h
 */
public void setHours (double h)
    {
        hours = h;

    }



     public double getHours ()
    {
        return hours;
    }
//get and set methods for gallons
/**
 *
 * @param g
 */
public void setGallons(double g)
    {
        gallons = g;
    }

/**
 *
 * @return
 */
public double getGallons()
{
    return gallons;
}

//Now add action listeners??? we will try it add the inner class to
//retrieve the variable data

/**
 *
 */
private class TextFieldHandler implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent e)
    {

        if (e.getSource() == kwhFieldU)
           {
           //relates string i with the action command
            i = e.getActionCommand(); 
            //turns string i into a double in variable kFinal
            double kFinal = Double.parseDouble (i); 
            //relates kFinal with kwh
            kwh = kFinal;
            }
        if (e.getSource() == hoursFieldU)
            {
            i = e.getActionCommand();
            double hFinal = Double.parseDouble (i); 
            hours = hFinal;
            }
        if  (e.getSource() == gallonsFieldU)
            {
            i = e.getActionCommand();
            double gFinal = Double.parseDouble (i); 
            gallons = gFinal;
            }
    }    


}   



}     



package applianceutilitycalculator;

import java.awt.GridLayout; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

/**
 *
 * @author Gerber
*/
public class Totals extends JFrame
{

//constants for the grid layout mgr for the Total panel
    private JPanel Panel;
private JLabel kwhLabel;
private JLabel hoursLabel;
private JLabel gallonsLabel;
private JLabel totalLabel;
public JTextField kwhFieldT;
public JTextField hoursFieldT;
public JTextField gallonsFieldT;
private JTextField totalFieldT;

    public Totals()
    {

        super();


         // create the Grid
        setLayout (new GridLayout (4,2));

        //Create the fields
        kwhFieldT = new JTextField(15);
        hoursFieldT = new JTextField(15);
        gallonsFieldT = new JTextField(15);
        totalFieldT = new JTextField(15);

        //create the labels
        kwhLabel = new JLabel ("Kw/H");
        hoursLabel = new JLabel ("Hours");
        gallonsLabel = new JLabel ("Gallons");
        totalLabel = new JLabel ("Totals");

         //Add a border
        TitledBorder titled;
        titled = BorderFactory.createTitledBorder("Totals");


        //add panels to grid
        add(kwhFieldT);
        add(kwhLabel);
        add(hoursFieldT);
        add(hoursLabel);
        add(gallonsFieldT);
        add(gallonsLabel);
        add(totalFieldT);
        add(totalLabel);

         //Add panel to context frame
        add(Panel);

        //display the window
        setVisible(true);
    }    
        //add actionperformed to get the return value to fill the text fields
        //need to convert it from double into string ???

/**
 *
 * @param evt
 */
public void actionPerformed(ActionEvent evt)
        {

        kwhFieldT = kwh.getText();
        hoursFieldT = hours.getText();
        gallonsFieldT = gallons.getText();

        }
}

如果我错了,请纠正我。但是
kwhFieldT
a
JTextField
kwh
不是一个原语
double
?因此我必须解析kwh输入并移动它?您是否收到任何编译错误或运行时异常?(红色文本)。如果是,请在这里发布。可能是
kwhFieldT.setText(String.valueOf(kwh))
而不是
kwhFieldT=kwh.getText()?那么,当您在用户输入中输入“50”时,Kwh字段中的“50”将显示在总计Kwh字段中?