有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在jFrame中居中

我试图将我的帧定位在屏幕中央,我知道这段代码必须运行良好:

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((screen.getWidth() - getWidth()) /2);
int y = (int) ((screen.getHeight() -getHeight()) /2);
setLocation(x, y); 

或者这个:

Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);

我已经尝试了所有可能的代码,但我不知道为什么它们不起作用,只需将帧移动到屏幕的右下角,我使用了setRelative to null,但同样不起作用

我最终可以通过将宽度乘以5,再除以一些东西,使框架非常接近中心,但我知道这不是正确的方法

谁能给我解释一下有什么问题吗

问题已修复:

解决方案: 我自己找到了最好的方法,如果你曾经遇到过同样的问题,只需让netbeans通过转到属性并检查生成中心自动完成,忘记那些愚蠢的代码


共 (4) 个答案

  1. # 1 楼答案

    在主屏幕上居中显示框架的最简单方法(如果有多个屏幕,则有一些选项)是使用setLocationRelativeTo(…)参数为null的帧方法:

    JFrame frame = new JFrame ();
    frame.setSize ( 500, 300 );
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
    

    如果不起作用,请尝试此选项(直接坐标):

    Dimension ss = Toolkit.getDefaultToolkit ().getScreenSize ();
    Dimension frameSize = new Dimension ( 500, 300 );
    
    JFrame frame = new JFrame ();
    frame.setBounds ( ss.width / 2 - frameSize.width / 2, 
                      ss.height / 2 - frameSize.height / 2,
                      frameSize.width, frameSize.height );
    frame.setVisible ( true );
    

    最后一个“终极”示例-它将在每个可用系统屏幕上显示居中的框架:

    public static void main ( String[] args )
    {
        Dimension frameSize = new Dimension ( 500, 300 );
        for ( GraphicsDevice screen : getGraphicsDevices () )
        {
            GraphicsConfiguration gc = screen.getDefaultConfiguration ();
            Rectangle sb = gc.getBounds ();
    
            JFrame frame = new JFrame ( gc );
            frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
            frame.setBounds ( sb.x + sb.width / 2 - frameSize.width / 2,
                    sb.y + sb.height / 2 - frameSize.height / 2, frameSize.width,
                    frameSize.height );
            frame.setVisible ( true );
        }
    }
    
    public static List<GraphicsDevice> getGraphicsDevices ()
    {
        List<GraphicsDevice> devices = new ArrayList<GraphicsDevice> ();
        for ( GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment ()
                .getScreenDevices () )
        {
            if ( gd.getType () == GraphicsDevice.TYPE_RASTER_SCREEN )
            {
                if ( gd == GraphicsEnvironment.getLocalGraphicsEnvironment ()
                        .getDefaultScreenDevice () )
                {
                    devices.add ( 0, gd );
                }
                else
                {
                    devices.add ( gd );
                }
            }
        }
        return devices;
    }
    
  2. # 2 楼答案

    I've already tried it, it is not working , could anything be wrong with the netbeans ??

    也许netbeans没有错

    I'm trying to locate my frames at the center of the screen

    你可以试试看

    JFrame frame = new JFrame ();
    frame.pack(); // or frame.setSize(int, int);
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
    
  3. # 3 楼答案

    我敢打赌,在设置JFrame的大小之前,您可以通过x和y坐标设置相对于null的位置/设置位置。如果尚未设置大小,则将其视为0x0,将其死点设置为屏幕中心。如果您随后更改大小,它将保留原来的左上角(屏幕中心),并向屏幕右下角缩放

    换句话说,在屏幕上居中设置setSize()之前

    你所做的一切在我看来都很好