java - ActionPerformed does not work -
have little problem code. actionperformed
method doesn't work. buttons knappstartsalg
, knappstartkunde
, don't react when push buttons.
all should have been imported imported.
will thankful help.
startmeny class.
public class startmeny extends jframe implements actionlistener { public jbutton knappstartsalg, knappstartkunde, knappstartinfo, knappstartstatistikk; public jpanel startmeny() { jpanel startpanel = new jpanel(); startpanel.setlayout(new gridlayout(2, 0, 25, 25) ); startpanel.setcomponentorientation(componentorientation.left_to_right); startpanel.setbackground(color.white); jbutton knappstartsalg = new jbutton(); knappstartsalg.settext("salg"); knappstartsalg.setverticaltextposition(jbutton.bottom); knappstartsalg.sethorizontaltextposition(jbutton.center); knappstartsalg.seticon(new javax.swing.imageicon(getclass().getresource("salg.png"))); knappstartsalg.seticontextgap(6); knappstartsalg.setforeground(color.black); knappstartsalg.setbackground(color.white); knappstartsalg.setborderpainted(false); knappstartsalg.addactionlistener(this); startpanel.add(knappstartsalg); jbutton knappstartkunde = new jbutton(); knappstartkunde.settext("kontroll"); knappstartkunde.setverticaltextposition(jbutton.bottom); knappstartkunde.sethorizontaltextposition(jbutton.center); knappstartkunde.seticon(new javax.swing.imageicon(getclass().getresource("heiskontroll.png"))); knappstartkunde.seticontextgap(6); knappstartkunde.setforeground(color.black); knappstartkunde.setbackground(color.white); knappstartkunde.setborderpainted(false); knappstartkunde.addactionlistener(this); startpanel.add(knappstartkunde); jbutton knappstartinfo = new jbutton(); knappstartinfo.settext("informasjonsvindu"); knappstartinfo.setverticaltextposition(jbutton.bottom); knappstartinfo.sethorizontaltextposition(jbutton.center); knappstartinfo.seticon(new javax.swing.imageicon(getclass().getresource("info.png"))); knappstartinfo.seticontextgap(6); knappstartinfo.setforeground(color.black); knappstartinfo.setbackground(color.white); knappstartinfo.setborderpainted(false); knappstartinfo.addactionlistener(this); startpanel.add(knappstartinfo); jbutton knappstartstatistikk = new jbutton(); knappstartstatistikk.settext("statistikk"); knappstartstatistikk.setverticaltextposition(jbutton.bottom); knappstartstatistikk.sethorizontaltextposition(jbutton.center); knappstartstatistikk.seticon(new javax.swing.imageicon(getclass().getresource("statistikk.png"))); knappstartstatistikk.seticontextgap(6); knappstartstatistikk.setforeground(color.black); knappstartstatistikk.setbackground(color.white); knappstartstatistikk.setborderpainted(false); knappstartstatistikk.addactionlistener(this); startpanel.add(knappstartstatistikk ); return startpanel; } @override public void actionperformed(actionevent e) { salgsvindu s = new salgsvindu(); if(e.getsource() == knappstartsalg) { s.vissalgvindunybruker(); system.out.println("hallotest"); } else if(e.getsource() == knappstartkunde) s.viskontrollvindu(); } }
main-class, runs gui.
public class skisenter { public static void main(string[] args) { /*salgsvindu s = new salgsvindu(); s.vissalgvindunybruker();*/ startmeny startmenyinstanse = new startmeny(); startmenyinstanse.setsize(600, 630); startmenyinstanse.settitle("startmeny skisenter"); startmeny st = new startmeny(); startmenyinstanse.setcontentpane(st.startmeny()); startmenyinstanse.setvisible(true); startmenyinstanse.setdefaultcloseoperation(jframe.exit_on_close); } }
in class, define instance variables:
public jbutton knappstartsalg, knappstartkunde, knappstartinfo, knappstartstatistikk;
you use these variables in action listener implementation. in constructor, construct jbutton
s , assign them new local variables, not instance variables declare above constructor. instead of:
jbutton knappstartsalg = new jbutton();
write
knappstartsalg = new jbutton();
to assign buttons instance variables , action listener should work.
Comments
Post a Comment