【问题标题】:JFileChooser in front of fullscreen Swing application全屏 Swing 应用程序前的 JFileChooser
【发布时间】:2017-12-20 13:05:21
【问题描述】:

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

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

有没有人提供这个不太特别的用例的工作示例?或者是否有一种解决方法让用户在全屏应用中选择文件?

【问题讨论】:

  • 为什么不改用setExtendedState(JFrame.MAXIMIZED_BOTH)
  • 因为最大化窗口不是全屏窗口 - Windows 任务栏就在那里,我不想要它
  • 我很少用全屏,也没注意到这个效果。您可以编辑您的问题以阐明要求,确定目标平台,并包含一个 Minimal, Complete, Tested and Readable Example,以显示您当前的方法。

标签: java swing fullscreen jfilechooser


【解决方案1】:

很遗憾,我无法说明您是如何实现全屏应用的。但我尝试了一些事情并想出了这个:

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)。

希望此解决方案对您有用,如果不是: 您能否再解释一下,您是如何创建全屏应用的?

【讨论】:

  • 感谢您的示例(注意:给定的代码缺少 import java.awt.Frame;)。不幸的是,这不适用于 CentOS,see picture。今天下午我将在 Windows 上尝试它,但我认为这行不通。我将编辑我的问题。
  • 实际上,您的代码在 Windows 8 上运行,但在 CentOS 上运行不正常(之前的评论)并且在 gnome-shell 上运行不完全,see the second picture。但是通过您的编辑,这也适用于 gnome-shell。
  • Jep,我也认识到 gnome 的问题......因此编辑;)。我对 CentOS 的了解不够,不知道如何解决这个问题。您自己找到解决方案了吗?我发现了一个关于全屏应用程序和 Java 6 的错误问题。也许这有助于link
  • 其实我没有在centOS机器上测试EDIT方案;我明天试试。即使它不起作用,您的解决方案也很好,因为 centOS 不是我的操作系统支持的目标之一。
  • 我很高兴听到这个消息。也许您可以在尝试 CentOS 后发表评论。我只是好奇;)。
【解决方案2】:

我建议不要使用弹出窗口,而是将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);
        }
    }
}

【讨论】:

    【解决方案3】:

    FullscreenLib

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

    您可以使用我的库来创建全屏窗口,您所面临的问题希望在我测试并正常工作后得到解决。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      相关资源
      最近更新 更多