Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新jpanel中的按钮数组_Java_Arrays_Swing_User Interface_Updatepanel - Fatal编程技术网

Java 更新jpanel中的按钮数组

Java 更新jpanel中的按钮数组,java,arrays,swing,user-interface,updatepanel,Java,Arrays,Swing,User Interface,Updatepanel,我向constructor panel发送了一个int数组,这是我第一次创建gui,但下一个数组没有更新panel/jframe class panel extends JPanel{ public panel(int matriz[][]) { this.setPreferredSize(new Dimension(300, 300)); this.setLayout(new GridLayout(5, 5)); this.reval

我向constructor panel发送了一个int数组,这是我第一次创建gui,但下一个数组没有更新panel/jframe

class panel extends JPanel{

    public panel(int matriz[][]) {

        this.setPreferredSize(new Dimension(300, 300));
        this.setLayout(new GridLayout(5, 5));
        this.revalidate();

        for (int i = 0; i < 5; i++) {

            for (int j = 0; j < 5; j++) {
                if(matriz[i][j]==1)
                {

                    JButton boton = new JButton("");   
                    boton.setBackground(Color.yellow);
                    this.add(boton);
                    boton.revalidate();
                    boton.repaint();

                }
                else
                {
                    JButton boton = new JButton("");   
                    this.add(boton);
                    boton.revalidate();
                    boton.repaint();
                }

                if(j==4) //imprimo pa ver si realmente me esta enviando datos desde creandoVida
                    System.out.println("n"+matriz[i][j]);
                else
                    System.out.print(matriz[i][j]);

            }
        }

        System.out.println("---");
        this.doLayout();
        this.revalidate();
        this.repaint();
    }

    public void paintComponent(Graphics g){
    super.paintComponent(g);
    this.revalidate();
}
类面板扩展了JPanel{
公共面板(int matriz[]{
此.setPreferredSize(新维度(300300));
这个.setLayout(新的GridLayout(5,5));
这个。重新验证();
对于(int i=0;i<5;i++){
对于(int j=0;j<5;j++){
if(matriz[i][j]==1)
{
JButton boton=新JButton(“”);
波顿。挫折背景(颜色。黄色);
这个。添加(boton);
boton.revalidate();
boton.repaint();
}
其他的
{
JButton boton=新JButton(“”);
这个。添加(boton);
boton.revalidate();
boton.repaint();
}
如果(j==4)//改善我的生活环境
系统输出println(“n”+矩阵[i][j]);
其他的
系统输出打印(matriz[i][j]);
}
}
System.out.println(“--”);
这个是doLayout();
这个。重新验证();
这个。重新绘制();
}
公共组件(图形g){
超级组件(g);
这个。重新验证();
}

无法根据发布的代码判断您正在做什么。但一些一般性意见:

  • 在创建所有组件并将其添加到面板之后,只需执行一次revalidate()和repaint(),因此这两条语句应该在for循环之外

  • 不要从paintComponent()方法中调用revalidate()。这可能会导致无限循环,因为很多时候revalidate()会再次调用repaint()。我认为您根本没有理由重写paintComponent()方法

  • 使用Java命名约定。类名以大写字符开头。但不要将类称为“Panel”,因为已经存在同名的AWT类

  • 如果你需要更多的帮助,那么发布一个适当的说明问题的帖子

    编辑:

    实际上,我看到您正在创建一个全新的面板,因此该类中不需要revalidate()、repaint()逻辑


    创建“面板”后您必须将面板添加到框架中,然后重新验证框架。否则,您所做的只是创建一个位于内存中的面板,而该面板不做任何操作。因此,创建此面板的代码负责将面板添加到框架中。由于您没有发布该代码,我无法判断您正在执行的操作。

    thx,我更新了代码,但问题仍然存在“matriz”有新的数据,但jpanel没有变化absolutely@user3547063,请参见编辑。如果没有合适的
    SSCCE
    ,我将无法提供更多帮助。