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,你认为这会解决这个问题还是有其他方法可以解决这个问题?

4

2 回答 2

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);
于 2012-12-26T18:19:54.283 回答
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);
        }
    }
}
于 2014-02-17T17:20:40.943 回答