gpt4 book ai didi

java - CarPark 不是抽象的,不会重写抽象方法(需要帮助)

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

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

/**
* This programs counts the cars entering and leaving a car park, always displaying
* the current number of cars in the car park.
*/
public class CarPark extends JFrame
implements ActionListener{

/**
* Globally accessible counter for the number of cars in the car park
*/
private int carCount = 0;

/**
* Globally accessible counter for the number of mini buses in the car park
*/
private int miniCount = 0;

/**
* Globally accessible counter for the total amount of spaces left
*/
private int limitCalc = 100;

/**
* Buttons to simulate cars entering and leaving the car park.
*/
private JButton enter, exit, miniEnter, miniExit;

/**
* Text field where the current number of cars is displayed
*/
private JTextField text, miniText, carMax;

/**
* The main launch method
*/

public static void main(String[] args)
{
CarPark frame = new CarPark();
frame.setSize(400, 250);
frame.setLocation(150, 150);
frame.setTitle("Car Park");
frame.createGUI();
frame.setVisible(true);

}

/**
* React to a GUI button press by adjusting the car counter correctly,
* and then updating the counter display
*/
/**
* Helper method to build up the GUI
*/
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());

int carLimit = 100;

enter = new JButton("Car Entering");
window.add(enter);
enter.addActionListener(this);

text = new JTextField("0 ");
text.setFont(new Font("Arial", Font.BOLD, 40));
window.add(text);

exit = new JButton("Car Exiting");
window.add(exit);
exit.addActionListener(this);

miniEnter = new JButton("Minibus Entering");
window.add(miniEnter);
miniEnter.addActionListener(this);

miniText = new JTextField("0 ");
miniText.setFont(new Font("Arial", Font.BOLD, 40));
window.add(miniText);

miniExit = new JButton("Minibus Exiting");
window.add(miniExit);
miniExit.addActionListener(this);

carMax = new JTextField(carLimit + "");
carMax.setFont(new Font("Arial", Font.BOLD, 40));
window.add(carMax);
}

public void actionPerformed(ActionEvent event, int carLimit)
{
if (event.getSource() == enter)
{
carCount = carCount + 1;
limitCalc = limitCalc-1;
System.out.println(limitCalc);
}
if (event.getSource() == exit)
{
carCount = carCount-1;
limitCalc = limitCalc +1;
System.out.println(limitCalc);
}
if (event.getSource() == miniEnter)
{
miniCount = miniCount+1;
limitCalc = limitCalc -1;
System.out.println(limitCalc);
}
if (event.getSource() == miniExit)
{
miniCount = miniCount-1;
limitCalc = limitCalc +1;
System.out.println(limitCalc);
}

miniText.setText(Integer.toString(miniCount));
text.setText(Integer.toString(carCount));

}

private int carCalculations(int carLimit)
{
int carTotal = carCount + miniCount;
limitCalc = limitCalc - carTotal;
carLimit = limitCalc;
System.out.println(carLimit);
return carLimit;
}
}

我不断收到此错误

"CarPark is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener

我做错了什么?提前致谢

最佳答案

您不是重写 actionPerformed(ActionEvent) 方法,而是使用 actionPerformed(ActionEvent, int) 重载它。

考虑如何从方法签名中删除 int 部分。

关于java - CarPark 不是抽象的,不会重写抽象方法(需要帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22281825/

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