繁体   English   中英

计算按钮不起作用

[英]The Calculate button won't work

好的,所以我知道我的大多数代码是正确的,并且我认为接口处理程序应该在构造函数之外,但在类内部。 我还是

PropertyTax2.java:111: error: ';' expected
    public void actionPerformed(ActionEvent e)
                                              ^

显然; 在该行之后不需要。 至少我认为它不是必需的。

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

public class PropertyTax2 extends JFrame
{
    // set parameters to define extent of the window
    //changed width to 500 since the words were getting cut off
    private static final int WIDTH = 500, HEIGHT = 300; 

    //Declare and initialize 6 JLabels
        JLabel assessL = new JLabel("Assessment Home Value: ", SwingConstants.RIGHT);
        JLabel schoolTaxL = new JLabel("Decimal Value of School Tax Rate: ", SwingConstants.RIGHT);
        JLabel countyTaxL = new JLabel("Decimal Value of County Tax Rate: ", SwingConstants.RIGHT);
        JLabel totalSchoolTaxL = new JLabel("School Taxes: ", SwingConstants.RIGHT);
        JLabel totalCountyTaxL = new JLabel("County Taxes: ", SwingConstants.RIGHT);
        JLabel totalTaxesL = new JLabel("Total Taxes: ", SwingConstants.RIGHT);

    //Declate and initialize 5 JTextFields
        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);

    //Declare and initialize Exit button

        JButton exit = new JButton("Exit");
        ExitHandler ebHandler = new ExitHandler();


    //Declare and initialize Calculate button
        JButton calculate = new JButton("Calculate"); 
        CalculateHandler cbHandler = new CalculateHandler();


    public PropertyTax2()
    {
    //Declare and initialize a container
        Container pane = getContentPane();
    //Set the container layout
        pane.setLayout(new GridLayout(7,2));
    //Set GUI objects in the container
        pane.add(assessL);
        pane.add(assessTF);
        pane.add(schoolTaxL);
        pane.add(schoolrateTF);
        pane.add(countyTaxL);
        pane.add(countyrateTF);
        pane.add(totalSchoolTaxL);
        pane.add(schooltaxTF);
        pane.add(totalCountyTaxL);
        pane.add(countytaxTF);
        pane.add(totalTaxesL);   
        pane.add(totaltaxTF);
        pane.add(exit);
        pane.add(calculate);


    // set title, size and visibility aspects of window
    setTitle("Calculation of Property Taxes");
    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    //Exit Button
    exit.addActionListener(ebHandler);

    //Calculate Button
    calculate.addActionListener(cbHandler);
    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));


    }

    //Handler to Exit
    public class ExitHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }

    //Handler for calculate
    public class CalculateHandler implements ActionListener
        {
        public void actionPerformed(ActionEvent e)
        }


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



}

您遇到了问题,因为您忘记了方法的括号。 我在下面包含了更正的代码

 //Handler for calculate
public class CalculateHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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