调整JFileChooser的大小

时间:2017-06-30 19:10:55

标签: java swing jframe

抱歉无知,我是初学者。我正在尝试调整JFileChooser的大小,使其达到屏幕分辨率的大小。为了得到解决方案,我使用了以下代码:

GraphicsDevice gd = GraphicsEnvironment
        .getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();

我搜索过,但我找不到如何有效地改变JFileChooser的大小。我测试了一些我发现的解决方案,但它们没有用。

1 个答案:

答案 0 :(得分:0)

试试这个(注释评论):

import java.awt.BorderLayout;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FileChooserFrame extends JFrame {

    public FileChooserFrame() {//construct a JFrame
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); //maximize to full screen

        //constract japnel. apply a border layout manager to it
        JPanel mainPanel = new JPanel(new BorderLayout());
        add(mainPanel); //add panel to frame

        JFileChooser fc = new JFileChooser(); //construct file chooser
        mainPanel.add(fc, BorderLayout.CENTER); //add it to panel
        setVisible(true);
    }

    public static void main(String[] args) {

        new FileChooserFrame();
    }
}


如果不需要在框架中添加其他组件,可以直接将文件选择器添加到框架中(不推荐):

public FileChooserFrame() {//construct a JFrame
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); //maximize to full screen
    JFileChooser fc = new JFileChooser(); //construct file chooser
    add(fc); //add file chooser to frame
    setVisible(true);
}