Error "TitleChanger is abstract; cannot be instantiated"
Hey, I'm fairly new to java, been reading sams teach yourself java 6 in 21 days.
I ran into an error with a code in the day 12 section.
Code :
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
[COLOR="DarkRed"]public class TitleChanger extends JFrame implements ActionListener[/COLOR]
{
JButton b1 = new JButton("Rosencrantz");
JButton b2 = new JButton("Guildenstern");
public TitleChanger()
{
super("Title Bar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
FlowLayout flow = new FlowLayout();
setLayout(flow);
add(b1);
add(b2);
pack();
setVisible(true);
}
public void actionPreformed(ActionEvent evt)
{
Object source = evt.getSource();
if (source == b1)
{
setTitle("Rosencrantz");
}
else if (source == b2)
{
setTitle("Guildenstern");
}
repaint();
}
public static void main(String[] arguments)
{
TitleChanger frame = new TitleChanger();
}
}
Now whenever i try to compile this, it will say that TitleChanger is not abstract and cannot override abstract method actionPreformed(java.awt.event.ActionEvent).
After i declare the main class as abstract however.
Code :
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public abstract class TitleChanger extends JFrame implements ActionListener
{
JButton b1 = new JButton("Rosencrantz");
JButton b2 = new JButton("Guildenstern");
public TitleChanger()
{
super("Title Bar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
FlowLayout flow = new FlowLayout();
setLayout(flow);
add(b1);
add(b2);
pack();
setVisible(true);
}
public void actionPreformed(ActionEvent evt)
{
Object source = evt.getSource();
if (source == b1)
{
setTitle("Rosencrantz");
}
else if (source == b2)
{
setTitle("Guildenstern");
}
repaint();
}
public static void main(String[] arguments)
{
[COLOR="DarkRed"]TitleChanger frame = new TitleChanger();[/COLOR]
}
}
The compiler will say that TitleChanger is abstract; cannot be instantiated.
TitleChanger frame = new TitleChanger();
I am currently using Windows Vista, and was wondering if anyone might have a solution, or be able to point me to another source.
Re: Abstract problem in java using vista
Hello Uzual, welcome to the Java Programming Forums.
You have made a very small mistake which has stopped the entire program from running properly.
I'm sure you will kick yourself any minute now!!
The first code you submitted was correct, except you misspelled the actionPerformed method.
Change:
Code :
public void actionPreformed(ActionEvent evt)
to:
Code :
public void actionPerformed(ActionEvent evt)
Sorted! :)
Re: Abstract problem in java using vista