swing - My Menu in Java can't be Called Twice? -


i'm new java , programming in general, , writing program has menu in it. (it jframe in java) when hit jbutton, shows applet on screen. when applet done, goes screen can choose run applet again or go main menu when hit button. problem when hit button go menu, doesn't. make neither button clickable. method use draw menu:

public static void drawmenu() {     f.add(boption1);     f.add(boption2); } 

the 2 jbuttons declared , such in constructor, , work fine first time run menu. then, when hit 1 of buttons, removes both buttons screen f.remove(...). know why won't work when call method second time?

edit: sorry, meant canvas, not applet.

edit edit: found solution problem, anyway.

here code main class, frame:

import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class frame extends jframe {     static oneplayer oneplayer;    static twoplayer twoplayer;     static frame f;    static jbutton boneplayer = new jbutton("single player");    static jbutton btwoplayer = new jbutton("multiplayer");    static jbutton binstructions = new jbutton("instructions");    static jbutton tomenu;    static jbutton replay;     public frame(string name) {       super(name);       this.settitle(name);       this.setvisible(true);       this.setsize(640, 673);       this.setdefaultcloseoperation(jframe.exit_on_close);       this.setresizable(false);       this.setlocationrelativeto(null);       this.setlayout(null);       this.setbackground(color.gray);        boneplayer.setbounds(120, 150, 400, 100);       btwoplayer.setbounds(120, 250, 400, 100);       binstructions.setbounds(120, 350, 400, 100);        boneplayer.setfont(new font("comic sans ms", font.italic, 20));       btwoplayer.setfont(new font("comic sans ms", font.italic, 20));       binstructions.setfont(new font("comic sans ms", font.italic, 20));        boneplayer.addactionlistener(new actionlistener() {          public void actionperformed(actionevent e) {             container onepane = f.getcontentpane();             onepane.setlayout(new gridlayout(1, 1));             oneplayer = new oneplayer();             onepane.add(oneplayer);             oneplayer.init();              f.remove(boneplayer);             f.remove(btwoplayer);             f.remove(binstructions);          }       });       btwoplayer.addactionlistener(new actionlistener() {          public void actionperformed(actionevent e) {             container twopane = f.getcontentpane();             twopane.setlayout(new gridlayout(1, 1));             twoplayer = new twoplayer();             twopane.add(twoplayer);             twoplayer.init();              f.remove(boneplayer);             f.remove(btwoplayer);             f.remove(binstructions);          }       });       binstructions.addactionlistener(new actionlistener() {          public void actionperformed(actionevent e) {           }       });    }     public static void main(string[] args) {       f = new frame("snake");       drawmenu();    }     public static void oneplayerdone(int score) {     }     public static void twoplayerdone(int winner, int p1score, int p2score) {       f.remove(twoplayer);        replay = new jbutton("play again");       tomenu = new jbutton("return menu");        replay.setbounds(120, 100, 400, 100);       tomenu.setbounds(120, 500, 400, 100);        replay.setfont(new font("comic sans ms", font.italic, 20));       tomenu.setfont(new font("comic snas ms", font.italic, 20));        f.add(replay);       f.add(tomenu);        replay.addactionlistener(new actionlistener() {          public void actionperformed(actionevent e) {             container twopane = f.getcontentpane();             twopane.setlayout(new gridlayout(1, 1));             twoplayer = new twoplayer();             twopane.add(twoplayer);             twoplayer.init();          }       });       tomenu.addactionlistener(new actionlistener() {          public void actionperformed(actionevent e) {             drawmenu();          }       });    }     public static void drawmenu() {       f.add(boneplayer);       f.add(btwoplayer);       f.add(binstructions);    } } 

suggestions:

  • first off, rename class other frame. name of closely related core java class, awt equivalent of jframe, , giving same name can confuse many. perhaps call snakeframe.
  • all of static variables should instead instance variables.
  • you shouldn't have snakeframe variable (your variable f). instead should use current snakeframe instance, this if will.
  • don't mix awt swing components unnecessarily , without need. instance should use no canvas-derived objects rather jpanel-derived objects.
  • your code should follow java naming conventions others (us instance) can understand it. variable , method names should begin lower case.
  • you want read on , use layout managers place gui components on gui rather use null layout , setbounds(...).
  • most important, swap views you're trying do, read on , use cardlayout built purpose.

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -