在我的程序中,我存储了ArrayList
个所有桌面图标位置。我的问题是,当我点击一个图标时,我的计算机会一次尝试打开所有最后一个程序,文件夹和文件,而我真的只想打开点击的内容。如何在没有启动其他所有程序的错误的情况下打开这个?
public void executeUserProgram(Point cursorPosition)
{
for (int i = 0; i < icons_.getIcon().size(); i++)
{
if (icons_.getIconDimension().get(i).contains(cursorPosition))
{
try
{
Desktop.getDesktop().open(
new File(icons_.getFilePath().get(i)));
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
答案 0 :(得分:2)
您的for
循环在找到图标后仍然继续。在return
或break
内添加if
或try
语句。
try{
Desktop.getDesktop().open(new File(icons_.getFilePath().get(i)));
break;
}