簡體   English   中英

全屏Swing應用程序前面的JFileChooser

[英]JFileChooser in front of fullscreen Swing application

我知道有一些與這個問題有關的主題(主要是這個未回答的問題和這個沒有處理全屏應用程序的問題)。

我基本上嘗試了第一個主題示例和可用方法(requestFocus,requestFocusInWindow等)的每種組合,但是JFileChooser始終顯示在全屏應用程序的后面。 我也嘗試更改filechooser的父項(將其自身設置為null或將其設置為父框架),但沒有成功。

有沒有人是這個不太特殊的用例的可行示例? 還是有一種解決方法讓用戶在全屏應用程序中選擇文件?

不幸的是,我不能說您是如何實現全屏應用程序的。 但我嘗試了幾件事,並提出了以下建議:

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

希望該解決方案對您有效,如果不能解決的話:您能否進一步說明如何創建全屏應用程序?

我建議不要使用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);
        }
    }
}

FullscreenLib

    //import
    import argha.util.Fullscreen;

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

您可以使用我的庫來創建全屏窗口,並且您遇到的問題希望在我測試並正常運行后可以解決。

希望對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM