java - How to display a JLabel without a layout manager -


i making game final project , need have label big "x" appear in middle of jpanel. have jpanels appearing label not appear because dont have layout manager, if use layout manager entire project gets changed. project have can see i'm trying do.

create game helps new mouse users improve hand-eye coordination. within jframe, display array of 48 jpanels in gridlayout using 8 rows , 6 columns. randomly display x on 1 of panels. when user clicks correct panel(the 1 displaying x), remove x , display on different panel. after user has “hit” correct panel 10 times, display congratulation message includes user’s percentage(hits divided clicks). save file jcatchthemouse.java.

here's code

package catchthemouse;  import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class catchthemouse extends jframe implements actionlistener, mouselistener{     final int rows = 8;     final int cols = 6;     final int gap = 2;     final int max_panels = rows * cols;     int clicks;     int hits;     int percentage = 0;     int width;     int height;     int panelx;     int panely;     int whichpanel = (int)(math.random() * 48 + 1);     int numberofpanels = 1;      jlabel grats = new jlabel("");     jlabel spot = new jlabel("x");     jpanel[] panel = new jpanel[max_panels];     jpanel pane = new jpanel(new gridlayout(rows, cols, gap, gap));     font xfont = new font("ariel", font.bold, 30);     font font = new font("ariel", font.plain, 12);      public catchthemouse() {         super("catch mouse");         setdefaultcloseoperation(jframe.exit_on_close);         setsize(300,300);         add(spot);         spot.setfont(xfont);         add(grats);         grats.setfont(font);         add(pane);         for(int x = 0; x < max_panels; ++x) {             panel[x] = new jpanel();             pane.add(panel[x]);             panel[x].setbackground(color.red);         }         pane.setbackground(color.black);         panelx = panel[whichpanel].getx();         panely = panel[whichpanel].gety();         width = panel[whichpanel].getwidth() / 2;         height = panel[whichpanel].getheight() / 2;         spot.setbounds(panelx, panely, width, height);     }      public void mouseclicked(mouseevent e) {         clicks = e.getclickcount();     }      public void mouseentered(mouseevent e) {         }      public void mouseexited(mouseevent e) {     }      public void mousepressed(mouseevent e) {         }      public void mousereleased(mouseevent e) {     }      public void actionperformed(actionevent e) {     }      public static void main(string[] args) {         catchthemouse frame = new catchthemouse();         frame.setvisible(true);     } } 

your spot being drawn behind other panels. if add

setcomponentzorder(spot, 0); 

this cause drawn on top of else.

a better way approach draw x inside of panels have instead of on top of them.

edit:

you can add x specific panel

panel[0].add(spot); 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -