Setting minimum size limit for a window in java swing

♀尐吖头ヾ 提交于 2019-12-28 14:01:06

问题


I have a JFrame which has 3 JPanels in GridBagLayout..

Now, when I minimize a windows, after a certain limit, the third JPanel tends to disappear. I tried setting minimizing size of JFrame using setMinimumSize(new Dimension(int,int)) but no success. The windows can still be minimized.

So, I actually want to make a threshhold, that my window cannot be minimized after a certain limit.

How can I do so?

Code:-

import java.awt.Dimension;

import javax.swing.JFrame;

public class JFrameExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Hello World");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setMinimumSize(new Dimension(400, 400));
        frame.setVisible(true);
    }
}

Also:

shadyabhi@shadyabhi-desktop:~/java$ java --showversion
java version "1.5.0"
gij (GNU libgcj) version 4.4.1

Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Usage: gij [OPTION] ... CLASS [ARGS] ...
          to invoke CLASS.main, or
       gij -jar [OPTION] ... JARFILE [ARGS] ...
          to execute a jar file
Try `gij --help' for more information.
shadyabhi@shadyabhi-desktop:~/java$

Gives me output like

**UPDATE: ** The same when run though Netbeans IDE gives expected output.. When I run through "java JFrameExample" compiler, I am facing issues.. Now, what that means??


回答1:


The documentation tells me, that this behavior is platform dependent. Especially, since the following example code works for me as desired in Windows Vista:

import java.awt.Dimension;

import javax.swing.JFrame;

public class JFrameExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Hello World");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setMinimumSize(new Dimension(100, 100));
        frame.setVisible(true);
    }
}



回答2:


There actually is a way to ensure minimum size on any platform. You need to set the minimum size of the JFrame to the minimum size of its content pane and then you need to write a ComponentAdapter and override componentResized. Then you just use getSize and getMinimum size on your JFrame and substitute width and/or height with the minimum width or height if it is greater. Assuming you are extending JFrame:

this.addComponentListener(new ComponentAdapter(){
        public void componentResized(ComponentEvent e){
            Dimension d=YourJFrame.this.getSize();
            Dimension minD=YourJFrame.this.getMinimumSize();
            if(d.width<minD.width)
                d.width=minD.width;
            if(d.height<minD.height)
                d.height=minD.height;
            YourJFrame.this.setSize(d);
        }
    });


来源:https://stackoverflow.com/questions/2781939/setting-minimum-size-limit-for-a-window-in-java-swing

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