JRootPane в полноэкранном режиме проблема с размером

Я пытаюсь создать JFrame с JRootPane, который занимает весь экран, однако по какой-то причине содержимое 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 с rootPane, который должен полностью его заполнить:

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

person Ben Marshall    schedule 20.08.2014    source источник


Ответы (1)


Давайте начнем с... Вам не нужно создавать JRootPane, у JFrame он уже есть, на самом деле единственное, что вам действительно нужно сделать, это либо добавить, либо установить TitleScreenPane на contentPane JRootPane, например.. .

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

Далее, JPanel по умолчанию использует FlowLayout, так что вы видите именно то, что должны видеть.

Чтобы получить то, что вы хотите, вам нужно изменить менеджер компоновки для TitleScreenPanel на BorderLayout...

В качестве работающего примера...

Полный экран

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;
        }
    }
}
person MadProgrammer    schedule 21.08.2014
comment
Да, я был введен в заблуждение относительно JRootPane этим stackoverflow.com/questions/12372302/ после избавления от дополнительной JRootPane и изменения макета на GridBagLayout, он отлично работает - person Ben Marshall; 22.08.2014