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
我做错了什么?提前致谢
答案 0 :(得分:2)
而不是覆盖actionPerformed(ActionEvent)
方法,而是使用actionPerformed(ActionEvent, int)
重载它。
考虑一下如何从方法签名中删除int
部分。