java - setContentPane covering everything up and JButton not working properly -


i making game in player attempts navigate maze, have run 2 bugs have absolutely stumped me. first problem using setcontentpane(). whenever set intended background, covers else on screen. have tried relocating every place thought be, still covered up. second question jbutton. whenever click it, changes playing speed intended, can't move afterwards. works when use method in keylistener, , have same code inside of them. have included code thought necessary, if need more, let me know. full source project can found here.

package game;  import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.arraylist; import java.util.random;  public class game extends jpanel implements actionlistener, keylistener {      private boolean uppressed    = false;     private boolean downpressed  = false;     private boolean rightpressed = false;     private boolean leftpressed  = false;      private int tilesize = 200;     private int playersize = tilesize / 4;     private int playerspeed = playersize / 10;     private int mazex = 51;     private int mazey = 51;      private int[][] maze = new int[mazex][mazey];     private int[][] initx = new int[mazex][mazey];     private int[][] inity = new int[mazex][mazey];     private boolean[][] visited = new boolean[mazex][mazey];      private int currentcellx = 0;     private int currentcelly = 0;     private int deltax = 0;     private int deltay = 0;      jcomponent background = new jlabel(new imageicon(main.class.getresource("/assets/ui/mainmenu.png")));     jbutton adventure = new jbutton();     jbutton freeplay = new jbutton();     jbutton exit = new jbutton();     jbutton pause = new jbutton();     jbutton resume = new jbutton();      private string screen = "menu";     public main main;       public game(main main) {         this.main = main;          setfocusable(true);         addkeylistener(this);         addbuttons();         setupinitialcoordinates();         timer timer = new timer(1000 / 120,  this);         timer.start();     }       private void tick() {         switch (screen) {             case "playing" :                 background.setvisible(false);                 if (uppressed) {                     deltay += playerspeed;                 } else if (downpressed) {                     deltay -= playerspeed;                 }                 if (rightpressed) {                     deltax -= playerspeed;                 } else if (leftpressed) {                     deltax += playerspeed;                 }                 break;             case "paused":                 background.setvisible(false);                 adventure.setvisible(false);                 freeplay.setvisible(false);                 pause.setvisible(false);                 exit.setvisible(true);                 break;             case "menu":                 main.setcontentpane(new jlabel(new imageicon(main.class.getresource("/assets/ui/mainmenu.png"))));                 adventure.setvisible(true);                 freeplay.setvisible(true);                 pause.setvisible(false);                 exit.setvisible(true);                 break;         }         repaint();     }      private void addbuttons() {         setlayout(null);         add(freeplay);         freeplay.setbounds(10, 100, 180, 80);         freeplay.setsize(180, 80);         freeplay.setaction(new abstractaction() {             @override             public void actionperformed(actionevent ae) {                 downpressed = false;                 rightpressed = false;                 leftpressed = false;                 generatemaze();                 screen = "playing";             }         });         freeplay.seticon(new imageicon(main.class.getresource("/assets/ui/buttons/freeplayunselected.png")));         freeplay.setrollovericon(new imageicon(main.class.getresource("/assets/ui/buttons/freeplayselected.png")));     }      private void setupinitialcoordinates() {     }       private void generatemaze() {     }       private boolean checkforunvisitedcells() {     }       private boolean checkforunvisitedadjacentcells() {     }       @override     public void actionperformed(actionevent e) {         tick();     }       @override     public void keytyped(keyevent e) {}       @override     public void keypressed(keyevent e) {     }       @override     public void keyreleased(keyevent e) {     }       @override     public void paintcomponent(graphics g) {     } } 

setcontentpane that, replaces ever been used content new component.

rootpane

take closer @ how use root panes more details.

i'd suggest better solution use cardlayout, in case, better solution change icon property of current content pane...

background.seticon(new imageicon(main.class.getresource("/assets/ui/mainmenu.png"))); 

and make background frame's content pane...

jlabel background = new jlabel(new imageicon(main.class.getresource("/assets/ui/mainmenu.png")));  //.... public game(main main) {     this.main = main;     setcontentpane(background);     //... 

then never change content pane...

you use cardlayout change appears on content pane

my second question jbutton. whenever click it, changes playing speed intended, can't move afterwards. works when use method in keylistener

welcome wonderful world of why shouldn't use keylistener, button has stolen key board focus , keylistener no longer respond key board input because relies on component registered focusable , focused. use key bindings instead. how use key bindings


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -