Transcript powerpoint

Lecture 25 Listening to buttons and mice
Quotes by Tony Hoare
There are two ways of constructing a software
design: (1) make it so simple that there are
obviously no deficiencies and (2) make it so
complicated that there are no obvious
deficiencies.
Inside every large program is a small program
trying to get out.
The unavoidable price of reliability is simplicity.
I was eventually persuaded of the need to design
programming notations so as to maximize the
number of errors that cannot be made, or if made,
can be reliably detected at compile time.
You cannot teach beginners top-down programming because they don't know which end is up.
1
Lecture 25 Listening to buttons and mice
Reading material is the same as the
previous lecture.
The programs we demo today are on the
website. Download them and experiment
with them!
Look at java.awt and javax.swing as
examples of how one designs a large system
of classes. This is not the only way to do
GUIs. This is how Java does it.
2
Review of lecture 23:
Lecture 24. We:
(1) Looked at different components, like
JButton, JPanel.
(2) Saw three different layout managers:
1. FlowLayout
2. BorderLayout
3. BoxLayout
They are used to lay out components as
they are added to a container.
(3) Saw how to add components to a
container. End with pack() and show().
3
Basic Hierarachy of GUI classes
Component: abstract
Canvas
Window
Container
JComponent
JButton
Dialog
Frame
JDialog
JFrame
JFileChooser
JPanel
JCheckBox
JComboBox
A Component generally has a
position and size, can be painted,
can receive input events.
Canvas: rectangular component
with methods for painting it.
Subclass it and override method
paint()
JLabel
JRadioBox
JTextArea
JTextField
4
Container: superclass of all Components that
can hold other components.
Container
Window
JDialog
JFrame
JComponent
JPanel
Components are generally added to a Container c:
c.add(new JButton(“yes”));
c.add(new JButton(“no”), “north”);
Basic top-level containers:
JWindow: top-level window with no border
JFrame: top-level window with border, menubar
JDialog: top-level window used for a dialog
JPanel, primary use: as a container of other components.
Allows one to organize objects into a unit, often to
simplify layouts. See this on the next slides.
JPanel, secondary use: paint on it with graphics
commands --lines, circles, text, etc. (instead of using
class Canvas).
5
2. implement
ActionListener
public class ButtonDemo1 extends JFrame
implements ActionListener {
private JButton westButton= new JButton("west");
private JButton eastButton= new JButton("east");
Listening to a button
/** Constructor: invisible frame: title t, 2 buttons */
public ButtonDemo1(String t) {
super(t);
Container cp= getContentPane();
cp.add(westButton,BorderLayout.WEST);
cp.add(eastButton,BorderLayout.EAST);
westButton.setEnabled(false);
3. Register this
eastButton.setEnabled(true);
object with the
westButton.addActionListener(this);
button
eastButton.addActionListener(this);
pack(); }
public void actionPerformed(ActionEvent e) {
boolean b= eastButton.isEnabled();
eastButton.setEnabled(!b);
westButton.setEnabled(b);
1. write procedure
}
actionPerformed
}
6
public class ButtonDemo2 extends JFrame
implements ActionListener {
private JButton westButton= new JButton("west");
private JButton eastButton= new JButton("east");
/** Constructor: inv frame with title t, two buttons */
public ButtonDemo2(String t) {
super(t);
Container cp= getContentPane();
cp.add(westButton,BorderLayout.WEST);
cp.add(eastButton,BorderLayout.EAST);
westButton.setEnabled(false);
eastButton.setEnabled(true);
westButton.addActionListener(this);
eastButton.addActionListener(new BeListener()); }
public void actionPerformed(ActionEvent e) {
boolean b= eastButton.isEnabled();
listener for
eastButton.setEnabled(!b);
eastButton
westButton.setEnabled(b); }
}
private class BeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
boolean b= eastButton.isEnabled(); listener for
eastButton.setEnabled(!b);
westButton,
westButton.setEnabled(b); in its own inner class
}
}
different listeners for different buttons
7
Processing mouse clicks
/** A red or green square, possibly with a pink disk */
public class Square extends JPanel {
public static final int HEIGHT= 50; // height and
public static final int WIDTH= 50; // width, square
private int x, y; // Coordinates of square on board
private bool hasDisk= false; // = "square has pink disk"
public Square(int x, int y) { // a square at (x,y)
this.x= x; this.y= y;
setPreferredSize(new Dimension(WIDTH,HEIGHT));
this.addMouseListener(new MouseEvents());
}
public void paint(Graphics g) { // paint this square
if ((x+y)%2 == 0) { g.setColor(Color.green); }
else
{ g.setColor(Color.red); }
g.fillRect(0,0,WIDTH-1,HEIGHT-1);
if (hasDisk) {
g.setColor(Color.pink);
g.fillOval(7,7,WIDTH-14,HEIGHT-14);
}
g.setColor(Color.black);
g.drawRect(0,0,WIDTH-1,HEIGHT-1);
g.drawString("("+x+", "+y+")", 10, 5+HEIGHT/2);
8
} // continued on next slide //
// Class Square (continued) //
/** Complement the "has pink disk" property */
public void complementDisk() {
hasDisk= !hasDisk;
repaint(); // Don’t call paint(Graphics) directly
}
// instead, call inherited method repaint()
/** Remove pink disk (if present) */
public void clearDisk() {
hasDisk= false;
repaint();
}
/** Contains methods that process mouse events */
public class MouseEvents extends MouseInputAdapter {
/** Complement the "has pink disk" property */
public void mouseClicked(MouseEvent e)
{ complementDisk(); }
}
}
API class MouseInputAdapter has
several methods for processing mouse
clicks, mouse presses, mouse releases, etc.
We override only one of them here.
That’s how we get the program to listen
to mouse events.
9
/** Demo use of a mouse listener.
Instance is a JFrame that is a 2x2 grid of squares, each of
class Square, and a button with title "reset". Clicking an
empty square draws a pink disk on it. Clicking again
removes it. Clicking button "reset" removes all pink disks.
This class listens to clicks of the button.
Clicks on a square are listened to in class Square. */
public class MouseDemo extends JFrame {
Box bl= new Box(BoxLayout.Y_AXIS);
Square b00= new Square(0,0);
Square b01= new Square(0,1);
Box br= new Box(BoxLayout.Y_AXIS);
Square b10= new Square(1,0);
Square b11= new Square(1,1);
JButton jb= new JButton("reset");
Box b= new Box(BoxLayout.X_AXIS);
// continued on next page //
10
// class MouseDemo continued from last page */
/** Constructor: an invisible JFrame with title t */
public MouseDemo(String t) {
super(t);
bl.add(b00); bl.add(b01);
br.add(b10); br.add(b11);
b.add(bl); b.add(br);
Container cp= getContentPane();
cp.add(b, BorderLayout.CENTER);
cp.add(jb, BorderLayout.SOUTH);
jb.addActionListener(new ButtonListener());
pack();
setResizable(false); // don’t let user resize!!!
}
// An inner class so that it can refer to fields b00, etc.
/** Respond to mouse click on button jb */
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
b00.clearDisk(); b01.clearDisk();
b10.clearDisk(); b11.clearDisk();
}
}
}
11