java - JOptionPane 运行 FileChooser

标签 java swing joptionpane

您好,我正在使用 Java Swing 开发一个程序,并且设置了 4 个选项 Pane 来获取某些输入,但是当我运行该程序时,它会显示选项窗口,但当我关闭选项 Pane 时它运行并运行最后一个按钮,我必须执行程序的其余部分。我目前很困惑为什么。以下是 actionPerformed() 方法和文件选择器方法的代码。请注意,选项 Pane 用于从单选按钮获取除"is"或“否”选择之外的输入,因此 4 个检查* 方法用于查看按下了哪个单选按钮以及如何处理该信息。

    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;

    }
}

最佳答案

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 值。

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

所以也许你的代码应该是这样的:

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

然后根据返回的值进行处理。

checkEncrypt( option );

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

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

我不知道为什么要分别调用这四个方法。

无论如何,您的代码都需要重组。

关于java - JOptionPane 运行 FileChooser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33987768/

相关文章:

java - JTextArea 在 actionListener 之前填充

java - 我的 JPanel 中的 .showMessageDialog 出现错误?

java - 在 Java 中的 JPanel 上移动图像

java - 同步链表 - peek

java - 放置在 5 个不同 JPanel 中的 Jbutton 数组中的 Action 监听器

Java - 有没有一种快速的方法可以用其他颜色替换某些颜色位图中的所有实例?

java - JOptionPane 怎么禁用X?

java - JPanel 中的paintComponent 期间的JOptionPane 输入对话框

java - 选择一个好的排序算法

java - 如何将 Java 1.5 类与 Java 1.6 类链接?