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);

1 comment:

K said...

you guys already did GUI @_@, thats fast ...