JOptionPane再次出现一次

时间:2012-11-07 14:19:15

标签: java swing user-interface dialog joptionpane

这是我第二次在stackOverflow上发布问题。 在我的代码中,我有一个JOptionPane,允许用户按“确定”或“取消”。 如果用户按下“OK”,则代码应返回包含ALL值或SELECTED值的ArrayList。

我已经测试了这段代码,它看起来似乎有效,或者看起来如此。每当用户第一次单击“确定”按钮时,JOptionPane将关闭,但之后会再次出现。当用户第二次单击“确定”按钮时,代码就像它应该的那样工作,并且窗口关闭而不再出现。

/**
 * This method will show a list of files to the user and give the user the option to continue or abort the copy.
 * 
 * @param resultList
 * @return ArrayList with values or null
 */
public ArrayList<String> display(ArrayList<String> resultList) {
    JPanel panel = new JPanel();
    //Here the list is being filled with the arrayList from the parameter.
    JList list = new JList(resultList.toArray());
    //Here the JLabel is added to the JPanel.
    panel.add(new JLabel("These files will be copied:"));
    JScrollPane scrollpane = new JScrollPane(list);
    scrollpane.setPreferredSize(new Dimension(400,200));
    panel.add(scrollpane);

    //Here the JOption,Pane is being shown with a confirm dialog.
    int result = JOptionPane.showConfirmDialog(null, panel, "Copy",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    //If the users clicks on the OK button the method will return the selected values.
    if (result == JOptionPane.OK_OPTION) {
        //Check if the user has selected some values, if not the method will return all the values.
        if(list.getSelectedValues().length < 1){
            return resultList;
        }
        //If the user has selected some values the method will get them, fill an ArrayList with them and return them.
        else{
            @SuppressWarnings({ "rawtypes", "unchecked" })
            ArrayList<String> results = new ArrayList(Arrays.asList(list.getSelectedValues()));
            return results;
        }
    }
    //Return null if the user has pressed Cancel.
    else{
        return null;
    }
}

我认为问题出在这一行:

 if (result == JOptionPane.OK_OPTION) {

但是我不明白为什么因为我已经在我的actionHandler中为删除按钮做了类似的事情,它的工作原理应该如此。

/**
     * This is the actionListener for the delete button.
     * This method will delete the values and properties of the selected configuration.
     */
    deleteButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            MessageConsole mc = new MessageConsole(textArea);
            //Here the getSaveAction is being used with the correct parameter values that are gained from the fields.
            mc.redirectOut();
            //Check if the user has entered a configuration name.
            if(configName.getSelectedItem().toString().length() > 0){
                int result1 = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this configuration?", "Delete", JOptionPane.YES_NO_OPTION);
                if(result1 == JOptionPane.YES_OPTION){
                    int result2 = JOptionPane.showConfirmDialog(null,"Are you really sure?", "Delete", JOptionPane.YES_NO_OPTION);
                    if(result2 == JOptionPane.YES_OPTION){
                        //If the user has entered a configuration name the method will delete all the properties and values that belong to it.
                        gf.getDeleteAction(configName.getSelectedItem().toString());
                        sourceField.setText("");
                        destinationField.setText("");
                        endsWithField.setText("");
                        containsField.setText("");
                        yearAfterField.setText("");
                        monthAfterField.setText("");
                        dayAfterField.setText("");
                        hourAfterField.setText("");
                        minuteAfterField.setText("");
                        secondAfterField.setText("");
                        yearBeforeField.setText("");
                        monthBeforeField.setText("");
                        dayBeforeField.setText("");
                        hourBeforeField.setText("");
                        minuteBeforeField.setText("");
                        secondBeforeField.setText("");
                        setComboBox();
                    }
                    if(result2 == JOptionPane.NO_OPTION){
                        System.out.println("Nothing has been deleted.");
                    }
                }

                if(result1 == JOptionPane.NO_OPTION){
                    System.out.println("Nothing has been deleted.");
                }
            }
            //If the user has not entered a configuration name the system will print a message.
            else{
                System.out.println("You need to select a configuration name");
            }
        }
    });

我希望我的问题有足够的结构和信息来回答。

-lordarnoud

1 个答案:

答案 0 :(得分:1)

我犯了一个愚蠢的错误。最初使用的代码没有JOptionPane。所以我忘了把它用在支票上。

if(cfa.display(resultList) != null){
            results.addAll(cfa.display(resultList));

我要感谢那些评论过的人。我已经学到了一些关于提问的新方法,并确保检查我的代码的每一部分,即使它看起来似乎无关。

-lordarnoud