How to check current window size in java swing?

前端 未结 3 443
梦如初夏
梦如初夏 2020-12-16 02:11

For instance, the size of the window changed (user resized it), how to get current window size?

相关标签:
3条回答
  • 2020-12-16 02:37
    Rectangle r = frame.getBounds();
    h = r.height;
    w = r.width;
    
    0 讨论(0)
  • 2020-12-16 02:37

    Assuming you have a JFrame in which you are drawing your interface:

    Dimension size = frame.getBounds().getSize()
    

    Returns the dimensions of the frame. Additionally, you can give the frame a resize handler to catch whenever the user adjusts the frame's size:

        class ResizeListener implements ComponentListener {
    
            public void componentHidden(ComponentEvent e) {}
            public void componentMoved(ComponentEvent e) {}
            public void componentShown(ComponentEvent e) {}
    
            public void componentResized(ComponentEvent e) {
                Dimension newSize = e.getComponent().getBounds().getSize();          
            }   
        }
    
        frame.addComponentListener(new ResizeListener());
    
    0 讨论(0)
  • 2020-12-16 02:55

    Simply use the getSize()method: javadoc

    0 讨论(0)
提交回复
热议问题