How do I add a bunch of JPanels to my JFrame using Java Swing?

狂风中的少年 提交于 2019-12-12 18:16:50

问题


I generate a bunch of JPanels and then pass them into a class that extends JFrame. How do I add an indefinite number of JPanels to this JFrame. I was also reading about JScrollPane should I incorporate this somehow into the design?

Example Code:

class foo extends JPanel
{
  //generate JPanels
}

class bar extends JFrame
{
  //grab some amount of foo classes and put them into this JFrame and show it
}

Also is there anything I need to watch out for when showing this JFrame?

Thanks


回答1:


How do I add an indefinite number of JPanels to this JFrame?

CardLayout, JDesktopPane/JInternalFrame, JTabbedPane, JScrollPane - there are a number of options.

Also is there anything I need to watch out for when showing this JFrame?

(shrugs)

  • Construct and show GUI components on the EDT.
  • pack() the GUI before setting the position and calling setVisible(true).
  • Don't rely on the default layouts of content panes.
  • Don't implement custom painting in a top level container.
  • ..



回答2:


JFrame -> JScrollPane -> fathers JPanel then you'll decide which of LayoutManager will lay your bunch of JPanels, by defalut FlowLayout, don't forget to play with PreferedSize for childsPanels



来源:https://stackoverflow.com/questions/6037573/how-do-i-add-a-bunch-of-jpanels-to-my-jframe-using-java-swing

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