我是为大学编写此代码,阅读用户输入并计算费用(它是医院账单)
但是,当我按下计算时,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();
}
}
答案 0 :(得分:1)
如果您按下按钮,则只需执行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
如果他们正在计算WHERE NOW() < date_add(ServerRequests.date, interval 10 second)
内部使用的值,则可能需要调用其他一些方法。
答案 1 :(得分:0)
我无法评论,所以这里作为答案。
如上所述,您实际上并未使用totalChargesTA.setText(String.valueOf(totalCharges));
来调用函数计算总费用,而是尝试直接访问方法totalCharges
的变量CalcTotalCharges()
(你应该在这里调用的方法),这是唯一可能的,因为你将所有你的变量声明为static int
,所以你甚至可以在代码的某些部分中访问它们>希望访问它们。
不要将所有变量声明为静态,你会看到不调用方法的错误。考虑哪些变量需要是静态的,并将其减少为那些。
也不要声明你将在中使用的所有变量只在你的类中使用一个函数,inistead在该函数中声明它们,这样就可以避免与静态声明相同的错误。
- 另外,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();
}
}