6 components that can contain other components

This preview shows page 1 - 2 out of 2 pages.

6 Components that can contain other components Component Box Container JComponent JPanel Panel Applet Window Frame JFrame JWindow java.awt is the old GUI package. javax.swing is the new GUI package. When they wanted to use an old name, they put J in front of it. (e.g. Frame and JFrame) When constructing javax.swing, the attempt was made to rely on the old package as much as possible. So, JFrame is a subclass of Frame. But they couldn’t do this with JPanel.
Image of page 1

Subscribe to view the full document.

2 7 /** Instance has labels in north /south, JPanel with four buttons in center. */ public class PanelDemo extends JFrame { JPanel p= new JPanel(); /** Constructor: an invisible frame with title t, two labels in north and south and a JPanel of 4 buttons in the center */ public PanelDemo(String t) { super (t); p.add( new JButton("0")); p. add (new JButton("1")); p.add( new JButton("2")); p. add (new JButton("3")); Container cp= getContentPane(); cp.add( new JLabel("north"), BorderLayout.NORTH); cp.add( new JCheckBox("I voted today"), BorderLayout.SOUTH); cp.add(p, BorderLayout.CENTER); pack(); show(); } } JPanel as a container JPanel layout manager default : FlowLayout. FlowLayout layout manager: Place any number of components. They appear in the order in which they were added, taking as many rows as necessary. 8 /** Demo class Box. Comment on constructor says how frame is laid out */ public class BoxDemo extends JFrame { /** Constructor: frame with title t, labels in the east / west, blank label in south, horizontal Box with three buttons in center. */ public BoxDemo(String t) { super (t); Box b= new Box(BoxLayout.X_AXIS); b.add( new JButton("first")); b.add( new JButton("second")); b.add( new JButton("third")); Container cp= getContentPane(); cp.add( new JLabel("WEST Label"), BorderLayout.WEST); cp.add( new JLabel("EAST Label"), BorderLayout.EAST); cp.add( new JLabel(" "), BorderLayout.SOUTH); cp.add(b, BorderLayout.CENTER); pack(); } } Class Box: a container Box layout manager default : BoxLayout. BoxLayout layout manager: Place any number of components. They appear in the order in which they were added, taking only one row.
Image of page 2
You've reached the end of this preview.
  • Spring '07
  • DAVIDGRIES

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern