java - Adding JLabel dynamically in a pattern but last one is not working correctly -


i'm trying creating 40 dynamical jlabels in pattern, working quite last jlabel not placing according pattern. can tell me did wrong ?

here have done far:

public class booking2 {      public static void main(string[] args) {         jframe jf = new jframe();         jf.setvisible(true);         jf.setdefaultcloseoperation(jf.exit_on_close);          jf.setsize(700, 400);         jf.setlocationrelativeto(null);         int c1 = 40;         int count = 0, count2 = 0, count3 = 0, count4 = 0, x;         jlabel[] jl = new jlabel[c1];          (int = 0; <= c1 - 1; i++) {             jl[i] = new jlabel();              if (i <= 9) {                 x = 25 * count;                 jl[i].setbounds(x, 50, 20, 30);                 count++;             }             if (i >= 10 && <= 19) {                 x = 25 * count2;                 jl[i].setbounds(x, 80, 20, 20);                 count2++;             }             if (i >= 20 && <= 29) {                 x = 25 * count3;                 jl[i].setbounds(x, 110, 20, 20);                 count3++;             }             if (i >= 30 && <= 39) {                 x = 25 * count4;                 jl[i].setbounds(x, 130, 20, 20);                 count4++;             }             // jl[i].seticon(new             // imageicon(booking2.class.getresource("booked.png")));             jl[i].settext("o");             jf.add(jl[i]);         }     } } 

enter image description here

you using absolute positioning / null layout haven't set layout null .default layout of jframe border layout.

add line

jf.setlayout(null); 

and after add components call revalidate() , repaint() method of jframe.

example

public class booking2 {      public static void main(string[] args) {         jframe jf = new jframe();         jf.setvisible(true);         jf.setdefaultcloseoperation(jf.exit_on_close);          jf.setlayout(null); // important           jf.setsize(700, 400);          int c1 = 40;         int count = 0, count2 = 0, count3 = 0, count4 = 0, x;         jlabel[] jl = new jlabel[c1];          (int = 0; <= c1 - 1; i++) {             jl[i] = new jlabel();              if (i <= 9) {                 x = 25 * count;                 jl[i].setbounds(x, 50, 20, 20);                 count++;             }             if (i >= 10 && <= 19) {                 x = 25 * count2;                 jl[i].setbounds(x, 80, 20, 20);                 count2++;             }             if (i >= 20 && <= 29) {                 x = 25 * count3;                 jl[i].setbounds(x, 110, 20, 20);                 count3++;             }             if (i >= 30 && <= 39) {                 x = 25 * count4;                 jl[i].setbounds(x, 130, 20, 20);                 count4++;             }             //jl[i].seticon(new imageicon(booking2.class.getresource("booked.png")));             jl[i].settext("o");             jf.add(jl[i]);         }          jf.revalidate();         jf.setvisible(true);     }  } 

output

enter image description here

note

1) should avoid null layout .use layout grid layout seems case .and if kind of game /animation should @ these example https://www3.ntu.edu.sg/home/ehchua/programming/java/j8a_gameintro-bouncingballs.html. can use paintcomponent() method draw grid in jpanel efficient , can draw kind of pattern .think if make big grid,for instance 100*100 using jlables it's not

2) it's better add components jpanel instead of add directly jframe .you can use setcontentpane() method that


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -