java - "Cannot instantiate the Type Main" -
i keep getting error every time create new object class name:
"cannot instantiate type main"
this code:
import javax.swing.*; import java.util.scanner; import java.applet.applet; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.geom.ellipse2d; public abstract class main extends jpanel implements actionlistener, keylistener{ timer t = new timer(5, this); double x = 0, y = 0, velx = 0, vely = 0; public main() { t.start(); addkeylistener(this); setfocusable(true); setfocustraversalkeysenabled(false); } public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g; g2.fill(new ellipse2d.double(x, y, 40, 40)); } public void actionperformed(actionevent e) { repaint(); x += velx; y += vely; } public void up() { vely = -1.5; velx = 0; } public void down() { vely = -1.5; velx = 0; } public void left() { velx = -1.5; vely = 0; } public void right() { velx = -1.5; vely = 0; } public void keypressed(keyevent e) { int code = e.getkeycode(); if (code == keyevent.vk_up) { up(); } if (code == keyevent.vk_down) { down(); } if (code == keyevent.vk_left) { left(); } if (code == keyevent.vk_right) { right(); } main m = new main(); jframe f = new jframe(); f.add(m); f.setvisible(true);; f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(800,600); } public void keytyped(keyevent e) {} public void keyreleased(keyevent e) {} }
and object keeps getting "error":
main m = new main();
(i very new making ui's, infact, 3rd, have no idea why cannot instantiate type)
you can't instantiate abstract classes in java, main
class abstract, make concrete class, since seems have functionalists , no need declare abstract class.
public class main extends jpanel implements actionlistener, keylistener { } //abstract keyword removed
consider using abstract classes if of these statements apply situation:
- you want share code among several closely related classes.
- you expect classes extend abstract class have many common methods or fields, or require access modifiers other public (such protected , private).
- you want declare non-static or non-final fields. enables define methods can access , modify state of object belong.
Comments
Post a Comment