程序打开所有应用程序

时间:2013-04-13 13:05:48

标签: java user-interface desktop-application executable

在我的程序中,我存储了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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您的for循环在找到图标后仍然继续。在returnbreak内添加iftry语句。

try{
    Desktop.getDesktop().open(new File(icons_.getFilePath().get(i)));
    break;
}