2015-11-29 28 views
0

您好我正在使用Java Swing开发一个程序,并且我有4个选项窗格用于获取某些输入,但是当我运行该程序时它将显示选项窗口,但是当我关闭选项窗格,并运行最后一个按钮,我必须执行程序的其余部分。我目前困惑,为什么。这里是actionPerformed()方法的代码和文件选择器的方法。请注意,选项窗格用于从单选按钮获取输入,而不用选择是或否,因此4检查*方法用于查看单选按钮被按下以及如何处理该信息。JOptionPane正在运行FileChooser

public void actionPerformed(ActionEvent e) { 

    if (e.getActionCommand().equals("settings")) { 
     JOptionPane.showOptionDialog(null, encryptPanel, 
       "Settings Choices", JOptionPane.NO_OPTION, 
       JOptionPane.QUESTION_MESSAGE, null, null, null); 

    } 
    if (e.getActionCommand().equals("paths")) { 
     JOptionPane.showOptionDialog(null, pathsPanel, 
       "Paths Options", JOptionPane.NO_OPTION, 
       JOptionPane.QUESTION_MESSAGE, null, null, null); 

    } 
    if (e.getActionCommand().equals("tools")) { 
     JOptionPane.showOptionDialog(null, toolsPane, 
       "Tools Options", JOptionPane.NO_OPTION, 
       JOptionPane.QUESTION_MESSAGE, null, null, null); 

    } 
    if (e.getActionCommand().equals("techniques")) { 
     JOptionPane.showOptionDialog(null, methodPane, 
       "Choose your encryption method", JOptionPane.YES_NO_CANCEL_OPTION, 
       JOptionPane.QUESTION_MESSAGE, null, null, null); 
    } 
    checkEncrypt(e.getActionCommand()); 
    checkPaths(e.getActionCommand()); 
    checkTools(e.getActionCommand()); 
    checkTech(e.getActionCommand()); 

    if (e.getActionCommand().equals("go")) ; 
    { 
     runLauncher(); 

    } 
} 
private void runLauncher() 
{ 

    directory.makeDir("PEP"); 
    JFileChooser getFile = new JFileChooser(); 
    getFile.setCurrentDirectory(new File(System.getProperty("user.home"))); 
    int result = getFile.showOpenDialog(this); 
    String str; 
    int numWheels = Integer.getInteger(wheels.getText()); 
    if (result == JFileChooser.APPROVE_OPTION) { 

     str = getFile.getSelectedFile().getAbsolutePath(); 


     int result2 = getFile.showOpenDialog(this); 
     if (result2 == JFileChooser.APPROVE_OPTION) { 
      String endFilePath = getFile.getSelectedFile().getAbsolutePath(); 
      if(gOn) 
      { 
       launcher go = new launcher(str, endFilePath, numWheels, 5); 
       go.run(); 
      } 
      else 
      { 
       launcher go = new launcher(str, endFilePath, numWheels, selection); 
       go.run(); 
      } 
     } 

     selection = 0; 

    } 
} 
+1

除了你的问题,你有几件事情要做。通常的做法是从大写的类名开始,然后将每个单词的每个首字母大写。即SomeVeryLongClassName。其次,如果你反复调用同样的方法,那么你做错了。调用一次并将其分配给一个变量。例如:String cmd = e.getActionCommand();那么你可以用cmd代替。第三,只有一个命令得到执行,这是说。而不是使用,如果如果...等等,你应该使用,如果否则,如果...等等。现在它没有区别,但很快它会。 – Gacci

+0

我很欣赏我需要修复的错误。仍然必须解决我的其他问题,它与每个JOptionPane运行最后一点位 – Snipeshot101

回答

3
if (e.getActionCommand().equals("settings")) { 
    JOptionPane.showOptionDialog(null, encryptPanel, 
      "Settings Choices", JOptionPane.NO_OPTION, 
      JOptionPane.QUESTION_MESSAGE, null, null, null); 
} 

... 

checkEncrypt(e.getActionCommand()); 

JOptionPane.showOptionDialog(...)方法不改变actionCommand的值。它只是返回一个int值,代表哪个按钮被点击。

所以基本上你是无缘无故地显示选项窗格,因为你的代码从不使用从选项窗格返回的值。

因此,也许你的代码应该是这样的:

int option = JOptionPane.showOptionDialog(...); 

然后您可以根据返回的值做你的处理。

checkEncrypt(option); 

或者,也许代码应该是这样的:

if (e.getActionCommand().equals("settings")) 
{ 
    int option = JOptionPane.showOptionDialog(...): 
    checkEncrypt(option); 
} 

我不知道为什么你单独调用这些四种方法。

无论如何你的代码需要重新构造。

+0

我可能没有解释JOptionPanes除了yes,no,cancel options之外还有其他按钮。它们仅用于从弹出窗口获得其他输出单选按钮。我不知道如果JOptionPane.showOptionDialog(...)的返回值会给我选定的单选按钮,并且checkEncrypt(...)是一种缩短actionListener方法的方法,所以我没有那个页面很长,它会检查用户选择的按钮。 – Snipeshot101

+0

@ Snipeshot101,重点是你没有使用JOptionPane的任何信息。您只需显示选项窗格,然后忽略它。也许你应该使用'JOptionPane.showInputDialog(...)'来获取用户的输入。有关更多信息和示例,请阅读[如何制作对话框]的Swing教程(http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html)。 – camickr

+0

啊,好的,谢谢你告诉我一个更好的方式去做这件事。 – Snipeshot101