Monday, March 5, 2007

Lecture 20

Menus

  • Important classes are

    • JMenuBar

    • JMenu

    • JMenuItem

  • all three of these are children of Component

  • all implement the add() method



Creating a Menu

  1. Create a Menubar

    • JMenuBar myMenuBar = new JMenuBar();

  2. Create menus and add them to the menubar

    • JMenu = new JMenu(“File”);

    • MyMenuBar.add(myMenu);

  3. Create and add menu items to the menu

    • JMenuItem Save = new JMenuItem(“Save”);

    • MyMenu.add(Save);

  4. Add action listener to each menu item

    • Save.addActionListener(this);

  5. Add menuBar to the JFrame

    • add (myMenuBar);//Bad!!

    • Set JMenuBar(myMenuBar); // Good!!





Windows and Inner Classes



  • For Complicated window components it makes sense to use inner classes

Recall:

Public class OuterClass;

{

Private class InnerClass

{

}

}

  • A complicated panel within a JFrame

Public class myJframe extends JFrame

{

Public myJPanel()

{//appearance of the panel;}

}

Public JFrame()

{

Add ( new myJPanel());

}

Private class myJPanel extends JPanel implements ActionListener

{

Public myJPanel()

{

JButton b = new JButton(“Button”);

b.addActionListener(this); /* this refers to myJPanel*/

add(b);

}

Public void ActionPerformed(ActionEvent e){…}



Text Field

  • JTextField

    • Constructor takes an integer size argument

    • JTextField t = newJTextField(20); /* 20 chars wide*/

      • public String getText()

      • public void setText(String S)

      • public void setEditable(boolean e)



Wednesday, February 28, 2007

Lecture 19

JButton myButton = new JButton("x");
myListener m = new myListener;
myButton.addActionListener(m);
-------------------------------
/* can be replaced with
below */
-------------------------------
myButton.addActionListener(new myListener());


public myJFrame()
{
super("title of Window");
.
.
.
JButton myButton = new JButton("title of button");
myButton.addActionListener( new myListener());
}

What happens when you have multiple buttons?

  • Bad way
    • define different listener classes for each button
    • register the appropriate class to the appropriate button
  • Good way
    • examine e
      • object of type ActionEvent
      • ActionEvent has a method returning the label of the button pressed

public String getActionCommand()
/* Andrew drew a picture of two buttons labeled different things then pointed out that when you click the button "one" e.getActionCommand returns "One"*/

  • These implement the interface LayoutManager
    • BorderLayout
    • FlowLayout
    • GridLayout

  • A window (JFrame) is a container
    • allowed to contain objects and have layouts
  • a window can be divided into regions using LayoutManager
    • each region can be made into a container by declaring it a JPanel
  • a panel is a Container

  1. Declare a LayoutManager
    1. setLayout(new BorderLayout());
  2. Add a apanel to as many regions as you want
    1. JPanel myPanel = new JPanel();
    2. add(MyPanel, BorderLayout, North);
  3. Declare a layout to the panel
    1. myPanel.SetLayout(new.FlowLayout());

Monday, February 26, 2007

Lecture 18

  1. Create the component

  2. Create a listener

  3. Create an instance of the listener

  4. Add (“register”) the listener to the component

  5. Add the component to the JFrame



  • You can skip steps 2 – 4 if your program doesn’t require an action



  • How does this work?

    • Create a visual interface

    • A JFrame

    • A button

  • Then implement a listener

    • this is the code which you wish to perform once the button Is pressed

  • register the listener to the button

    • when the button is pressed

    • Java calls ActionPerformed

      • You do not call ActionPerformed

      • ActionPerformed is an automation in Java

  • Registration tells Java to call it when the button is pressed

    • And then reads the listener



Labels

  • can add a line of text using JLabel component

    1. create a component

    2. JLabel MyLabel = new JLabel(“My Label”);

    3. Skip

    4. Skip

    5. Skip

    6. Add the component to the JFrame

      • MyWindow.add(MyLabel);



Layouts

  • We require a way to arrange multiple objects within a window

  • Java technique which accomplishes that is the layout

    • BorderLayout

    • GridLayout

      • FlowLayout

  • JFrame contains a method setLayout()

  • MyWindow.setLayout(Layout l);

  • Executing this sets the layout of the window to the layout object l.

  • Andrew is not sure that setLayout take a Layout as its param, he will find out for next class

BorderLayout

  • When Adding a component also specify a region in which It goes

    • MyWindow.add(MyButton, BorderLayout.North/East/South/West/Center)



GridLayout

GridLayout GL;

GL = new GridLayout (2, 3)

//2 = rows, 3 = columns

MyWindows.setLayout(GL);

JLabel Label 1 = new JLabel (“1”)

MyWindow.add (Label 1);



Flow Layout

flowLayout FL;

FL = newFlowLayout ();

MyWindow.setLayout (FL);

// add components

Good JFrame Practice

  • Up to now, everything has been done in main

  • This is bad practice because the window is an object

  • It should be treated as such

  • We are creating a modification of JFrame

  • Use inheritance


Friday, February 23, 2007

Lecture 17

  • JFrame is the class that handles windows in Java
    • import 'javax.Swing.JFrame;
    • creating a window is equivalent to creating an object of type JFrame
    • JFrame has an extensive public interface

public void setSize(int width, int height)
public void setTitle(String title)
public JFrame(String title)
public void setVisible(boolean v)
public void add(Component x)

  • notice that the program didn't end when the window closes by default
  • default window close behaviour is to hide the window (but not exit the program)
  • we have to handle the event where the user clicks close

public void setDefaultCloseOperation(int x)
  • we can use
JFrame.EXIT_ON_CLOSE
JFrame.HIDE_ON_CLOSE
JFrame.DO_NOTHING_ON_CLOSE

  • These are given integer values (in order) 3,0,1
    • do not use these values as Java might change those numbers in an update


Action Listeners and Action Events

  • each component must register the listener that handles its events
  • Java interface ActionListener
    • event type is a class called ActionEvent
      • a listener you define must implement the interface ActionListener
public void actionPerform(ActionEvent e)
{

}

  1. Create the component
    1. JButton myButton = new JButton(" ");
  2. Write a listener class
    1. public void myListener implements ActionListener
      {
      public void actionPerformed(ActionEvent c)
      {
      }
      }
  3. Create an instance of the listener
    1. MyListener m = new myListener();
  4. register the listener with the component
    1. myButton.addActionListener(m)
  5. add the component to the JFrame
    1. myWindow.add(MyButton);

Wednesday, February 21, 2007

Finished Test 2 Impressions

Just wrote the test yesterday...
  • had to make an
    • abstract class
    • a child extended class from that class
    • an array which takes in args parameters and inputs them into the class
  • i found it to be somewhat simple
    • but the time we had i didn't feel was enough
    • and Andrew's API was quite unfinished
      • and seemed to have a typo in a method description in his API
    • being given a test that had to support an unfinished API
      • and had to start from scratch
    • was VERY difficult for me to get done
  • still felt the test was fair, for what it tested
  • but my advice to those who haven't written it
    • practice your speed
    • be sure of your coding practice

later
Jake

Lecture 16

  • polymorphism is a very powerful tool because many related classes can be handled the same way and through the same public interface.
  • java figures out which overridden method is the public interface to use

Student
  • Undergrad
  • Graduate
  • all 'students' have a routine for calculating letter grades

public string getLetterGrade()

public void printTranscript(Student s)
{
System.out.println(s.getLetterGrade());
}

  • u - undergraduate
  • g - graduate student
  • q - special student
    • these are all classes which have getLetterGrade overrides
      • print transcript(u)
      • print transcript(g)
      • print transcript(q)
Polymorphism and Arrays

  • consider an array of students
    • conglomeration of undergraduate students, graduate students and others.
  • how do we create the array?
Arrays vs. non-arrays
Student[] s; Student S;
s = new Student[5]; S= new Student(); // not legal


Graphical User Interfaces (GUI)

  • so for our programs we have started on the first line, gone through every line in sequence, and terminated at the end
  • windows does not work that way
  • the programmer doesn't determine the order of events
  • the program 'listens' for user-generated events and passes them to the event handler
  • A GUI program consists largely of event handlers that respond to events
  • you would almost never call event handler methods instead java calls them when the even happens

Monday, February 19, 2007

Lab Test 2 Impressions

Here you can post how you felt the test went, give pointers to those who haven't taken it and so on.