将“退出”按钮转换为“重置”按钮

时间:2014-05-14 04:15:48

标签: java swing applet jbutton

好的 - 所以这是我第一次使用applet。我想将此程序转换为applet。我已修复(注释掉)applet不需要的项目,我的最后一步是将EXIT按钮替换为RESET按钮。这里缺少什么明显的东西吗?

错误

> PropertyTax99.java:43: error: cannot find symbol
>         ResetHandler rbHandler = new ResetHandler();
>         ^   symbol:   class ResetHandler   location: class PropertyTax99 PropertyTax99.java:43: error: cannot find symbol
>         ResetHandler rbHandler = new ResetHandler();
>                                      ^   symbol:   class ResetHandler   location: class PropertyTax99 PropertyTax99.java:93: error: cannot
> find symbol
>             reset();
>             ^   symbol:   method reset()   location: class PropertyTax99.resetHandler 3 errors

代码

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

public class PropertyTax99 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 reset button       
        JButton reset = new JButton("Reset");
        ResetHandler rbHandler = new ResetHandler();



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


    public PropertyTax99()
    {
    //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(reset);
        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);

    //reset Button
        reset.addActionListener(rbHandler);

    //Calculate Button
        calculate.addActionListener(cbHandler);  

    }

    //Handler for reset
    public class resetHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            reset();
        }
    }

    //Handler for calculate
    public 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 void init()
    {
    // main program to invoke constructor
    PropertyTax99 proptax = new PropertyTax99();
    }
}

2 个答案:

答案 0 :(得分:1)

你有两个基本问题......

您使用...

声明rbHandler
ResetHandler rbHandler = new ResetHandler();

但您的ResetHandler实际上已声明为

public class resetHandler implements ActionListener {

不是名字的差异。将类声明更改为public class ResetHandler implements ActionListener {

两个

代码中的任何位置都没有声明reset方法。

仅供参考:小程序不应该创建框架。如果您确实想要使程序可移植,请将核心UI移至JPanel,然后根据需要将其添加到JFrameJApplet

答案 1 :(得分:0)

我看到你有一个名为ResetHandler的类,它实现了ActionListener

在actionPerformed方法中,您调用了一个名为reset();.的方法 但是你在哪里声明了这个方法?

另一个问题是该线     ResetHandler rbHandler = new ResetHandler(); 类名中的r是小的,所以它将是

resetHandler rbHandler = new resetHandler();

最后,我想向您展示如何调试问题: 这条线

  

class ResetHandler location:class PropertyTax99   PropertyTax99.java:43:错误:找不到符号ResetHandler   rbHandler = new ResetHandler();

告诉我们这条线有问题。 Java找不到任何名为ResetHandler的对象。

  

找不到符号reset();符号:方法reset()location:class

告诉我们“reset();”行有些错误。 java找不到任何这样的方法。

  

PropertyTax99.resetHandler