关闭JOptionPane后全屏冻结

时间:2012-12-28 19:47:32

标签: java swing joptionpane

package sample;

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class NewClass {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        final JDesktopPane d = new JDesktopPane();
        frame.setTitle("Frame");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        device.setFullScreenWindow(frame);
        device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
        frame.setVisible(true);

        JButton btn = new JButton();
        btn.setText("Button");
        final JPanel panel = new JPanel();

        panel.add(btn);
        frame.add(panel);
        final JFileChooser chooser = new JFileChooser();
        chooser.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
                    System.out.println("File selected: " + chooser.getSelectedFile());
                    chooser.getFocusCycleRootAncestor().setVisible(false);
                } else {
                    chooser.getFocusCycleRootAncestor().setVisible(false);
                }
            }
        });
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showInternalOptionDialog(frame.getContentPane(), chooser, "Browse", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null);
            }
        });
    }
}

此代码看起来很奇怪,但这是使用GraphicsDevice保留全屏的唯一方法。我的问题是,当我点击JFileChooser的取消或打开按钮时,我的屏幕会冻结使用此代码chooser.getFocusCycleRootAncestor().setVisible(false);。如何在不冻结屏幕或关闭整个屏幕的情况下使用内部对话框关闭JOPtionPane。

2 个答案:

答案 0 :(得分:1)

你的问题不在

chooser.getFocusCycleRootAncestor().setVisible(false);

如果您进行了这些更改,您的代码将完美运行

删除此部分

 JOptionPane.showInternalOptionDialog(frame.getContentPane(),chooser, "Browse",JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null);

并添加此代码

 chooser.showOpenDialog(frame);

如果您有其他疑虑,请与我们联系

答案 1 :(得分:0)

问题是,程序仍然认为打开了一个模态对话框,这限制了模态对话框的重点......

尝试将chooser的{​​{1}}更改为类似的内容......

actionListener

这基本上“欺骗”chooser.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Container parent = chooser.getParent(); while (!(parent instanceof JOptionPane)) { parent = parent.getParent(); } JOptionPane op = (JOptionPane) parent; op.setValue("done"); if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) { System.out.println("File selected: " + chooser.getSelectedFile()); } else { } } }); 认为用户已经选择了一个值(你实际上没有提供任何东西)并关闭对话框,将控制权返回给你的应用程序