JFileChooser在全屏Swing应用程序前面

问题描述 投票:2回答:3

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

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

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

java swing fullscreen jfilechooser
3个回答
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在前面)。我相信窗口装饰的问题与我的窗口管理器有关(我正在使用带有gnome的linux)。

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


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);
        }
    }
}

0
投票

FullscreenLib

    //import
    import argha.util.Fullscreen;

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

你可以使用我的库创建全屏窗口,你面临的问题希望它在我测试和工作之后解决了。

希望它可以帮到你

© www.soinside.com 2019 - 2024. All rights reserved.