My JFrame always becomes a few pixels too big.

十年热恋 提交于 2019-12-17 20:53:50

问题


I am working on this game in Java, and I just rewrote all my window related code from being based on java.awt to javax.swing. Soon after, I realized that things were a bit more complicated now, and so i did some research and and found out how to draw things, how to set the size of a JFrame, etc. But for some reason, the size of my JFrame always goes 10 pixels beyond the size I've specified it to become. In this case, I wanted it to be 640 by 640 pixels.

Here's my code:

package chrononaut;

import java.awt.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;


@SuppressWarnings("serial")
public class GameJFrame extends JFrame 
    {

    //Background color:
    static Color bgrCol = new Color(12, 4, 64);

    //Size:
    final static int size = 640;

    //The component that does the actual drawing:
    static GameJComp drawingComp = new GameJComp();

    //Constructor:
    public GameJFrame()
        {
        //Calling super() and setting the title:
        super("Chrononaut");

        //Setting icon image:
        try 
        {
        Image icon = ImageIO.read(getClass().getResource("/icon/icon 1.png"));
        setIconImage(icon);
        }
        catch (IOException e) {}

        //make it closable:
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


        //Setting the size:
        Container c = getContentPane();
        Dimension d = new Dimension(size, size);    //for some reason, it goes 10 pixels beyond the given values
        c.setPreferredSize(d);
        pack();

        //Adding the drawing component to the content pane:
        getContentPane().add(drawingComp);

        //Making sure the window size stays constant:
        setResizable(false);

        //Centering position:
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension s = tk.getScreenSize();
        setLocation(s.width/2-getWidth()/2, s.height/2-getHeight()/2);

        //Background color:
        setBackground(bgrCol);

        //Make it visible:
        setVisible(true);

        }


    public void drawAll()
        {
        drawingComp.drawAll();
        }

    public void closeGame()
        {
        dispose();
        System.exit(0);
        }

}

When I run it, the space inside the borders of the JFrame appears to have a size of 650 pixels, not 640, even when I call setPreferredSize(d). I tried making Dimension d = new Dimension(size - 10, size - 10); which does seem to work, but I have no idea of weather it would on other platforms. I haven't seen this problem anywhere else on the web, so I have absolutely no idea of why it does this. :(


回答1:


This is an issue related to using setResizable(false); AFTER you've tried setting the size of the window.

The problem is that (on Windows at least), the size of the frames border is different between resizable and non-resizable windows.

Call setResizable before calling pack

If you want a better (and generally more reliable) way to center your window, consider using

setLocationRelativeTo(null);

instead of

setLocation(s.width/2-getWidth()/2, s.height/2-getHeight()/2);

Toolkit.getScreenSize doesn't take into consideration the space taken up by the task bar...



来源:https://stackoverflow.com/questions/26689006/my-jframe-always-becomes-a-few-pixels-too-big

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