JTextArea 在单击计算按钮后显示 0

JTextArea Displays 0 after calculate button clicked

我正在为大学编写这段代码,以读取用户输入并计算费用(它是医院账单)

但是当我按下计算时 JTextArea 显示 0 作为值

我是一个新手,所以任何指导都将不胜感激

密码是:

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();
}
}

如果您按下按钮,您只需执行 actionPerformed(ActionEvent e),这只会执行 totalChargesTA.setText(String.valueOf(totalCharges));。为了获得一个值,您应该在使用 setText 方法之前使用任何计算方法。

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);
}

您可能还需要调用其他一些方法,如果它们正在计算 CalcTotalCharges.

中使用的值

我还不能评论,所以在这里作为答案。

如前所述,您实际上并没有调用 函数 来计算您使用 totalChargesTA.setText(String.valueOf(totalCharges)); 的总费用,而是尝试直接访问您方法的变量 totalCharges CalcTotalCharges() (你应该在这里调用的方法),这是唯一可能的,因为你声明 all 你的变量为 static int 所以你甚至可以在部分您实际上不想访问它们的代码。

不要将所有变量都声明为静态,否则您会看到未调用方法的错误。想想哪些变量 需要 是静态的,并将其减少到那些。

另外,不要在 中仅声明一个 函数中的所有变量,而是在该函数中声明它们,这样您就可以避免与您的静态声明相同的错误。

-- 另外,Subin Thomas 提供的答案显示了您的代码中的主要错误,他的解决方案不会起作用,因为他混淆了一些功能,您还必须修复该功能:

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

  return totalCharges;
}

您遇到与以下相同类型的错误:

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

  return totalCharges;
}

编辑:实际上,从输入文本字段中检索值的方式还有另一个错误。为什么要使用 public void actionPerformedGet(ActionEvent g) 方法?

这不适用于您的 ActionListener。而是将此函数中的代码复制到您的 actionPerformed 方法。然后它就会真正起作用。为了完整起见,工作代码:

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();
}
}