您在這裡

JFrame with JButtons but no event handlers (Frame1.java)

15 一月, 2016 - 09:02
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6
Note: Frame1.java is available in the source code archive file.

To display other GUI components on a JFrame, we first need to get the content pane of the JFrame and then add the desired GUI components to this content pane.

If we want to arrange the added GUI components in certain ways, we need to add an appropriate "layout manager" to the JFrame. The task of laying out the GUI component inside of a container GUI component is specifed by an interface called LayoutManager. In Frame1, we add the FlowLayout that arranges all GUI components in a linear fashion. If we do not add any layout manager to the JFrame, it has no layout manager and does a very bad job of arranging GUI components inside of it. As an exercise, comment out the code to add the FlowLayout to Frame1 and see what happens!