swing - java -- how to make new text block appear every time a button is clicked -


i'm new @ java , trying make simple interactive fiction game gui using swing. right i'm trying break large block of text sections fit onto screen easily. want 1 block of text show in jtextarea "continue" jbutton on bottom of screen, every time user clicks jbutton current text disappear , replaced new text. here's relevant code:

    public class forwardscreenhandler implements actionlistener{     public void actionperformed(actionevent event){         maintextarea.settext("nice change!");         maintextarea.settext("do again!");         maintextarea.settext("please third!");              }         }  public void chooseyes(){     yesnobuttonpanel.setvisible(false);     continuebuttonpanel.setvisible(true);     continuebuttonpanel.setbackground(color.red);     continuebutton.removeactionlistener(conthandler);     continuebutton.addactionlistener(usercont);     position = "yes";     maintextarea.settext("blah blah blah");  } 

clearly code result in "blah blah blah" being shown, when click continue button final settext "please third!". understand it's because haven't written code telling java settext every time continue button clicked, can't figure out how this. said i'm beginner, explanation helpful can understand do.

you need use variable , if statements this.

declare variable @ class level store how many times button has been pressed:

private int buttonpresscount = 0; 

in action listener continue button, first increment variable, set text based on value of it.

buttonpresscount++; switch (buttonpresscount) {     case 1:         maintextarea.settext("nice change!");         break;     case 2:         maintextarea.settext("do again!");         break;      case 3:         maintextarea.settext("please third!"); } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -