Java 调整JFileChooser的大小

Java 调整JFileChooser的大小,java,swing,jframe,Java,Swing,Jframe,对不起,我是个初学者。我正在尝试调整JFileChooser的大小,使其成为屏幕分辨率的大小。为了获得分辨率,我使用了以下代码: GraphicsDevice gd = GraphicsEnvironment .getLocalGraphicsEnvironment().getDefaultScreenDevice(); int width = gd.getDisplayMode().getWidth(); int height = gd.getDisplayMode().get

对不起,我是个初学者。我正在尝试调整
JFileChooser
的大小,使其成为屏幕分辨率的大小。为了获得分辨率,我使用了以下代码:

GraphicsDevice gd = GraphicsEnvironment
        .getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
我已经搜索过了,但找不到如何有效地更改
JFileChooser
的大小。我测试了我找到的一些解决方案,但它们不起作用。

试试这个(注意注释):


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


考虑<代码> JFrm。最大化两者;将
JFileChooser
添加到具有
GridLayout
的面板中。
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);
}