gpt4 book ai didi

java - 设置复选框/单选框以向 JButton 添加额外的 cmd 参数?

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

事实上,我有很多个复选框/单选框,首先,主 jButton 是“RunGameButton”,然后我在这里得到了它的代码

private void RunGameButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
try
{
Process process = Runtime.getRuntime().exec("cmd.exe /C start C:\\mygame\\game.exe");
}
catch(IOException e)
{
}

}

例如,我有一个名为“NoMonstersBox”的复选框,选中后,它会将 -nomonsters 命令添加到 mygame.exe 的启动中,并添加到“RunGameButton”的代码中,如下所示

private void RunGameButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
try
{
Process process = Runtime.getRuntime().exec("cmd.exe /C start C:\\mygame\\game.exe -nomonsters");
}
catch(IOException e)
{
}

}

但是,我几乎是一个新手,我不知道该把什么放在哪里,或者把什么放在哪里。大量的谷歌搜索没有得到任何有用的结果。

编辑

这是我当前要求的内容

private void RunGameButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
try
{
if(CheatsBox.isSelected())
{
Process process = Runtime.getRuntime().exec("cmd.exe /C start C:\\mygame\\game.exe +SV_CHEATS 1");
}
if(NoMonstersBox.isSelected())
{
Process process = Runtime.getRuntime().exec("cmd.exe /C start C:\\mygame\\game.exe -nomonsters");
}
else
{
Process process = Runtime.getRuntime().exec("cmd.exe /C start C:\\mygame\\game.exe");
}
}
catch(IOException e)
{
//Log Error
}
}

最佳答案

假设您的 NoMonsters 标志由名为 chkNoMonsters 的复选框表示,您可以做的就是将您的 RunGameButtonActionPerformed 事件处理程序增强为考虑复选框的状态,如下所示:

private void RunGameButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
try
{
if(chkNoMonsters.isSelected())
{
Process process = Runtime.getRuntime().exec("cmd.exe /C start C:\\mygame\\mygame.exe -nomonsters");
}
else
{
Process process = Runtime.getRuntime().exec("cmd.exe /C start C:\\mygame\\game.exe");
}
}
catch(IOException e)
{
//Log Error
}
}

另外,作为旁注,捕获异常而不对其执行任何操作被认为是回溯实践。您通常至少要做的就是记录它们,以便您意识到出现了问题。

编辑:根据您的问题编辑,主要问题是您有 2 组条件组。第一组由以下 if 语句表示:if(CheatsBox.isSelected()),第二组由 if(NoMonstersBox.isSelected()) 表示...否则...

为了确保您的游戏仅运行一次,您需要将两组条件组折叠为一组。为此,您只需将 if(NoMonstersBox.isSelected()) 替换为 else if(NoMonstersBox.isSelected()) 即可。这将确保仅选择 3 个条件中的一个。

但是,这将使您的选择相互排斥,这意味着您将无法同时启用 NoMonsters+SV_CHEATS 1 标志。要解决这个问题,您可以使用 StringBuilder类,类似这样:

private void RunGameButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
try
{
//Basic game launching command.
StringBuilder sb = new StringBuilder("cmd.exe /C start C:\\mygame\\game.exe");
if(chkNoMonsters.isSelected())
{
//Add the No Monsters Flag
sb.append(" -nomonsters");
}
if(CheatsBox.isSelected())
{
//Add the Cheats Flag
sb.append(" +SV_CHEATS 1");
}

//Launch the game
Process process = Runtime.getRuntime().exec(sb.toString());
}
catch(IOException e)
{
//Log Error
}
}

关于java - 设置复选框/单选框以向 JButton 添加额外的 cmd 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27815707/

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