繁体   English   中英

分配给新多维数组的数据不断更新

[英]Data assigned to new multidimensional array keeps updating

抱歉,我不知道这个问题的标题,但我在将数据分配给新的多维数组时遇到问题,做一些事情然后检索旧数据。

在我的情况下,我试图循环遍历 JTextFields,将所有当前数据专门分配给新的多维数组的颜色。 然后我想进行搜索并更改找到的文本字段的背景颜色。

现在我有一个重置按钮,我希望将现在位于新多维数组中的旧颜色分配回字段。 我遇到的问题是新的多维数组在搜索后使用新颜色进行了更新。 如果有人能指出我正确的方向,我真的很感激。

这是我的代码:

public JTextField[][] fields = new JTextField[totalX][totalY];
    public JTextField[][] newFields = new JTextField[totalX][totalY];
    
    if (e.getSource() == btnFind  || e.getSource() == txtSearch)
    {
        // Make a copy of fields before selecting everything
        for(int t = 0; t < totalX; t++){
            for(int r = 0; r < totalY; r++){
                newFields[t][r] = fields[t][r];
            }
        }
        
        findStudentRecord();
        // when looping though newFields[x][y] here it is already updated to the current colour
        
    }
    if (e.getSource() == btnReset)
    {
        for(int x = 0; x < totalX; x++){
            for(int y = 0; y < totalY; y++){
                fields[x][y].setText(newFields[x][y].getText());
                fields[x][y].setBackground(newFields[x][y].getBackground()); 
                // have tried this one but doesn't work
                if(newFields[x][y].getBackground() == Color.green){
                    fields[x][y].setBackground(Color.green);
                    System.out.print(fields[x][y].getText() + "\n");
                }
            }
        }
    } 

这是 findStudentRecord()

public void findStudentRecord()
{
    boolean found = false;
    String strFind = txtSearch.getText();
    for(int x = 0; x < totalX; x++){
        for(int y = 0; y < totalY; y++){
            if(fields[x][y].getText().equalsIgnoreCase(strFind))
            {
                found = true;
            }
        }
    }
    if (found)
    {
        for (int x = 0; x < totalX; x++)
        {
            for(int y = 0; y < totalY; y++){
                if(fields[x][y].getText().equalsIgnoreCase(strFind))
                {
                    fields[x][y].setBackground(new Color(255,217,200));
                }
            }
        }
        txtSearch.setText(txtSearch.getText() + " ...Found.");
    }
    else
    {
        txtSearch.setText(txtSearch.getText() + " ...Not Found.");
    }
}

您必须创建一个新的JTextField并添加相关数据:

    newFields[t][r] = new javax.swing.JTextField();
    newFields[t][r].setText(fields[t][r].getText());
    newFields[t][r].setColumns(fields[t][r].getColumns());
    // any other property you want to transfer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM