“计算”按钮不起作用

时间:2014-03-21 23:27:47

标签: java swing compiler-errors jbutton

好的,所以我知道我的大部分代码都是正确的,我认为接口处理程序应该在构造函数之外但在类中。我还是得到了

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



}

1 个答案:

答案 0 :(得分:0)

您遇到问题,因为您忘记了方法中的括号。我已将纠正后的代码包括在内

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