setMaximumSize 在 Java 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4061010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:37:04  来源:igfitidea点击:

setMaximumSize not working in java

javajframe

提问by thejh

I hava a java program with a JFrame

我有一个带有 JFrame 的 java 程序

I am using absolute positioning

我正在使用绝对定位

here is my main function

这是我的主要功能

public static void main(String[] args) {
    ape Ape = new ape();
    Ape.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Ape.setSize(1000,1000);
    Ape.setMinimumSize(new Dimension(1000,1000));
    Ape.setMaximumSize(new Dimension(1000,1000));
    Ape.setVisible(true);
}

When I run the program I try to resize it and make the window smaller but I can't

当我运行程序时,我尝试调整它的大小并使窗口变小,但我不能

when I try to make the window bigger it works fine I basicly skips the setMaximumSize()function

当我尝试使窗口变大时,它工作正常,我基本上跳过了该setMaximumSize()功能

I have read around and aparently this has happened before

我已经阅读了,显然这以前发生过

is this a known bug?

这是一个已知的错误?

if so I heard I could make a Window Listener, when I tried it I implemented the functions that WindowListener needed but could not find anything to solve my problem

如果是这样,我听说我可以制作一个 Window Listener,当我尝试它时,我实现了 WindowListener 需要的功能,但找不到任何东西来解决我的问题

please try this yourself and see what happens...

请自己尝试一下,看看会发生什么......

thanks in advance

提前致谢

PS... please don't laugh about the names I give my classes... :)

PS...请不要嘲笑我给我的课程起的名字... :)

回答by thejh

see http://forums.sun.com/thread.jspa?threadID=5342801:

http://forums.sun.com/thread.jspa?threadID=5342801

It's a known bug:

这是一个已知的错误:

Maybe you could use

也许你可以用

Ape.setResizable(false)

instead?

反而?

PS: It's a convention to give classes names that start with a capital letter and variables ones with a small letter, not vice versa.

PS:类名以大写字母开头,变量名以小写字母开头是一种约定,反之亦然。

回答by Johntor

In my case I used the following and it worked:

在我的情况下,我使用了以下内容并且它有效:

    Dimension newDim = new Dimension(width, height);

    label.setMinimumSize(newDim);
    label.setPreferredSize(newDim);
    label.setMaximumSize(newDim);
    label.setSize(newDim);
    label.revalidate();

回答by Coder ACJHP

I fixed it like this :

我是这样修的:

    frame.setBounds(0, 0, 1480, 910);
    frame.setMinimumSize(new Dimension(1200, 799));
    frame.setMaximumSize(new Dimension(1480, 910));
    frame.setPreferredSize(new Dimension(1480, 910));
    frame.setLocationRelativeTo(null);
    frame.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            double w = frame.getSize().getWidth();
            double h = frame.getSize().getHeight();
            if(w > 1480.0 && h > 910.0){
                frame.setSize(new Dimension(1480, 910));
                frame.repaint();
                frame.revalidate();
            }

            super.componentResized(e);
        }

    });

回答by A.Aleem11

For Netbeans user try to set values for the maximum frame in setMaximizedBounds() click in properties of frame you will find an option to define values for setMaximizedBounds.

对于 Netbeans 用户,尝试在 setMaximizedBounds() 中设置最大帧的值,单击帧的属性,您将找到一个选项来定义 setMaximizedBounds 的值。