What is the relation between ContentPane and JPanel?

前端 未结 6 1811
情书的邮戳
情书的邮戳 2020-12-01 02:14

I found one example in which buttons are added to panels (instances of JPanel) then panels are added to the the containers (instances generated by getCont

6条回答
  •  眼角桃花
    2020-12-01 02:39

    I believe the reason is because Swing was built off of AWT, and Container is a top level AWT object. It really isn't the greatest design choice, though, since you generally don't want to mix AWT (heavyweight) objects with Swing (lightweight).

    I think the best way to handle it is to always cast the contentPane to a JPanel.

    JPanel contentPanel = (JPanel)aFrame.getContentPane();
    

提交回复
热议问题