简体   繁体   中英

How to check current window size in java swing?

例如,窗口的大小已更改(用户调整了大小),如何获取当前窗口的大小?

Rectangle r = frame.getBounds();
h = r.height;
w = r.width;

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());

只需使用getSize()方法: javadoc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM