更新 GridLayout 中的网格

发布于 2024-10-06 10:51:36 字数 1076 浏览 4 评论 0原文

我有一个通过 JPanel 中的 GridLayout 布局的对象数组。我需要能够在数组的索引中重新创建对象,并更新 GridLayout 来反映这一点。到目前为止,我无法找到“刷新”或重绘 GridLayout 的方法。是否可以在不创建整个 GridLayout 或 JPanel 的情况下刷新 GridLayout?假设我无权访问 JFrame。

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

public class Test
{
    public static void main(String args[])
    {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        panel.setLayout(new GridLayout(5,5));

        JLabel[][] labels = new JLabel[5][5];
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                labels[j][i] = new JLabel("("+j+", "+i+")");
                panel.add(labels[j][i]);
            }
        }

        labels[0][0] = new JLabel("Hello World");

        //Without doing it this way (cause my objects can't do this)
        //labels[0][0].setText("Hello World!");

        frame.add(panel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
    }
}

I have an array of objects laid out through a GridLayout in a JPanel. I need to be able to recreate the object in an index in the array and have the GridLayout update to reflect this. As of yet, I cannot find anyway to "refresh" or redraw the GridLayout. Is it possible to refresh a GridLayout without creating the entire GridLayout or JPanel? Assume I do not have access to JFrame.

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

public class Test
{
    public static void main(String args[])
    {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        panel.setLayout(new GridLayout(5,5));

        JLabel[][] labels = new JLabel[5][5];
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                labels[j][i] = new JLabel("("+j+", "+i+")");
                panel.add(labels[j][i]);
            }
        }

        labels[0][0] = new JLabel("Hello World");

        //Without doing it this way (cause my objects can't do this)
        //labels[0][0].setText("Hello World!");

        frame.add(panel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
    }
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩虹直至黑白 2024-10-13 10:51:36

我不明白为什么你不能只更新标签上的文字。

为什么需要“重新创建对象”?这毫无意义。但如果你确实需要这样做,那么代码将类似于:

panel.remove(0);
panel.add(theNewLabel, 0);
panel.revalidate();
panel.repaint();

I don't understand why you can't just update the text on the label.

Why do you need to "recreate the object"? It makes no sense. But if you really need to do this then the code would be something like:

panel.remove(0);
panel.add(theNewLabel, 0);
panel.revalidate();
panel.repaint();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文