-3

试图让它工作,它是我的 GUI 的最后一部分,不完全确定这一切是如何工作的:L 这是我的代码。“youtube,facebook,twitter”部分是字符串调用顺便说一句。基本上发生的事情是 facebook 选项同时打开 twitter 和 facebook。

public static void SocialMedia(){
    Object[] possibleValues = { "Twitter", "YouTube", "FaceBook" };
    Object selectedValue = JOptionPane.showInputDialog(null,"Where would you like to go?", "",JOptionPane.QUESTION_MESSAGE, null,possibleValues, possibleValues[0]);
    if (selectedValue != null) { 
        if (selectedValue.toString().equals(possibleValues[2])) {
            try {
                Desktop.getDesktop().browse(new URI(YouTube));
            } catch (IOException | URISyntaxException e) {
        }
    } else {
            try {
                Desktop.getDesktop().browse(new URI(Twitter));
            } catch (IOException | URISyntaxException e) { }
    }

            try {
            Desktop.getDesktop().browse(new URI(FaceBook));
            } catch (IOException | URISyntaxException e) {}

    }
}
4

1 回答 1

2
  1. 你需要一些“else if (....)”块。

例如,

if (selectedValue.toString().equals(possibleValues[2])) {
  //...
} else if (selectedValue.toString().equals(possibleValues[1])) {
  //...
} else if (selectedValue.toString().equals(possibleValues[0])) {
  // ...
}

这将阻止您不想运行的代码块运行。

  • 你不需要有空的 catch 块,因为这样会带来危险和疯狂。
  • 您需要更好地格式化您的代码,以便其他人(我们!)可以轻松阅读和理解它。
  • 于 2013-07-09T02:04:41.103 回答