如何获得最大化的框架尺寸?

hud*_*udi 4 java swing jframe

我设置了最大化的框架: setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

现在我怎么能得到这个尺寸,因为当我打电话getSize()getPreferredSize它返回时0 0

Cos*_*lis 5

setVisible(true);执行后,您将正确获得最大化的大小.

public NewJFrame() {                            // Constructor
    initComponents();
    this.setExtendedState( getExtendedState() | JFrame.MAXIMIZED_VERT | Frame.MAXIMIZED_HORIZ);
    // height and width still prints the original values 
    System.out.println(this.getSize().height + " " + this.getSize().width); 
}

....

public static void main(String args[]) {        // main
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            NewJFrame foo = new NewJFrame();
            foo.setVisible(true);
            // after setVisible(true) actual maximized values
            System.out.println(foo.getSize().height + " " + foo.getSize().width);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)