Java 在一个帧中将对象添加到Arraylist,但无法在第二个已打开的帧中访问该对象

Java 在一个帧中将对象添加到Arraylist,但无法在第二个已打开的帧中访问该对象,java,swing,arraylist,Java,Swing,Arraylist,有点奇怪,但我有一个框架,其中包含一个搜索栏,这样当用户键入某个药物名称时,它将搜索一个包含所有药物的arraylist(allDrugs),并在文本区域显示其信息 这很好,但是如果我接着创建另一个药物,打开第二个框架,填写所有信息,将该信息保存为新的药物对象,然后将其添加到allDrugs arrayList中,然后返回到仍然打开的原始框架并尝试搜索新药物名称,它将无法找到创建的新药物 这几乎就像我必须刷新框架,但我已经尝试过,移除所有,验证(),重新绘制(),但它仍然找不到新药。 下面是搜索

有点奇怪,但我有一个框架,其中包含一个搜索栏,这样当用户键入某个药物名称时,它将搜索一个包含所有药物的arraylist(allDrugs),并在文本区域显示其信息

这很好,但是如果我接着创建另一个药物,打开第二个框架,填写所有信息,将该信息保存为新的药物对象,然后将其添加到allDrugs arrayList中,然后返回到仍然打开的原始框架并尝试搜索新药物名称,它将无法找到创建的新药物

这几乎就像我必须刷新框架,但我已经尝试过,移除所有,验证(),重新绘制(),但它仍然找不到新药。 下面是搜索栏,有点像主窗口或框架1

searchButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == searchButton)
            {
                try
                {
                    for(Drug d: allDrugs)
                    {
                        //finished here, makesure that the string that comes in from the name of the drug from the field is the same as the one already i nthe arraylist
                        //Done by capitalising the first letter no matter what.
                        if(d.getName().equalsIgnoreCase(searchField.getText()))
                        {
                                MainMenu.textArea.setText(null);
                                MainMenu.textArea.append(d.allDetailsToString());
                                MainMenu.textArea.setEditable(false);
                                break;
                        }

                        JOptionPane.showMessageDialog(null, "Drug not found");
                        break;

                    }
                }
                catch(Exception ex)
                {
                    JOptionPane.showMessageDialog(null, "Error when searching for drug");
                }
            }
这是frame2中存在的createDrug方法:

 private void createDrug(ArrayList<Drug>allDrugs)
        {  
 String name = nameField.getText().substring(0,1).toUpperCase() + nameField.getText().substring(1);
                Drug drug = new Drug(name, classField.getText(), aimArea.getText(), moaArea.getText(), altArea.getText(), dosageArea.getText(), observationArea.getText(), durationArea.getText(), elimArea.getText(), unwantedArea.getText(), interactionArea.getText(), ideasArea.getText());
                allDrugs.add(drug);
private void createDrug(ArrayListallDrugs)
{  
String name=nameField.getText().substring(0,1).toUpperCase()+nameField.getText().substring(1);
药物=新药(名称,classField.getText(),aimArea.getText(),moaArea.getText(),altArea.getText(),dosageArea.getText(),observationArea.getText(),durationArea.getText(),elimArea.getText(),unwantedArea.getText(),interactionArea.getText(),ideasrea.getText());
所有药物。添加(药物);
如果这有点难以理解,我道歉。 那么,我如何才能在第2帧中创建一种新药,然后在第1帧中仍然打开的搜索栏将拾取新添加的药物呢?
提前感谢!

您是否尝试搜索药物列表中除第一项以外的任何项目?据我所知,一旦发现第一项不是目标,您的
foreach
循环将中断

for(Drug d: allDrugs)
{
    //finished here, makesure that the string that comes in from the name of the drug from the field is the same as the one already i nthe arraylist
    //Done by capitalising the first letter no matter what.
    if(d.getName().equalsIgnoreCase(searchField.getText()))
    {
         MainMenu.textArea.setText(null);
         MainMenu.textArea.append(d.allDetailsToString());
         MainMenu.textArea.setEditable(false);
         break;
    }
    //this will run for the first loop round and it will break the loop if the first item in list is not the search target
    JOptionPane.showMessageDialog(null, "Drug not found");
    break;

}
您的
JOptionPane.showMessageDialog(null,“未找到药物”);
应该在循环之外,并且应该抛出
中断;

boolean found = false;
for(Drug d: allDrugs)
{
    //finished here, makesure that the string that comes in from the name of the drug from the field is the same as the one already i nthe arraylist
    //Done by capitalising the first letter no matter what.
    if(d.getName().equalsIgnoreCase(searchField.getText()))
    {
         MainMenu.textArea.setText(null);
         MainMenu.textArea.append(d.allDetailsToString());
         MainMenu.textArea.setEditable(false);
         found = true;
         break;
    }
}
if(!found)
    JOptionPane.showMessageDialog(null, "Drug not found");     

希望能有所帮助。

1)看(听起来第二帧应该是一个对话框。2)为了更快地获得更好的帮助,可以添加or。感谢Andrew,我用LEE的答案解决了问题,但在阅读了您链接的帖子后,我会重新考虑使用多帧