gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:47:51 25 4
gpt4 key购买 nike

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

这工作正常,但如果我然后去创建另一种打开第二个框架的药物,填写所有信息,将该信息保存为新的药物对象,然后将其添加到 allDrugs 数组列表中,然后我返回到仍然打开的原始框架并尝试搜索新药物名称,但无法找到该新创建的药物。

这几乎就像我必须刷新框架,但我已经尝试过,removeAll,validate(),repaint()但它仍然找不到新药物。下面是搜索栏,有点像主窗口或框架 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);

如果这有点难以理解,我深表歉意。那么,当在 nframe 2 中创建新药物时,我该如何做到这一点,那么在frame1 中仍然打开的搜索栏将拾取新添加的药物?提前致谢!

最佳答案

您是否尝试过搜索药品列表中除第一项以外的任何项目?据我所知,一旦发现第一个项目不是目标,您的 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, "Drug not find"); 应该位于循环之外,并且 break; 应该被抛出。

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");

希望有帮助。

关于java - 在一帧中将对象添加到 Arraylist,但无法在第二个已打开的帧中访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57283213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com