在模式中动态添加 JLabel 但最后一个无法正常工作

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

我正在尝试在 模式 中创建 40 个动态 JLabels,效果很好,但最后一个 JLabel 没有按照模式放置。谁能告诉我我做错了什么?

这是我到目前为止所做的:

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]);
        }
    }
}

您正在使用绝对定位/空布局,但您尚未将布局设置为空。jframe 的默认布局是边框布局。

添加这一行

jf.setLayout(null);

添加所有组件后调用 jframe 的 revalidate()repaint() 方法。

例子

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);
    }

}

输出

备注

1) 你应该避免空布局。使用布局 GRID 布局似乎适合你的情况。而且如果这是一种游戏/动画,你应该看看这些示例 https://www3.ntu.edu.sg/home/ehchua/programming/java/J8a_GameIntro-BouncingBalls.html。你可以使用 paintComponent() 方法在高效的 jpanel 中绘制这个网格,你可以绘制任何类型的图案。想想如果你制作一个大网格,例如使用 jlables 的 100*100 它不好

2) 最好将组件添加到 jpanel 而不是直接添加到 jframe 。你可以使用 setContentPane() 方法

使用layout会好很多,比如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 的容器)布局设置为空:

jf.setLayout(null);

这将允许在容器内进行绝对定位。似乎默认 JFrames BorderLoyout 扭曲了您的 setBounds() 设置。

但是通常不推荐使用空布局,这被认为是一种糟糕的编程习惯。最好使用 LayoutMenagers!