Results 1 to 2 of 2
Thread: abstract classes
- 12-09-2012, 02:30 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
abstract classes
I am trying to create a simple action listener. I am getting an error that says:
my code:Java Code:GUI.buttonHandler is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
If you could please explain what I am doing wrong, because I have done research, and I can't understand. It has to do with the subclass, or something of the sort?Java Code:public class GUI extends JFrame { private static final int WIDTH = 400; private static final int HEIGHT = 300; private JLabel name; private JTextField textField; private JButton submit; private buttonHandler btnHandler; public static void main(String[] args) { GUI gui = new GUI(); } public GUI() { setTitle("A sameple"); setSize(WIDTH, HEIGHT); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); name = new JLabel("Name: ", SwingConstants.LEFT); textField = new JTextField(10); submit = new JButton("Submit"); btnHandler = new buttonHandler(); submit.addActionListener(btnHandler); setTitle("Sample Title"); Container pane = getContentPane(); pane.setLayout(new GridLayout(4, 2)); pane.add(name); pane.add(textField); pane.add(submit); setSize(WIDTH, HEIGHT); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } private class buttonHandler implements ActionListener // it does not like buttonHandler here { public void actionPeformed(ActionEvent e) { textField.setText("Good!!!!(:"); } }
- 12-09-2012, 02:44 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: abstract classes
So check the spelling of the method you thought was actionPerformed().does not override abstract method actionPerformed(ActionEvent)
Sometimes people try and guard against this by using the @Override annotation.
@Override means "the following method must be declared in the super type". Written this way the compiler will alert you to the source of the problem because actionPeformed() is not declared in ActionListener.Java Code:private class buttonHandler implements ActionListener { @Override public void actionPeformed(ActionEvent e) { textField.setText("Good!!!!(:"); } }
Similar Threads
-
Help with Abstract Classes please.
By fatabass in forum New To JavaReplies: 1Last Post: 03-23-2012, 03:26 AM -
Abstract classes
By FadedAura in forum New To JavaReplies: 4Last Post: 11-21-2011, 10:16 PM -
Abstract Classes and ArrayLists.
By Hooky75 in forum New To JavaReplies: 9Last Post: 05-14-2011, 09:38 AM -
Abstract Classes.
By maknib in forum New To JavaReplies: 3Last Post: 05-12-2011, 02:30 PM -
abstract classes
By renju krishnan in forum New To JavaReplies: 1Last Post: 09-29-2010, 08:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks