JFrame doesn't take the actual screen size

浪子不回头ぞ 提交于 2021-02-05 11:56:18

问题


I have been trying to set the size of my JFrame to the exact same size of my screen (2256x1504). It also seems to take that size but when I display something the outcome is always bigger than intended.

public class Frame extends JFrame {

    public Frame() {
        JPanel p = new JPanel();
        int width = 2256;
        int height = 1504;
        Dimension size = new Dimension(width,height);
        p.setPreferredSize(size);
        p.setMinimumSize(size);
        p.setMaximumSize(size);
        p.setSize(size);
        this.setUndecorated(true);
        this.setContentPane(p);
        this.pack();
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.setVisible(true);
    }
}

回答1:


int width = 2256;
int height = 1504;

Guessing your screen is 2556x1504. You give the frame this size. When we full screen a program (from default button OS gives in upper right corner of the window), it does not get screen's size. It takes screen's size - the height of the OS task bar. I do not know for other operating systems, but if you want to make an undecorated frame full screen in windows and involve the task bar, you have to use this:

public class Frame extends JFrame {

    public Frame() {
        JPanel p = new JPanel();
        int width = 2256;
        int height = 1504;
        Dimension size = getScreenDimensionWithoutTaskbar(this);
        p.setPreferredSize(size);
        p.setMinimumSize(size);
        p.setMaximumSize(size);
        p.setSize(size);
        this.setUndecorated(true);
        this.setContentPane(p);
        this.pack();
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.setVisible(true);
    }
    public static Dimension getScreenDimensionWithoutTaskbar(Frame frame) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = screenSize.width;
        int height = screenSize.height;
        Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(frame.getGraphicsConfiguration());
        int taskBarSize = screenInsets.bottom;
        return new Dimension(width, height - taskBarSize);
    }
    public static void main(String[] args) {
        new Frame();
    }


}



回答2:


I have been trying to set the size of my JFrame to the exact same size of my screen (2256x1504).

Don't use magic numbers. Each computer can be using a screen with a different resolution. Let Swing determine the size. Use:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

So the basic structure of the code would be:

JPanel panel = new JPanel();
add( panel );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setUndecorated(true);
//setResizable(false); // not needed since the title bar is removed
setExtendedState(JFrame.MAXIMIZED_BOTH);
//pack(); // not needed since the frame will be the screen size
setVisible(true);


来源:https://stackoverflow.com/questions/60787057/jframe-doesnt-take-the-actual-screen-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!