java - Identifier expected in an ActionListener -
i compiled code , got error:
c:\users\rj\desktop>javac windowtest.java windowtest.java:17: error: <identifier> expected click.addactionlistener(new actionlistener(){ ^ windowtest.java:22: error: ';' expected }); ^ 2 errors
i still new java , appreciate examples and/or step step explanations. if move override error saying didn't override. code is:
import java.awt.*; import java.awt.event.*; import javax.swing.jframe; import javax.swing.*; import java.util.scanner; public class windowtest extends jframe implements actionlistener{ public static void main(string args[]){ jframe frame = new jframe(); jlabel label = new jlabel("this test."); jbutton click = new jbutton("test"); jpanel buttonpan = new jpanel(); jpanel textpan = new jpanel(); final jtextfield textin = new jtextfield(); @override click.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ string text = textin.gettext(); system.out.println(text); } }); buttonpan.setsize(100, 100); textin.setpreferredsize(new dimension(700, 48)); textpan.add(textin); buttonpan.add(click); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(800, 700); frame.setlocationrelativeto(null); frame.setlayout(new borderlayout()); label.sethorizontalalignment(jlabel.center); label.setverticalalignment(jlabel.center); frame.add(label, borderlayout.north); frame.add(buttonpan, borderlayout.south); frame.add(textpan, borderlayout.center); frame.setvisible(true); frame.settitle("window test"); } }
first problem, need move @override
down, because annotation applies method definition, inside anonymous class.
click.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e){ string text = textin.gettext(); system.out.println(text); } });
second problem, windowtest
class claims it implements actionlistener
not implement actionperformed
. since not seem need it, can remove in declaration.
public class windowtest extends jframe{ ... }
with these 2 changes, application compile , run.
Comments
Post a Comment