Java JRootPane在全屏模式下的大小调整问题

Java JRootPane在全屏模式下的大小调整问题,java,swing,jpanel,fullscreen,sizing,Java,Swing,Jpanel,Fullscreen,Sizing,我试图用JRootPane创建一个占据整个屏幕的JFrame,但是由于某些原因,JRootPane的内容并没有像它应该的那样填满屏幕 我相信问题在于JRootPane没有填满它的父面板,但是有可能JRootPane的子面板没有填满根面板 这是它当前显示的内容: 以下是相关代码: public class Runner { public static void main(String[] args){ GraphicsEnvironment graphicsEnviro

我试图用JRootPane创建一个占据整个屏幕的JFrame,但是由于某些原因,JRootPane的内容并没有像它应该的那样填满屏幕

我相信问题在于JRootPane没有填满它的父面板,但是有可能JRootPane的子面板没有填满根面板

这是它当前显示的内容:

以下是相关代码:

public class Runner {
    public static void main(String[] args){
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
        new MainFrame(devices[0]);
}
}
这是带有根窗格的JFrame,应该完全填充它:

public class MainFrame extends JFrame{
private GraphicsDevice graphicsDevice;
private DisplayMode origDisplay;
private final JRootPane rootPane;

public MainFrame(GraphicsDevice graphicsDevice){
    super();
    this.setGraphicsDevice(graphicsDevice);
    this.setOrigDisplay(graphicsDevice.getDisplayMode()); 

    rootPane = new JRootPane(); 
    rootPane.setContentPane(TitleScreenPanel.getInstance(this));
    this.add(rootPane, BorderLayout.CENTER);

    if (graphicsDevice.isFullScreenSupported()){
      setUndecorated(true);
      setResizable(false);
      graphicsDevice.setFullScreenWindow(this);
      revalidate();
    }else{
      System.out.println("Full-screen mode not supported");
    }  

    try {
        Theme.loadTheme(new File("res/IS.theme"));
        UIManager.setLookAndFeel(new TinyLookAndFeel());
    } catch (UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }


    SwingUtilities.updateComponentTreeUI(this);
    Toolkit.getDefaultToolkit().setDynamicLayout(true);
    System.setProperty("sun.awt.noerasebackground", "true");
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    rootPane.revalidate();


}

public DisplayMode getOrigDisplay() {
    return origDisplay;
}

public void setOrigDisplay(DisplayMode origDisplay) {
    this.origDisplay = origDisplay;
}

public GraphicsDevice getGraphicsDevice() {
    return graphicsDevice;
}

public void setGraphicsDevice(GraphicsDevice graphicsDevice) {
    this.graphicsDevice = graphicsDevice;
}

}
正在添加到JRootPane的面板:

public class TitleScreenPanel extends JPanel{
private static TitleScreenPanel titleScreenPanel;
private JButton exitButton;
private JButton startButton;

private TitleScreenPanel(final MainFrame context){
    startButton = new JButton("START");
    startButton.setFont(startButton.getFont().deriveFont(48f));
    startButton.setBorder(BorderFactory.createEmptyBorder());
    startButton.setContentAreaFilled(false);
    exitButton = new JButton("Exit Full-Screen Mode");
    exitButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            context.getGraphicsDevice().setDisplayMode(context.getOrigDisplay());
            System.exit(0);
        }
    });
    this.add(startButton, BorderLayout.CENTER);
    this.add(exitButton, BorderLayout.SOUTH);
}

public static TitleScreenPanel getInstance(MainFrame context){
    if(titleScreenPanel == null){
        titleScreenPanel = new TitleScreenPanel(context);
    }
    return titleScreenPanel;
}
}

让我们从…开始吧,你不需要创建一个
JRootPane
,一个
JFrame
已经有了一个,事实上,你真正需要做的唯一一件事就是在
JRootPane
contentPane
中添加或设置
TitleScreenPane
,例如

//rootPane = new JRootPane();
setContentPane(TitleScreenPanel.getInstance(this));
//add(rootPane, BorderLayout.CENTER);
import java.awt.BorderLayout;
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.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame {

    public static void main(String[] args) {
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
        new MainFrame(devices[0]);
    }

    private GraphicsDevice graphicsDevice;
    private DisplayMode origDisplay;
//    private final JRootPane rootPane;

    public MainFrame(GraphicsDevice graphicsDevice) {
        super();
        this.setGraphicsDevice(graphicsDevice);
        this.setOrigDisplay(graphicsDevice.getDisplayMode());

//        rootPane = new JRootPane();
        setContentPane(TitleScreenPanel.getInstance(this));
//        add(rootPane, BorderLayout.CENTER);

        if (graphicsDevice.isFullScreenSupported()) {
            setUndecorated(true);
            setResizable(false);
            graphicsDevice.setFullScreenWindow(this);
            revalidate();
        } else {
            System.out.println("Full-screen mode not supported");
        }

//        try {
//            Theme.loadTheme(new File("res/IS.theme"));
//            UIManager.setLookAndFeel(new TinyLookAndFeel());
//        } catch (UnsupportedLookAndFeelException e) {
//            e.printStackTrace();
//        }
//        SwingUtilities.updateComponentTreeUI(this);
//        Toolkit.getDefaultToolkit().setDynamicLayout(true);
        System.setProperty("sun.awt.noerasebackground", "true");
//        JFrame.setDefaultLookAndFeelDecorated(true);
//        JDialog.setDefaultLookAndFeelDecorated(true);
//        rootPane.revalidate();

    }

    public DisplayMode getOrigDisplay() {
        return origDisplay;
    }

    public void setOrigDisplay(DisplayMode origDisplay) {
        this.origDisplay = origDisplay;
    }

    public GraphicsDevice getGraphicsDevice() {
        return graphicsDevice;
    }

    public void setGraphicsDevice(GraphicsDevice graphicsDevice) {
        this.graphicsDevice = graphicsDevice;
    }

    public static class TitleScreenPanel extends JPanel {

        private static TitleScreenPanel titleScreenPanel;
        private JButton exitButton;
        private JButton startButton;

        private TitleScreenPanel(final MainFrame context) {
            setLayout(new BorderLayout());
            startButton = new JButton("START");
            startButton.setFont(startButton.getFont().deriveFont(48f));
            startButton.setBorder(BorderFactory.createEmptyBorder());
            startButton.setContentAreaFilled(false);
            exitButton = new JButton("Exit Full-Screen Mode");
            exitButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    context.getGraphicsDevice().setDisplayMode(context.getOrigDisplay());
                    System.exit(0);
                }
            });
            this.add(startButton, BorderLayout.CENTER);
            this.add(exitButton, BorderLayout.SOUTH);
        }

        public static TitleScreenPanel getInstance(MainFrame context) {
            if (titleScreenPanel == null) {
                titleScreenPanel = new TitleScreenPanel(context);
            }
            return titleScreenPanel;
        }
    }
}
接下来,
JPanel
默认使用
FlowLayout
,因此您看到的实际上就是您应该看到的

为了获得您想要的内容,您需要将
TitleScreenPanel
的布局管理器更改为
BorderLayout

作为一个可运行的例子

//rootPane = new JRootPane();
setContentPane(TitleScreenPanel.getInstance(this));
//add(rootPane, BorderLayout.CENTER);
import java.awt.BorderLayout;
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.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame {

    public static void main(String[] args) {
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
        new MainFrame(devices[0]);
    }

    private GraphicsDevice graphicsDevice;
    private DisplayMode origDisplay;
//    private final JRootPane rootPane;

    public MainFrame(GraphicsDevice graphicsDevice) {
        super();
        this.setGraphicsDevice(graphicsDevice);
        this.setOrigDisplay(graphicsDevice.getDisplayMode());

//        rootPane = new JRootPane();
        setContentPane(TitleScreenPanel.getInstance(this));
//        add(rootPane, BorderLayout.CENTER);

        if (graphicsDevice.isFullScreenSupported()) {
            setUndecorated(true);
            setResizable(false);
            graphicsDevice.setFullScreenWindow(this);
            revalidate();
        } else {
            System.out.println("Full-screen mode not supported");
        }

//        try {
//            Theme.loadTheme(new File("res/IS.theme"));
//            UIManager.setLookAndFeel(new TinyLookAndFeel());
//        } catch (UnsupportedLookAndFeelException e) {
//            e.printStackTrace();
//        }
//        SwingUtilities.updateComponentTreeUI(this);
//        Toolkit.getDefaultToolkit().setDynamicLayout(true);
        System.setProperty("sun.awt.noerasebackground", "true");
//        JFrame.setDefaultLookAndFeelDecorated(true);
//        JDialog.setDefaultLookAndFeelDecorated(true);
//        rootPane.revalidate();

    }

    public DisplayMode getOrigDisplay() {
        return origDisplay;
    }

    public void setOrigDisplay(DisplayMode origDisplay) {
        this.origDisplay = origDisplay;
    }

    public GraphicsDevice getGraphicsDevice() {
        return graphicsDevice;
    }

    public void setGraphicsDevice(GraphicsDevice graphicsDevice) {
        this.graphicsDevice = graphicsDevice;
    }

    public static class TitleScreenPanel extends JPanel {

        private static TitleScreenPanel titleScreenPanel;
        private JButton exitButton;
        private JButton startButton;

        private TitleScreenPanel(final MainFrame context) {
            setLayout(new BorderLayout());
            startButton = new JButton("START");
            startButton.setFont(startButton.getFont().deriveFont(48f));
            startButton.setBorder(BorderFactory.createEmptyBorder());
            startButton.setContentAreaFilled(false);
            exitButton = new JButton("Exit Full-Screen Mode");
            exitButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    context.getGraphicsDevice().setDisplayMode(context.getOrigDisplay());
                    System.exit(0);
                }
            });
            this.add(startButton, BorderLayout.CENTER);
            this.add(exitButton, BorderLayout.SOUTH);
        }

        public static TitleScreenPanel getInstance(MainFrame context) {
            if (titleScreenPanel == null) {
                titleScreenPanel = new TitleScreenPanel(context);
            }
            return titleScreenPanel;
        }
    }
}


是的,我在JRootPane上被这个误导了,因为我去掉了额外的JRootPane,把布局改成了GridBagLayout,效果很好