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

时间:2014-05-18 02:55:52

标签: java

好吧,我对getter和setter很困惑(也是Java的新手)。我正在处理的代码将在一个容器中有多个类。这些类是UserInput,Totals,Greeting和Checkboxes。我遇到的问题是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();

        }
}

0 个答案:

没有答案