2014-02-14 92 views
2

我知道这个问题有一些主题(主要是this unanswered onethis one,它不处理全屏应用程序)。JFileChooser在全屏幕Swing应用程序

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

有没有人有这个不那么特别的用例的工作示例?或者是否有解决方法让用户选择全屏应用程序中的文件?

+0

为什么'setExtendedState(JFrame.MAXIMIZED_BOTH)'而不是? – trashgod

+0

因为最大化的窗口不是全屏窗口 - Windows任务栏在那里,我不想要它 – bagage

+0

我很少使用全屏幕,从来没有注意到这种效果。您可以编辑您的问题以阐明需求,确定目标平台,并包含显示您当前方法的[最小,完整,测试和可读示例*](http://stackoverflow.com/help/mcve)。 – trashgod

回答

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)相关联。

希望这个解决方案对你有用,如果没有的话: 你能解释一点点,你如何创建全屏应用?

+0

感谢您的例子(注意:给定的代码缺少'import java.awt.Frame;')。不幸的是,这不适用于CentOS,[见图片](http://postimg.org/image/p8wljn0gr/786a51ec/)。我会在今天下午在Windows上尝试它,但我认为这不会奏效。我将编辑我的问题。 – bagage

+0

其实你的代码在Windows 8上工作,但不能在CentOS上工作(以前的评论),并没有完全在gnome-shell上工作,[见第二张图片](http://s11.postimg.org/h3m2zndn7/aaaa.png )。但是随着你的编辑,这也在gnome-shell上工作。 – bagage

+0

Jep,我也认识到gnome的问题......所以编辑;)。我不知道CentOS知道如何解决这个问题。你自己找到了解决方案吗?我发现一个有关全屏应用程序和Java 6的错误的问题。也许这有助于[链接](http://stackoverflow.com/questions/8837719/java-in-full-screen-on-linux-how-to-cover -task-bar) – ROT13

0

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

你可以使用我的图书馆创建全屏窗口和你所面对的问题,希望以后我测试和它的工作解决了。

希望它可以帮到你

相关问题