Java swing JLabel 没有出现在 BorderLayout 中

Java swing JLabel doesn't appear in BorderLayout

我在随机位置的 JPanel 上制作了 10 颗星。

但问题是当我设置布局时,开头没有显示。

public class Main {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container c = frame.getContentPane();
    c.setLayout(new BorderLayout());

    JPanel northPanel = new JPanel(new FlowLayout());
    northPanel.add(new JButton("Open"));
    c.add(northPanel, BorderLayout.NORTH);

    JPanel southPanel = new JPanel(new FlowLayout());
    southPanel.add(new JButton("Integer Input"));
    c.add(southPanel, BorderLayout.SOUTH);

    JPanel centerPanel = new JPanel(null);
    centerPanel.setBackground(Color.pink);
    for(int i = 0; i < 10; i++) {
        int x = (int) (Math.random() * 300);
        int y = (int) (Math.random() * 300);
        JLabel label = new JLabel("*");
        label.setLocation(x, y);
        label.setForeground(Color.green);
        label.setOpaque(true);
        centerPanel.add(label);
    }
    c.add(centerPanel, BorderLayout.CENTER);

    frame.setVisible(true);
}

粉色部分应该是10颗星

Basically, every container like JPanel, JFrame used some layout to place the component in some specific order. as per your code, you are passing null as argument to centralPanel. so basically central panel has no layout to place component.
Note : Flow Layout is a default layout.

in this case lets try with below steps.
1. please use setBound(x, y, height, width), on the place of setLocation(x, y).
    for (int i = 0; i < 10; i++) {
         int x = (int) (Math.random() * 300);
         int y = (int) (Math.random() * 300);
         JLabel label = new JLabel("*");
         **label.setBounds(x,y,10,10);**
         label.setForeground(Color.green);
         label.setOpaque(true);
         centerPanel.add(label);
    }


  [1]: https://i.stack.imgur.com/RUlUI.png

如果您想在随机位置放置 10 个字符 (*),我建议您通过自定义绘画来完成,如下面的 mre 所示:

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        //frame.setSize(300, 300); don't set size let the layout managers do it
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container c = frame.getContentPane();
        //c.setLayout(new BorderLayout()); not needed, it is the default

        JPanel northPanel = new JPanel(new FlowLayout()); //FlowLayout is the default
        northPanel.add(new JButton("Open"));
        c.add(northPanel, BorderLayout.NORTH);

        JPanel southPanel = new JPanel(new FlowLayout());
        southPanel.add(new JButton("Integer Input"));
        c.add(southPanel, BorderLayout.SOUTH);

        JPanel centerPanel = new PinkPlanet();
        c.add(centerPanel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}

class PinkPlanet extends JPanel{

    private static final int W = 300, H = 300;
    private static final String STAR ="*";
    private final Dimension pSize = new Dimension(W, H);
    private final List<Point> locations = new ArrayList<>();

    public PinkPlanet() {
        setBackground(Color.pink);
        for(int i = 0; i < 10; i++) {
            int x = (int) (Math.random() * W);
            int y = (int) (Math.random() * H);
            locations.add(new Point(x, y));
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return pSize;
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        for(Point location : locations){
            g.drawString(STAR, location.x, location.y);
        }
    }
}

我找到了自己的路。

设置后 label.setSize(10,10);

我可以在面板中找到 10 颗星。