9 public class boxdemo2 extends jframe constructor

This preview shows page 2 out of 2 pages.

9 public class BoxDemo2 extends JFrame { /** Constructor: frame with title t and 3 columns with n, n+1, and n+2 buttons. */ public BoxDemo2(String t, int n) { super (t); // Create Box b1 with n buttons. Box b1= new Box(BoxLayout.Y_AXIS); for ( int i= 0; i != n; i= i+1) b1.add( new JButton(”1 " + i)); // Create Box b2 with n+1 buttons. Box b2= … // Create Box b3 with n+2 buttons. Box b3= … // Create horizontal box b containing b1, b2, b3 Box b= new Box(BoxLayout.X_AXIS); b.add(b1); b.add(b2); b.add(b3); Container cp= getContentPane(); cp.add(b, BorderLayout.CENTER); pack(); show(); } Boxes within a Box 3 vertical boxes, each a column of buttons, are placed in a horizontal box BoxLayout layout manager: Place any number of components. They appear in the order in which they were added, taking only one row. 10 You can change the layout manager of a JFrame --but not to a BoxLayout manager. To change it to a flow layout, use: getContentPane.setLayout( new FlowLayout()); To simulate using a BoxLayout manager for a JFrame, create a Box and place it as the sole component of the JFrame : JFrame jf= new JFrame(“title”); Box b= new Box(BoxLayout.X_AXIS); Add components to b; jf.add(b,BorderLayout.CENTER); Changing Layout Managers 11 1.Many people start developing a GUI by changing an already existing one. There are a lot of details, and it is hard to get all the details right when one starts from scratch and has little idea about the Java GUI package. 2.The easiest way to learn about GUIs is to listen the the ProgramLive lectures in Chapter 17 . That chapter shows you code for everything, and you can also download the code from the CD and compile and use it yourself. 3.We have shown you how to place components in a GUI. We haven’t yet shown you how to “listen” to things like button clicks in a GUI. That comes later. For those interested in learning more about building GUIS:
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