Java JTextfield财产税的输入和输出

时间:2014-03-01 00:01:44

标签: java swing

我一直在尝试运行此代码,该代码获取用户评估的房屋价值,县和学校税,然后将其输出到文本字段。此代码编译并运行,但某处存在逻辑错误,不会输出任何文本。我刚刚开始使用Java,所以任何帮助都会受到赞赏。

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

public class PropertyTax3 extends JFrame
{
    // set parameters to define extent of the window
private static final int WIDTH = 500, HEIGHT = 300;
private ExitHandler ebHandler;
private CalculateHandler cbHandler;
private JTextField assessTF, schoolRateTF, countyRateTF, schoolTaxTF, countyTaxTF,         totalTaxTF;

public PropertyTax3()
{
// set title, size and visibility aspects of window
setTitle("Calculation of Property Taxes");
//Label Definitions
JLabel assessL = new JLabel("Assesment Home Value:", SwingConstants.RIGHT);
JLabel schoolRateL = new JLabel("Decimal Value of School Tax Rate:", SwingConstants.RIGHT);
JLabel countyRateL = new JLabel("Decimal Value of County Tax Rate:", SwingConstants.RIGHT);
JLabel schoolTaxL = new JLabel("School Taxes:", SwingConstants.RIGHT);
JLabel countyTaxL = new JLabel("County Taxes:", SwingConstants.RIGHT);
JLabel totalTaxL = new JLabel("Total Taxes:", SwingConstants.RIGHT);
//Text Field Definitions
JTextField assessTF = new JTextField(10);
JTextField schoolRateTF = new JTextField(10);
JTextField countyRateTF = new JTextField(10);
JTextField schoolTaxTF = new JTextField(10);
JTextField countyTaxTF = new JTextField(10);
JTextField totalTaxTF = new JTextField(10);

//Exit Button
JButton exit = new JButton("Exit");
ebHandler = new ExitHandler();
exit.addActionListener(ebHandler);

//Calculate Button
JButton calculate = new JButton("Calculate");
cbHandler = new CalculateHandler();
calculate.addActionListener(cbHandler);

//Container format
Container pane = getContentPane();
pane.setLayout(new GridLayout(7,2));
pane.add(assessL);
pane.add(assessTF);
pane.add(schoolRateL);
pane.add(schoolRateTF);
pane.add(countyRateL);
pane.add(countyRateTF);
pane.add(schoolTaxL);
pane.add(schoolTaxTF);
pane.add(countyTaxL);
pane.add(countyTaxTF);
pane.add(totalTaxL);
pane.add(totalTaxTF);
pane.add(exit);
pane.add(calculate);

setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}
private class ExitHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
    {
     System.exit(0);
    }
}
private class CalculateHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
    double countyRate, schoolRate, assessment, schoolTax, countyTax, totalTax;      
    assessment = Double.parseDouble(assessTF.getText());
    schoolRate = Double.parseDouble(schoolRateTF.getText());
    countyRate = Double.parseDouble(countyRateTF.getText());
    schoolTax = assessment * schoolRate * .01;
    countyTax = assessment * countyRate * .01;
    totalTax = schoolTax + countyTax;
    schoolTaxTF.setText(""+ String.format("%.2f", schoolTax));
    countyTaxTF.setText(""+ String.format("%.2f", countyTax));
    totalTaxTF.setText(""+ String.format("%.2f", totalTax));
}
}


public static void main(String[] args)
{
// main program to invoke constructor
PropertyTax3 proptax = new PropertyTax3();

}

}

2 个答案:

答案 0 :(得分:1)

更改此

JTextField assessTF = new JTextField(10);
JTextField schoolRateTF = new JTextField(10);
JTextField countyRateTF = new JTextField(10);
JTextField schoolTaxTF = new JTextField(10);
JTextField countyTaxTF = new JTextField(10);
JTextField totalTaxTF = new JTextField(10);

到这个

assessTF = new JTextField(10);
schoolRateTF = new JTextField(10);
countyRateTF = new JTextField(10);
schoolTaxTF = new JTextField(10);
countyTaxTF = new JTextField(10);
totalTaxTF = new JTextField(10);

这样,您不是使用您定义为类字段的变量,而是创建新的变量。这就是您之后无法使用getTextsetText访问它们的原因。

答案 1 :(得分:0)

那是因为没有值与程序一起运行...