java - Where have I messed up regarding creating a Game Menu? -


i trying create basic game menu game right now. testing out menu now, , of options wrote test out whether menu works or not. have menu class , optionpanel class well.

here menu class:

import java.awt.event.*; import javax.swing.*; import java.awt.*;  public class main extends jframe  {  jpanel cardpanel;  public main(string title) {     super(title);     setbounds(100, 100, 800, 600);     setdefaultcloseoperation(jframe.exit_on_close);      cardpanel = new jpanel();     cardlayout cl = new cardlayout();     cardpanel.setlayout(cl);       optionpanel panel1 = new optionpanel(this);     board panel2 = new board();     rules panel3 = new rules();        cardpanel.add(panel1,"1");     cardpanel.add(panel2,"2");     cardpanel.add(panel3,"3");      add(cardpanel);      setvisible(true); }  public static void main(string[] args) {     main w = new main("ap animation demo"); }  public void changepanel() {     ((cardlayout)cardpanel.getlayout()).next(cardpanel);     requestfocus(); }  } 

and here option panel class:

  import java.awt.*;   import javax.swing.*;   import java.awt.event.*;    public class optionpanel extends jpanel implements actionlistener {  main w;  public optionpanel(main w) {     this.w = w;     jbutton button = new jbutton("press me!");     button.addactionlistener(this);     add(button);     jbutton button2 = new jbutton("game rules");     button2.addactionlistener(this);     add(button2); }  public void paintcomponent(graphics g)   {     super.paintcomponent(g);     setbackground(color.black);     }// call jpanel's paintcomponent method paint background  public void actionperformed(actionevent e) {      w.changepanel(); }  } 

how make when menu pops up, can click on 1 button leads game, , when clicking on button, linked screen. think has actionperformed thing, tried adding if (e.getsource == button) , stuff that, not find button variable. advice/feedback?

if want actionperformed() method able access button variable, variable has have instance scope (or static, less preferable always). referring in method have written won't work because button variable local constructor.

the suggestion in comments make separate actionlistener each button; need use if (e.getsource() == button) if 1 actionperformed() method getting called multiple buttons. difference between these little answer; can tutorial on action listeners in java tutorials @ oracle.

the way have started above suggests going use optionpanel single action listener buttons, , therefore needs test button invoked it. if instead have separate action listener each button, knows button invoked , doesn't need test.

try looking "anonymous inner classes" relate action listeners in java.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -