Java 更新jpanel中的按钮数组
我向constructor panel发送了一个int数组,这是我第一次创建gui,但下一个数组没有更新panel/jframeJava 更新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
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);
这个。重新验证();
}
无法根据发布的代码判断您正在做什么。但一些一般性意见:
创建“面板”后您必须将面板添加到框架中,然后重新验证框架。否则,您所做的只是创建一个位于内存中的面板,而该面板不做任何操作。因此,创建此面板的代码负责将面板添加到框架中。由于您没有发布该代码,我无法判断您正在执行的操作。thx,我更新了代码,但问题仍然存在“matriz”有新的数据,但jpanel没有变化absolutely@user3547063,请参见编辑。如果没有合适的
SSCCE
,我将无法提供更多帮助。