前言,我是一名学生并学习Java。我很享受,但是当他们不工作时,简单的事情是非常令人沮丧的。我收到以下错误。
PropertyTax2.java:85: error: cannot find symbol
public class ExitHandler implements ActionListener
^
symbol: class ActionListener
location: class PropertyTax2
PropertyTax2.java:87: error: cannot find symbol
public void actionPerformed(ActionEvent e)
^
symbol: class ActionEvent
location: class PropertyTax2.ExitHandler
PropertyTax2.java:81: error: method addActionListener in class AbstractButton cannot be applied to given types;
exit.addActionListener(ebHandler);
^
required: ActionListener
found: PropertyTax2.ExitHandler
reason: actual argument PropertyTax2.ExitHandler cannot be converted to ActionListener by method invocation conversion
3 errors
代码:
import javax.swing.*;
import java.awt.*;
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 schoolTaxTF = new JTextField(10);
JTextField countyTaxTF = new JTextField(10);
JTextField totalSchoolTaxTF = new JTextField(10);
JTextField totalCountyTaxTF = new JTextField(10);
JTextField totalTaxesTF = new JTextField(10);
//Declare and Exit button
JButton exit = new JButton("Exit");
ExitHandler ebHandler = new ExitHandler();
//Declare and Calculate button
JButton calculate = new JButton("Calculate");
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(schoolTaxTF);
pane.add(countyTaxL);
pane.add(countyTaxTF);
pane.add(totalSchoolTaxL);
pane.add(totalSchoolTaxTF);
pane.add(totalCountyTaxL);
pane.add(totalCountyTaxTF);
pane.add(totalTaxesL);
pane.add(totalTaxesTF);
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.addActionListener(ebHandler);
}
public class ExitHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
// main program to invoke constructor
PropertyTax2 proptax = new PropertyTax2();
}
}
答案 0 :(得分:2)
将此行移至构造函数中:
exit.addActionListener(ebHandler);