Fundamentals of Computer Science II (CSC-152 2000F)


Class 22: GUIs, Revisited

Back to Arrays. On to Algorithm Analysis.

Held Monday, October 2, 2000

Summary

Today we revisit GUI design.

Notes

Overview


A question on homework 3 prompted a long discussion and "lab" like experience on building GUIs. Here's my sample code:


import java.awt.Frame;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Button;
import java.awt.TextField;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * A simple illustration of how to make GUIs using panels and
 * the basic layouts.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of October 2000
 */
public class Fooey {
  Frame mainFrame;
  TextField mainData;
  Panel stuff;
  Panel numbers;
  Panel controls;

  /** Build everything */
  public Fooey() {
    mainFrame = new Frame("A Simple GUI");
    mainFrame.setSize(300,500);
    mainFrame.setLayout(new FlowLayout());

    mainData = new TextField("0000000") {
      public Dimension getPreferredSize() {
        return new Dimension(300,150);
      }
    };
    mainData.setSize(300,150);
    mainFrame.add(mainData);

    stuff = new Panel();
    stuff.setSize(300,350);
    stuff.setLayout(new FlowLayout());
    mainFrame.add(stuff);

    numbers = new Panel() {
      public Dimension getPreferredSize() {
        return new Dimension(200,200);
      }
    };
    numbers.setSize(200,200);
    numbers.setLayout(new GridLayout(3,3));
    stuff.add(numbers);

    for (int i = 1; i <= 9; i++) {
      Button b = new Button(Integer.toString(i));
      b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
         mainData.setText(mainData.getText() + "X");
        }
      });
      numbers.add(b);
    }

    controls = new Panel();
    controls.setSize(100,200);
    controls.setLayout(new GridLayout(3,1));
    stuff.add(controls);

    controls.add(new Button("X"));
    controls.add(new Button("Y"));
    controls.add(new Button("Z"));

    mainFrame.show();
  } // Fooey()

  /** Go for it! */
  public static void main(String[] args) {
    new Fooey();
  } // main(String[])

} // class Fooey



History

Wednesday, 23 August 2000

Thursday, 24 August 2000

Monday, 2 October 2000

Tuesday, 3 October 2000

Back to Arrays. On to Algorithm Analysis.


Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2000F/Outlines/outline.22.html

Source text last modified Wed Oct 25 10:05:37 2000.

This page generated on Fri Oct 27 08:19:56 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu