JFileChooser在全屏Swing应用程序前面

时间:2014-02-14 17:32:17

标签: java swing fullscreen jfilechooser

我知道有一些与此问题相关的主题(主要是this unanswered onethis one,它们不处理全屏应用)。

我基本上尝试了第一个主题示例和可用方法(requestFocus,requestFocusInWindow,...)的每个组合,但JFileChooser始终显示在全屏应用程序后面。我也尝试更改filechooser的父级(将其设置为null,本身或父框架),但没有成功。

有没有人能够举例说明这个不那么特别的用例?或者是否有一种解决方法让用户在全屏应用中选择文件?

3 个答案:

答案 0 :(得分:3)

不幸的是,我不能说你是如何实现全屏应用的实现的。但我尝试了一些事情并想出了这个:

import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Gui extends JFrame {

    public Gui() {

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        //this.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
        // Set some charateristics of the frame
        this.setExtendedState(Frame.MAXIMIZED_BOTH);
        this.setBackground(Color.black);
        this.setUndecorated(true);

        JButton a = new JButton("PRESS ME!");

        a.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser fc = new JFileChooser();
                fc.showOpenDialog(getParent());
            }
        });

        this.add(a);

        this.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Gui();
            }
        });
    }
}

注意这个事实,我创建了一个新的JFileChooser,当前JFrame的父节点作为参数。

修改 我现在甚至试图设置

java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(new Gui());

没有

this.setUndecorated(true);

它对我有用(有一个漂亮的全屏视图,JFileChooser在前面)。我相信窗口装饰的问题与我的窗口管理器有关(我正在使用linux和gnome)。

希望这个解决方案适合您,如果不是: 您能解释一下,如何创建全屏应用程序?

答案 1 :(得分:0)

我建议不要使用Popup,只需将JFileChooser嵌入到您的应用程序中。在无窗口应用程序中弹出窗口并没有多大意义(就个人而言,我不喜欢弹出窗口)。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FullScreenApp {

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        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)); // Ugh.
        frame.setVisible(true);

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

        panel.add(btn);
        frame.add(panel);

        final CustomFileChooser chooser = new CustomFileChooser(panel);

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               chooser.show();
            }
        });
    }

    public static class CustomFileChooser extends JFileChooser{
         /** Node this chooser should be added to.
          *  There's likely a better way of doing this, 
          *  but it was convenient for a quick example */
        Container parent;

        public CustomFileChooser(Container parent){
            super();
            this.parent = parent;
            //Make configurations for your file chooser
            setApproveButtonText("Open");
        }

        @Override
        public void approveSelection(){
            super.approveSelection();
            //Perform accept action here
            System.out.println(getSelectedFile().getAbsolutePath());
            parent.remove(CustomFileChooser.this);
            parent.repaint();
        }

        @Override
        public void cancelSelection(){
            super.cancelSelection();
            //Perform cancel action here
            System.out.println("Canceled");
            parent.remove(CustomFileChooser.this);
            parent.repaint();
        }

        @Override
        public void show(){
             rescanCurrentDirectory();
             parent.add(this);
             revalidate();
             repaint();
        }

        @Override
        public Dimension getMaximumSize(){
            //Not necessary - But I felt the chooser should have a maximum size
            return new Dimension(500,300);
        }
    }
}

答案 2 :(得分:0)

matplotlib - extracting data from contour lines

    //import
    import argha.util.Fullscreen;

    //this for JFrame
    //true for setting Undecorated on/off
    Fullscreen screen = new Fullscreen(this, true);
    screen.DoTheWorkFor();

您可以使用我的库来创建全屏窗口,而您遇到的问题希望它在我测试及其工作之后解决。

希望它可以帮到你