0
final JFileChooser chooser = new JFileChooser(); 
JOptionPane.showInternalOptionDialog(this, chooser, "Browse",
        JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
        new Object[]{}, null);
            chooser.addActionListener(new ActionListener() {        
                public void actionPerformed(ActionEvent e) {
                   if(e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION))

                        System.out.println("File selected: " +
 chooser.getSelectedFile());
                        //code to close here
                    } else { 
                        //code to close here
                    }
                }
            });

这段代码看起来很奇怪,但它只是我程序的一部分。我使用全屏GraphicsDevice。我将文件选择器放在内部JOptionPane中以保留全屏窗口。现在我想以编程方式关闭JOptionPane内部本身,而不是在我的 actionlistener 中关闭整个应用程序。怎么做到呢?

4

1 回答 1

0

When you call showInternalOptionDialog, you cannot easily access the reference to the dialog. You could add options, ie. use

new Object[] { "Browse", "Cancel" }

instead of

new Object[]{}

but then you would end up having two sets of buttons. I think there is no easy way to use showInternalOptionDialog with JFileChooser. I would suggest to create JInternalFrame yourself.

final JFileChooser chooser = new JFileChooser();
JOptionPane pane = new JOptionPane(chooser, JOptionPane.PLAIN_MESSAGE,
            JOptionPane.DEFAULT_OPTION, null, new Object[0], null);
final JInternalFrame dialog = pane.createInternalFrame(this, "Dialog Frame");
chooser.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
            System.out.println("File selected: "
                    + chooser.getSelectedFile());
        }
        dialog.setVisible(false);
    }
});
dialog.setVisible(true);

Also in your code snippet you are invoking addActionListener after showInternalOptionDialog. The showInternalOptionDialog invocation will return only after you close the dialog, so the ActionListener will be created too late. You need to first addActionListener and then show the dialog.

于 2013-01-01T19:18:54.590 回答