BoxLayout for a JFrame

后端 未结 1 563
礼貌的吻别
礼貌的吻别 2021-01-20 16:22

Could you help me understand what is going on here. I consulted Javadoc: JFrame has setLayout method. So, what sharing error springs out is a mystery to me.



        
相关标签:
1条回答
  • 2021-01-20 16:52

    Try this one on JFrame#getContentPane()

    this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
    

    Read more How to Use BoxLayout


    All the components are added in JFrame's content pane.

    Read more Adding Components to the Content Pane

    Here is the pictorial representation how JFrame looks like

    enter image description here


    EDIT

    From comments:

    Well, not clear anyway. I analyze it like this: BoxLayout class needs to know it target. JFrame has setLayoutt method and needs to know its layout.

    this.setLayout(manager) internally calls getContentPane().setLayout(manager);

    The below line

    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    

    is converted to below line that is not correct.

    this.getContentPane().setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    

    For more detail have a look at Source code

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