2012-12-26 72 views
3
import java.awt.DisplayMode; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
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.JPanel; 

public class Sample { 
    public static String audioName; 
    /** 
    * @param args the command line arguments 
    */ 
    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)); 
     frame.setVisible(true); 

     JButton btn = new JButton(); 
     btn.setText("Button"); 
     JPanel panel = new JPanel(); 

     panel.add(btn); 
     frame.add(panel); 
     btn.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       JFileChooser chooser = new JFileChooser(); 
       int returnName = chooser.showOpenDialog(frame); 
       if (returnName == JFileChooser.APPROVE_OPTION) { 
        System.out.println("Sample"); 
       } 
      } 
     }); 
    } 
} 

如何在全屏幕中顯示JFileChooser?我不熟悉JInternalFrame/JDesktopPane,你認爲這會解決這個問題還是有另一種方法呢?JFileChooser外部顯示完全屏蔽JFrame

+0

如果Java應用程序更改我的Windows顯示設置,我將永遠不會再使用該應用程序。 –

+0

@GilbertLeBlanc,java只會在運行應用程序時將其臨時更改。 – Jong

+0

@GilbertLeBlanc可能已經注意到同樣的效果,讓我對這段代碼非常感興趣。每次運行它時,它都會將屏幕最右側的兩個屏幕WinAmp強制放置在新屏幕大小的限制範圍內。這讓我意識到,看起來全屏模式在每一步都能捕捉到你。什麼是這個應用程序非常重要。它應該是'全屏'? –

回答

3

JFileChooser位於框架中央,位於裝有Java 6的Windows XP計算機上。我將框架移到了兩臺顯示器上的不同位置。

我註釋了改變顯示設置的行,並修復了一些其他問題。

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.JPanel; 
import javax.swing.SwingUtilities; 

public class Sample implements Runnable { 
    public static String audioName; 

    public void run() { 
     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)); 

     JButton btn = new JButton(); 
     btn.setText("Button"); 
     JPanel panel = new JPanel(); 

     panel.add(btn); 
     frame.add(panel); 
     frame.setExtendedState(
       frame.getExtendedState() | JFrame.MAXIMIZED_BOTH); 
     btn.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       JFileChooser chooser = new JFileChooser(); 
       int returnName = chooser.showOpenDialog(frame); 
       if (returnName == JFileChooser.APPROVE_OPTION) { 
        System.out.println("Sample"); 
       } 
      } 
     }); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Sample()); 
    } 
} 

如果你想最大化你的JFrame,您在setVisible方法在什麼地方添加以下語句。

frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH); 
+0

您註釋掉的代碼是**必需的**。 – Jong

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