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

No comments: