gpt4 book ai didi

java - 简单的退出按钮不起作用

转载 作者:行者123 更新时间:2023-12-02 06:02:23 26 4
gpt4 key购买 nike

作为前言,我是一名学生,正在学习 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();
}



}

最佳答案

将此行移至构造函数中:

exit.addActionListener(ebHandler);

关于java - 简单的退出按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22570748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com