在Java中最大化JFrame [英] Maximizing JFrame in Java

查看:110
本文介绍了在Java中最大化JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有与此相关的几个问题,例如此处此处,两者都说最大化JFrame的方法是使用以下代码:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //Some answers have these lines
frame.setVisible(true);                        //reversed

但是,对我来说,不确定这是否是Windows 10 bug/java 8 bug,当我使用此代码时,结果是这样的(无论上述两行代码采用哪种方式):

如您在图像中所见,该窗口是正确的大小,但是它略微覆盖了底部,并且相对于左侧略有偏移.有没有办法解决此问题,或者通过用代码点击JFrame上的最大化按钮来实际上使程序最大化?

修改
这是演示该问题的MCVE:

import java.awt.GridLayout;
import javax.swing.JFrame;

public class CDBurner extends JFrame {
    private static final long serialVersionUID = -6027473114929970648L;

    private CDBurner() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(1, 1));
        setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
        setVisible(true);
        setLocationRelativeTo(null);
        requestFocus();
    }

    public static void main(String[] args) {
        new CDBurner();
    }
}

解决方案

setLocationRelativeTo(null);

设置框的位置.如果是之后

setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

它在最大化后更改框架的位置,从而导致观察到的间隙.您应该删除该行,因为无论如何最大化框架都会设置其位置(尽管您可以将其放在设置扩展状态之前,在这种情况下它什么也不做).

There have been several questions related to this, for example here and here, that both say the way to maximize a JFrame is to use the following code:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //Some answers have these lines
frame.setVisible(true);                        //reversed

However, for me, not sure if this is a windows 10 bug/java 8 bug or not, when I use this code the result is this (no matter which way round the two lines of code above are):

As you can see in the image, the window is the correct size, however, it slightly overlays the bottom, and it is slightly offset from the left. Is there a way to fix this problem, or actually maximise the program by hitting the maximise button on the JFrame with code?

Edit
Here is a MCVE that demonstrates the problem:

import java.awt.GridLayout;
import javax.swing.JFrame;

public class CDBurner extends JFrame {
    private static final long serialVersionUID = -6027473114929970648L;

    private CDBurner() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(1, 1));
        setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
        setVisible(true);
        setLocationRelativeTo(null);
        requestFocus();
    }

    public static void main(String[] args) {
        new CDBurner();
    }
}

解决方案

The line

setLocationRelativeTo(null);

sets the position of the frame. If it comes after

setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

it changes the location of the frame after it maximizes, causing the gap you observed. You should remove that line since maximizing the frame sets its position anyway (though you can just put it before setting the extended state, in which case it does nothing).

这篇关于在Java中最大化JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆