将jFileChooser中选择的文件设置为字符串?

时间:2015-01-07 14:39:25

标签: java swing

我想将jFileChooser中选择的文件设置为stringbuilder段的字符串,但是,以特定模式。即。

        {
            sb.append(" -file").append(mods);
        }

" mods"是文件,每个文件之间是一个" -file"例如,如果我选择file1.wad,file2.wad和file3.pk3,那么程序的所需输出将是

-file file1.wad -file file2.wad -file file3.pk3

我该怎么做?

编辑这是我的更多代码。代码Paco Abato建议去哪里?因为在代码的底部会吐出大量的错误。

private void RunGameButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    String PlayerCount = PlayerCountHere.getText();
    String HostIP = HostIPHere.getText();
    //String MapWarp = MapWarpBox.getName();
    try
    {
        //Basic game launching command.
        StringBuilder sb = new StringBuilder("cmd.exe /C start C:\\mygame\\game.exe");
        if(NoMonstersBox.isSelected())
        {
            //Add the No Monsters Flag
            sb.append(" -nomonsters");
        }
        if(CheatsBox.isSelected())
        {
            //Add the Cheats Flag
            sb.append(" +SV_CHEATS 1");
        }
        if(Netmode0Button.isSelected())
        {
            //Add Netmode 0 Flag
            sb.append(" -netmode 0");
        }
        if(Netmode1Button.isSelected())
        {
            //Add Netmode 1 Flag
            sb.append(" -netmode 1");
        }
        if(DeathmatchButton.isSelected())
        {
            //Add the Deathmatch Flag
            sb.append(" -deathmatch");
        }
        if(CoopButton.isSelected())
        {
            //Add the Co-op Flag
            sb.append(" -coop");
        }
        if(Skill1Button.isSelected())
        {
            //Add the Skill 1 Flag
            sb.append(" -skill 1");
        }
        if(Skill2Button.isSelected())
        {
            //Add the Skill 2 Flag
            sb.append(" -skill 2");
        }
        if(Skill3Button.isSelected())
        {
            //Add the Skill 3 Flag
            sb.append(" -skill 3");
        }
        if(Skill4Button.isSelected())
        {
            //Add the Skill 4 Flag
            sb.append(" -skill 4");
        }
        if(Skill5Button.isSelected())
        {
            //Add the Skill 5 Flag
            sb.append(" -skill 5");
        }
        if(HostButton.isSelected())
        {
            //Add the Host Flag and add the Player Count from the box
            sb.append(" -host ").append(PlayerCount);
        }
        if(JoinButton.isSelected())
        {
            //Add the Join Flag and add the Host IP from the box
            sb.append(" -join ").append(HostIP);
        }
        //Launch the game
        Process process = Runtime.getRuntime().exec(sb.toString());
    }
    catch(IOException e)
    {
        //Log Error
    }
}

1 个答案:

答案 0 :(得分:1)

这很简单:

for (File file : selectedFiles) {
    sb.append(" -file ").append(file.getName());
}