Jlabels在Gridlayout中是看不见的

发布于 2025-02-06 16:08:50 字数 1496 浏览 4 评论 0原文

我做了一个jframe,其中添加了jpanel gridlayout。网格由jlabel组件,其中包含自定义号码。我没有例外。我的问题是在jframemainframe)上可见面板,但是您看不到添加到面板中的标签的数字。

有人能告诉我为什么我只看到面板,而不是标签?

首先,我创建GUI:

public Gui() {
    mainFrame = new JFrame();
    mainFrame.setResizable(false);
    mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    mainFrame.setUndecorated(true);
    mainFrame.setLayout(null);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

然后运行此方法:

public void startGame(Map map) {
    //Deletes every component that could be on the JFrame
    loescheAlles();

    //In the test are 100 paths in the String
    String[] paths = map.getPaths();
    //width has the value 1000
    int width = map.getBreite()*100;
    //height has the value 1000
    int height = map.getHoehe()*100;

    gamePanel = new JPanel();
    gamePanel.setBounds(0,0,width,height);
    gamePanel.setLayout(new GridLayout(width,height));

    JLabel[] label = new JLabel[paths.length];

    for(int i = 0; i<paths.length; i++) {
        //Text and border are not visible???
        label[i] = new JLabel("" + i);
        label[i].setBorder(new LineBorder(Color.BLACK));
        gamePanel.add(label[i]);
    }

    //Because I can see the Border on the JFrame I know, that the JPanel is visible.
    gamePanel.setBorder(new LineBorder(Color.BLACK));
    mainFrame.add(gamePanel);
}

I made a JFrame where I add a JPanel with a GridLayout. The grid consists of JLabel components with a custom number in it. I have no Exception in the program. My problem is that the panel is visible on the JFrame (mainFrame), but you can't see the numbers of the labels that are added to the panel.

Can anybody tell me why I only see the panel, but not the labels?

First I create the GUI:

public Gui() {
    mainFrame = new JFrame();
    mainFrame.setResizable(false);
    mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    mainFrame.setUndecorated(true);
    mainFrame.setLayout(null);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

Then I run this method:

public void startGame(Map map) {
    //Deletes every component that could be on the JFrame
    loescheAlles();

    //In the test are 100 paths in the String
    String[] paths = map.getPaths();
    //width has the value 1000
    int width = map.getBreite()*100;
    //height has the value 1000
    int height = map.getHoehe()*100;

    gamePanel = new JPanel();
    gamePanel.setBounds(0,0,width,height);
    gamePanel.setLayout(new GridLayout(width,height));

    JLabel[] label = new JLabel[paths.length];

    for(int i = 0; i<paths.length; i++) {
        //Text and border are not visible???
        label[i] = new JLabel("" + i);
        label[i].setBorder(new LineBorder(Color.BLACK));
        gamePanel.add(label[i]);
    }

    //Because I can see the Border on the JFrame I know, that the JPanel is visible.
    gamePanel.setBorder(new LineBorder(Color.BLACK));
    mainFrame.add(gamePanel);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文