Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm creating a simple GUI that counts the number of times you press a button. This is my code:

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

public class PushCounter 
{
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Push Counter");
		JPanel panel = new JPanel();
		JLabel label = new JLabel("Counter");
		JTextField textArea = new JTextField(7);
		JButton button = new JButton("Count");
		panel.add(label);
		panel.add(textArea);
		panel.add(button);
		frame.getContentPane().add(panel);
		frame.pack();
		frame.setVisible(true);
		
		ButtonListener listener = new ButtonListener();
		button.addActionListener(listener);
		
		
		final class ButtonListener implements ActionListener
		{
			int count = 0;
			
			@Override
			public void actionPerformed(ActionEvent event)
			{
				count++;
				textArea.setText(Integer.toString(count));
			}
		}
		
	}
}


For some reason I get an error that says "ButtonListener cannot be resolved to a type". How would you solve this? Also, do you have any suggestions on how to write the program?

What I have tried:

See code above........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Posted
Updated 14-Jul-17 5:34am

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900