简体   繁体   中英

JTextArea Displays 0 after calculate button clicked

I'm writing this code for university, to read the user input and calculate the charges (its a hospital bill)

But when I press calculate the JTextArea displays 0 as value

I'm very much a newbie so any guidance would be appreciated

the code is:

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

public class HospitalChargesCalculator extends JFrame implements ActionListener{

private JLabel hospitalStayLabel;
private JLabel medicationLabel;
private JLabel surgicalFeesLabel;
private JLabel labFeesLabel;
private JLabel rehabLabel;
private JLabel totalLabel;

private JTextField hospitalStayTF;
private JTextField medicationTF;
private JTextField surgicalFeesTF;
private JTextField labFeesTF;
private JTextField rehabTF;

private JTextArea totalChargesTA;

private JButton calculateB;
private JButton exitB;

public static final int WIDTH = 500;
public static final int HEIGHT = 350;

static int totalStayCharge;
static int totalMisc;
static int totalCharges;

static int totalDays;
static int totalMedication;
static int totalSurgical;
static int totalLab;
static int totalRehab;

public HospitalChargesCalculator() {
    setTitle("Hospital Charges");
    Container c = getContentPane();
    setSize(WIDTH, HEIGHT);

    c.setLayout(null);

    hospitalStayLabel = new JLabel(" Number of days spent in hospital: ",
            SwingConstants.LEFT);
    medicationLabel = new JLabel(" Total Medication Charges: ",
            SwingConstants.LEFT);
    surgicalFeesLabel = new JLabel(" Total sugical charges : ",
            SwingConstants.LEFT);
    labFeesLabel = new JLabel(" Total lab fees: ",
            SwingConstants.LEFT);
    rehabLabel = new JLabel(" Total Rehab charges: ",
            SwingConstants.LEFT);
    totalLabel = new JLabel(" Total Charges: ",
            SwingConstants.LEFT);

    calculateB = new JButton("Calculate");
    calculateB.addActionListener(this);
    exitB = new JButton("Exit");
    exitB.addActionListener(this);


    hospitalStayTF = new JTextField();
    medicationTF = new JTextField();
    surgicalFeesTF = new JTextField();
    labFeesTF = new JTextField();
    rehabTF = new JTextField();


    totalChargesTA = new JTextArea();

    hospitalStayLabel.setSize(250, 30);
    hospitalStayTF.setSize(200, 30);
    medicationLabel.setSize(200, 30);
    medicationTF.setSize(200, 30);
    surgicalFeesLabel.setSize(200, 30);
    surgicalFeesTF.setSize(200, 30);
    labFeesLabel.setSize(200, 30);
    labFeesTF.setSize(200, 30);
    rehabLabel.setSize(200, 30);
    rehabTF.setSize(200,30);
    totalLabel.setSize(200, 30);
    totalChargesTA.setSize(200,30);
    calculateB.setSize(100, 30);
    exitB.setSize(100, 30);

    hospitalStayLabel.setLocation(30, 25);
    hospitalStayTF.setLocation(250, 25);
    medicationLabel.setLocation(30, 60);
    medicationTF.setLocation(250, 60);
    surgicalFeesLabel.setLocation(30, 95);
    surgicalFeesTF.setLocation(250, 95);
    labFeesLabel.setLocation(30, 130);
    labFeesTF.setLocation(250, 130);
    rehabLabel.setLocation(30, 165);
    rehabTF.setLocation(250, 165);
    totalLabel.setLocation(30, 250);
    totalChargesTA.setLocation(250, 250);
    calculateB.setLocation(70, 205);
    exitB.setLocation(300, 205);

    c.add(hospitalStayLabel);
    c.add(hospitalStayTF);
    c.add(medicationLabel);
    c.add(medicationTF);
    c.add(surgicalFeesLabel);
    c.add(surgicalFeesTF);
    c.add(labFeesLabel);
    c.add(labFeesTF);
    c.add(rehabLabel);
    c.add(rehabTF);
    c.add(totalLabel);
    c.add(totalChargesTA);
    c.add(calculateB);
    c.add(exitB);

    hospitalStayTF.addActionListener(this);
    medicationTF.addActionListener(this);
    surgicalFeesTF.addActionListener(this);
    labFeesTF.addActionListener(this);
    rehabTF.addActionListener(this);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

}

public void actionPerformedGet(ActionEvent g)
{
    totalDays = Integer.parseInt(hospitalStayTF.getText());
    totalMedication = Integer.parseInt(medicationTF.getText());
    totalSurgical = Integer.parseInt(surgicalFeesTF.getText());
    totalLab = Integer.parseInt(labFeesTF.getText());
    totalRehab = Integer.parseInt(rehabTF.getText());
}

public int CalcStayCharges()
{
    int dailyCharge = 350;

    totalStayCharge = totalDays * dailyCharge;

    return totalStayCharge;
}

public int CalcMiscCharges()
{
    totalMisc = (totalMedication + totalSurgical + totalLab + totalRehab);

    return totalMisc;
}

public int CalcTotalCharges()
{
    totalCharges = (totalStayCharge + totalMisc);

    return totalCharges;
}

public void actionPerformed(ActionEvent e)
{
    if (e.getActionCommand().equals("Calculate"))
    {
        totalChargesTA.setText(String.valueOf(totalCharges));
    }
    else if (e.getActionCommand().equals("Exit"))
        System.exit(0);
}

public static void main(String[] args) 
{
    HospitalChargesCalculator hospCalc = new HospitalChargesCalculator();
}
}

if you press the button you simply execute actionPerformed(ActionEvent e) , which only does totalChargesTA.setText(String.valueOf(totalCharges)); . In order to get a value you should use any of your calculalationmethods before using the setText method.

public void actionPerformed(ActionEvent e)
{
    if (e.getActionCommand().equals("Calculate"))
    {
        totalCharges = CalcTotalCharges();
        totalChargesTA.setText(String.valueOf(totalCharges));
    }
    else if (e.getActionCommand().equals("Exit"))
        System.exit(0);
}

it might be that you need to call some of the other methods aswell, if they are calculating the values that are used inside of CalcTotalCharges .

I cant comment yet, so here as an answer.

As said you dont actually call the function to calculate your total charges with totalChargesTA.setText(String.valueOf(totalCharges)); but instead try to directly access the variable totalCharges of your method CalcTotalCharges() (the method you should be calling here), this is only possible because you declared all your variables as static int so you can access them even in parts of your code where you dont actually want to access them.

Dont declare all your variables static, and you would have seen the error of not calling the methods. Think about which variables need to be static and reduce it to those.

Also dont declare all your variables you are going to use in just that one function in your class, inistead declare them in that function, so that you avoid the same error as with your static declarations.

-- Also, additionaly the answer provided by Subin Thomas that shows the principal error in your code, his solution wont work because he mixed up some of the functions and you also have to fix the function:

public int CalcTotalCharges()
{
  totalCharges = (totalStayCharge + totalMisc);

  return totalCharges;
}

Where you have the same kind of error to:

public int CalcTotalCharges()
{
  totalCharges = (CalcStayCharges() + CalcMiscCharges());

  return totalCharges;
}

EDIT: Actually you also have another error in the way you retrieve the values from you input textfields. Why did you use the method public void actionPerformedGet(ActionEvent g) ?

This wont work with your ActionListener. Instead copy the code in this function to your actionPerformed Method. Then it will actually work. For completeness here the working code:

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

public class HospitalChargesCalculator extends JFrame implements ActionListener{

private JLabel hospitalStayLabel;
private JLabel medicationLabel;
private JLabel surgicalFeesLabel;
private JLabel labFeesLabel;
private JLabel rehabLabel;
private JLabel totalLabel;

private JTextField hospitalStayTF;
private JTextField medicationTF;
private JTextField surgicalFeesTF;
private JTextField labFeesTF;
private JTextField rehabTF;

private JTextArea totalChargesTA;

private JButton calculateB;
private JButton exitB;

public static final int WIDTH = 500;
public static final int HEIGHT = 350;


int totalDays;
int totalMedication;
int totalSurgical;
int totalLab;
int totalRehab;

public HospitalChargesCalculator() {
setTitle("Hospital Charges");
Container c = getContentPane();
setSize(WIDTH, HEIGHT);

c.setLayout(null);

hospitalStayLabel = new JLabel(" Number of days spent in hospital: ",
        SwingConstants.LEFT);
medicationLabel = new JLabel(" Total Medication Charges: ",
        SwingConstants.LEFT);
surgicalFeesLabel = new JLabel(" Total sugical charges : ",
        SwingConstants.LEFT);
labFeesLabel = new JLabel(" Total lab fees: ",
        SwingConstants.LEFT);
rehabLabel = new JLabel(" Total Rehab charges: ",
        SwingConstants.LEFT);
totalLabel = new JLabel(" Total Charges: ",
        SwingConstants.LEFT);

calculateB = new JButton("Calculate");
calculateB.addActionListener(this);
exitB = new JButton("Exit");
exitB.addActionListener(this);


hospitalStayTF = new JTextField();
medicationTF = new JTextField();
surgicalFeesTF = new JTextField();
labFeesTF = new JTextField();
rehabTF = new JTextField();


totalChargesTA = new JTextArea();

hospitalStayLabel.setSize(250, 30);
hospitalStayTF.setSize(200, 30);
medicationLabel.setSize(200, 30);
medicationTF.setSize(200, 30);
surgicalFeesLabel.setSize(200, 30);
surgicalFeesTF.setSize(200, 30);
labFeesLabel.setSize(200, 30);
labFeesTF.setSize(200, 30);
rehabLabel.setSize(200, 30);
rehabTF.setSize(200,30);
totalLabel.setSize(200, 30);
totalChargesTA.setSize(200,30);
calculateB.setSize(100, 30);
exitB.setSize(100, 30);

hospitalStayLabel.setLocation(30, 25);
hospitalStayTF.setLocation(250, 25);
medicationLabel.setLocation(30, 60);
medicationTF.setLocation(250, 60);
surgicalFeesLabel.setLocation(30, 95);
surgicalFeesTF.setLocation(250, 95);
labFeesLabel.setLocation(30, 130);
labFeesTF.setLocation(250, 130);
rehabLabel.setLocation(30, 165);
rehabTF.setLocation(250, 165);
totalLabel.setLocation(30, 250);
totalChargesTA.setLocation(250, 250);
calculateB.setLocation(70, 205);
exitB.setLocation(300, 205);

c.add(hospitalStayLabel);
c.add(hospitalStayTF);
c.add(medicationLabel);
c.add(medicationTF);
c.add(surgicalFeesLabel);
c.add(surgicalFeesTF);
c.add(labFeesLabel);
c.add(labFeesTF);
c.add(rehabLabel);
c.add(rehabTF);
c.add(totalLabel);
c.add(totalChargesTA);
c.add(calculateB);
c.add(exitB);

hospitalStayTF.addActionListener(this);
medicationTF.addActionListener(this);
surgicalFeesTF.addActionListener(this);
labFeesTF.addActionListener(this);
rehabTF.addActionListener(this);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

}


public int CalcStayCharges()
{
int dailyCharge = 350;

int totalStayCharge = totalDays * dailyCharge;

return totalStayCharge;
}

public int CalcMiscCharges()
{
int totalMisc = (totalMedication + totalSurgical + totalLab + totalRehab);

return totalMisc;
}

public int CalcTotalCharges()
{
int totalCharges = (CalcStayCharges() + CalcMiscCharges());

return totalCharges;
}

public void actionPerformed(ActionEvent e)
{

totalDays = Integer.parseInt(hospitalStayTF.getText());
totalMedication = Integer.parseInt(medicationTF.getText());
totalSurgical = Integer.parseInt(surgicalFeesTF.getText());
totalLab = Integer.parseInt(labFeesTF.getText());
totalRehab = Integer.parseInt(rehabTF.getText());

if (e.getActionCommand().equals("Calculate"))
{
    totalChargesTA.setText(CalcTotalCharges()+"");
}
else if (e.getActionCommand().equals("Exit"))
    System.exit(0);
}

public static void main(String[] args) 
{
HospitalChargesCalculator hospCalc = new HospitalChargesCalculator();
}
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM