1
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 を閉じるにはどうすればよいですか。

4

2 に答える 2

1

あなたの問題はありません

chooser.getFocusCycleRootAncestor().setVisible(false);

これらの変更を行うと、コードは問題なく動作します

この部分を外すだけ

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

代わりにこのコードを追加します

 chooser.showOpenDialog(frame);

さらに懸念がある場合はお知らせください

于 2012-12-29T04:39:21.473 に答える
0

問題は、プログラムがまだモーダルダイアログが開いていると考えていることです。これにより、フォーカスがモーダルダイアログに制限されます...

chooserあなたのをこのようなものに変えてみてください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 {
        }
    }
});

これは基本的JOptionPaneに、ユーザーが値(実際には何も提供していない)を選択したと思い込ませてダイアログを閉じ、制御をアプリケーションに戻します。

于 2012-12-29T12:16:41.717 に答える