Setting minimum size limit for a window in java swing

前端 未结 2 1699
猫巷女王i
猫巷女王i 2020-12-06 04:18

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

Now, when I minimize a windows, after a certain limit, the

相关标签:
2条回答
  • 2020-12-06 04:38

    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);
            }
        });
    
    0 讨论(0)
  • 2020-12-06 04:40

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题