java - JButton Action Listener not working -


i'm making short text adventure game first big application, , i've run problem. i'm using gui game, , problem action listener i'm using second button, proceed. of other code works, except method buttonaction. added action listener proceed , whenever run code , click proceed, nothing happens , can't figure out why. moved code found in init2() actionperformed(), had no effect. i've tried using @override on buttonaction, gives me error "the method buttonaction(actionevent) of type simplegui must override or implement supertype method". please keep in mind java skills @ best, elementary, please try explain best can. appreciated. code application below.

import java.awt.borderlayout; import java.awt.color; import java.awt.event.actionevent; import java.awt.event.actionlistener;  import javax.swing.borderfactory; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jtextfield;  import classes.rogue; import classes.warrior; import classes.wizard;  public class simplegui extends jframe implements actionlistener {  public jpanel panelcontrol, panel, panel2, panel3, panel4; public jbutton create, proceed; public jtextfield name; public final jtextfield textfield = new jtextfield(); public jcombobox playerclass; public char player1; public string[] classoptions = { "rogue", "wizard", "warrior" }; public jlabel textobject;  simplegui() {      super("rpg quest");     name = new jtextfield(20);     init();     this.setvisible(true);     this.setsize(455, 250);     this.setresizable(false); }// end simplegui  public void init() {      panel = new jpanel();     panel3 = new jpanel();     panel2 = new jpanel();     playerclass = new jcombobox(classoptions);     create = new jbutton("create character");     create.addactionlistener(this);     textobject = new jlabel("name");      panel.setborder(borderfactory             .createtitledborder("<html><font color:black>create character</font></html>"));     panel.add(textobject);     panel.add(name);     panel.add(playerclass);     panel.setbackground(color.white);      panel3.setborder(borderfactory             .createtitledborder("<html><font color:black>class descriptions</font></html>"));     panel3.add(new jlabel(             "<html><font color:black>the rogue has 20% chance land critical hits on enemies.</font></html>"));     panel3.add(new jlabel(             "<html><font color:black>the wizard can cast spells , has 70% chance of landing hit,</font></html>"));     panel3.add(new jlabel(             "<html><font color:black>but spells more damage melee attacks.</font></html>"));     panel3.add(new jlabel(             "<html><font color:black>the warrior has 30% chance block incoming attack.</font></html>"));     panel3.setbackground(color.white);      panel2.add(create);     panel2.setbackground(color.white);      this.add(panel);     this.add(panel3);     this.add(panel2);     this.pack();     this.setresizable(true);     this.setlayout(new borderlayout());      this.add(panel, borderlayout.north);     this.add(panel3);     this.add(panel2, borderlayout.south); }// end void  @override public void actionperformed(actionevent event) {     if (event.getsource() == create) {          string type = classoptions[playerclass.getselectedindex()];          if (type == "rogue") {             player1 = new rogue();         }         if (type == "wizard") {             player1 = new wizard();         }         if (type == "warrior") {             player1 = new warrior();         }         player1.name = name.gettext();          joptionpane.showmessagedialog(this, "you " + type                 + ". name " + player1.name + ".");          init2();     } }  public void init2() {      this.remove(panel);     this.remove(panel2);     this.remove(panel3);      panel.remove(textobject);     panel.remove(name);     panel.remove(playerclass);     panel.setborder(borderfactory             .createtitledborder("<html><font color:black>info</font></html>"));     panel.setbackground(color.white);     proceed = new jbutton("proceed");     proceed.addactionlistener(this);     panel.add(proceed);      textfield.settext("hello there");     textfield.seteditable(false);      panelcontrol = new jpanel();     panelcontrol.setbackground(color.white);     panelcontrol.setborder(borderfactory.createbevelborder(normal));     panelcontrol.add(textfield);      this.add(panelcontrol, borderlayout.north);     this.add(panel, borderlayout.south);     this.pack(); }  public void buttonaction(actionevent event) {     proceed.addactionlistener(this);      if (event.getsource() == proceed) {         joptionpane.showmessagedialog(this, "it works!");         system.out.println("hi");     } } }  // end class 

since class implements actionlistener, must define implementation actionperformed, have correctly done. when say

proceed.addactionlistener(this); 

you connecting button actionperformed defined in class, , not buttonaction() method seem wish connect to. same listener create connected to. should able fix using

public void actionperformed(actionevent event) { if (event.getsource() == create) {      string type = classoptions[playerclass.getselectedindex()];      if (type == "rogue") {         player1 = new rogue();     }     if (type == "wizard") {         player1 = new wizard();     }     if (type == "warrior") {         player1 = new warrior();     }     player1.name = name.gettext();      joptionpane.showmessagedialog(this, "you " + type             + ". name " + player1.name + ".");      init2();   } else if (event.getsource() == proceed) {     joptionpane.showmessagedialog(this, "it works!");     system.out.println("hi"); {  } 

this make buttonaction() unnecessary. add instead of implementing actionlistener, define listeners inner classes, , connect buttons them individually, making code cleaner. research :)


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -