Adding JLabel dynamically by pattern, but the latter does not work correctly

I am trying to create 40 dynamic JLabels in a template that works fine , but the last JLabel does not fit to the template. Can someone tell me what I did wrong?

Here is what I have done so 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];

        for (int i = 0; i <= c1 - 1; i++) {
            jl[i] = new JLabel();

            if (i <= 9) {
                x = 25 * count;
                jl[i].setBounds(x, 50, 20, 30);
                count++;
            }
            if (i >= 10 && i <= 19) {
                x = 25 * count2;
                jl[i].setBounds(x, 80, 20, 20);
                count2++;
            }
            if (i >= 20 && i <= 29) {
                x = 25 * count3;
                jl[i].setBounds(x, 110, 20, 20);
                count3++;
            }
            if (i >= 30 && i <= 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

+4
source share
2 answers

you use absolute layout / null layout, but you did not set the layout for null .default layout jframe is the border layout.

add this line

jf.setLayout(null);

and after adding all the components, call the method revalidate()and repaint()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); // this is important 

        jf.setSize(700, 400);

        int c1 = 40;
        int count = 0, count2 = 0, count3 = 0, count4 = 0, x;
        JLabel[] jl = new JLabel[c1];

        for (int i = 0; i <= c1 - 1; i++) {
            jl[i] = new JLabel();

            if (i <= 9) {
                x = 25 * count;
                jl[i].setBounds(x, 50, 20, 20);
                count++;
            }
            if (i >= 10 && i <= 19) {
                x = 25 * count2;
                jl[i].setBounds(x, 80, 20, 20);
                count2++;
            }
            if (i >= 20 && i <= 29) {
                x = 25 * count3;
                jl[i].setBounds(x, 110, 20, 20);
                count3++;
            }
            if (i >= 30 && i <= 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) . use layout GRID- . /, https://www3.ntu.edu.sg/home/ehchua/programming/java/J8a_GameIntro-BouncingBalls.html. paintComponent() jpanel, , . , 100 * 100, jlables, .

2) jpanel jframe. setContentPane()

+5

, GridLayout:

import javax.swing.*;
import java.awt.*;

public class Booking2 {

    public static void main(String[] args) {
        JFrame jf = new JFrame();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
        jf.setLayout(new GridLayout(4,10));
        jf.setSize(700, 400);
        jf.setLocationRelativeTo(null);
        int c1=40;
        JLabel[] jl = new JLabel[c1];

        for(int i=c1-1; i>=0; i--){
            jl[i]=new JLabel();
            jl[i].setText("O");
            jf.add(jl[i]);
        }
    }
}

. setBound(), JFrame ( JLabels) null:

jf.setLayout(null);

. , JFrames BorderLoyout setBounds().

, . LayoutMenagers!

+4

All Articles