JFrame getLayout method does not return set cardLayout

我怕爱的太早我们不能终老 提交于 2019-12-24 13:15:59

问题


I am setting a CardLayout as the a JFrame's layout but when I call the getLayout method a border layout is returned.

import java.awt.CardLayout;

import javax.swing.JFrame;

public class SSCCE {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        CardLayout cl = new CardLayout();
        frame.setLayout(cl);
        System.out.println(cl);
        System.out.println(frame.getLayout());

    }
}

This is printed to console:

java.awt.CardLayout[hgap=0,vgap=0]
java.awt.BorderLayout[hgap=0,vgap=0]

回答1:


JFrame redirects the setLayout() to content pane. That's source snippet

public void setLayout(LayoutManager manager) {
    if(isRootPaneCheckingEnabled()) {
        getContentPane().setLayout(manager);
    }
    else {
        super.setLayout(manager);
    }
}


来源:https://stackoverflow.com/questions/27376254/jframe-getlayout-method-does-not-return-set-cardlayout

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