Transcript Slide 1

Lecture 19
Graphics User Interfaces
(GUIs)
java.sun
User Interface
• The way in which the user interacts with the program
• Separate “what” the program does from “how” the user interacts with it
– Television
• Function: Adjust volume, channels, color, etc
• Interface:
– Manual controls
– Universal Remote
– Automobile
• Function: Turn, accelerate, decelerate, stop
• Interface:
– Human driver
– Autonomous vehicle
– Project 3’s Forest
• Function: Add rocks, trees, weeds, grass, foxes, mice, rabbits
• Interface:
– Files
– Console
– Graphical
java.sun
Powerball Example
• Here’s an example using a class called PowerBallMachine. It is based on
the Powerball lottery where a person must correctly match 5 balls
selected from a collection of 55 balls.
• PowerballMachine has a single method called getOdds(int n, int k).
getOdds computes the chances of correctly selecting k balls from a
collection of size n.
• Let’s look at two interfaces for out PowerBallMachine. The first will be a
text based interface and the second with be a graphical interface.
import java.util.*;
public class PowerBallMachine {
public int getOdds(int n, int k) {
int lotteryOdds = 1;
for (int i = 1; i <=k; i++) {
lotteryOdds = lotteryOdds * (n-i+1)/i;
}
return lotteryOdds;
}
}
java.sun
Powerball- Console Interface
import java.util.*;
public class PowerBallMain {
public static void main(String args[]) {
PowerBallMachine machine = new PowerBallMachine();
Scanner input = new Scanner(System.in);
System.out.println("Enter number of balls to pick from: ");
int n = input.nextInt();
System.out.println("How many balls do you want to draw? ");
int k = input.nextInt();
System.out.println("Odds of winning are 1 in " + machine.getOdds(n, k));
}
}
java.sun
Powerball – Graphical User Interface
java.sun
Building Graphical User Interfaces
• Abstract Window Toolkit versus Swing
– AWT depend on native code (heavyweight)
• Faster than swing
• Not portable
– Swing is pure java (lightweight)
• Portable
• Not necessarily good for graphics
– Pros and Cons: see http://bdn1.borland.com/article/0,1410,26970,00.html
java.sun
Swing Components
Frames
Scroll Panes
Panels
Tabbed Panes
Sun’s Java Tutorial
Split Panes
java.sun
Swing Components
Progress Bars
Lists
Buttons
Combo Boxes
Labels
Text Boxes
Menus
Spinners
Sun’s Java Tutorial
Sliders
java.sun
Anatomy of a Frame
java.sun
Swing Components
Interact with your Program
PowerballMachine
getOdds(int n, int k)
java.sun
Event Handling
Graphical User Interface
Widget
Registered Listeners
Event
Object A
Object B
Object A
Object B
Object C
Object C
Event Listeners
or
Event Handlers
java.sun
Creating & Sending Events
• GUI components that can change state, generate “events” when a
change occurs
–
–
–
–
–
JButtons – ActionEvent
JTextField – ActionEvent
JLists – ListSelectionEvent
JCheckBox – ItemEvent
plus many, many more...
• All have a method which allows objects to register as an event listener
– addxxxListener(xxxListener x);
java.sun
Event Listeners/Handlers
•
•
Objects become event listeners by implementing the appropriate interface that will
allow them to register with a GUI component that generates events
Ex: JButton is a GUI component that generates events every time it is pressed
JButton b = new JButton(“Button #1”);
– Generate ActionEvents
– addActionListener(ActionListener a);
•
By virtue of being a JButton,
b generates ActionEvents
Any object implementing the ActionListener interface can register with the
JButton to be notified when the button changes state (is pressed)
public interface ActionListener {
void actionPerformed(ActionEvent e);
}
public class MyObject implements ActionListener {
...
public void actionPerformed(ActionEvent e) {
System.out.println(“Don’t press the button!”);
}
}
MyObject mo = new MyObject();
b.addActionListener(mo);
By implementing the
ActionListener
interface, MyObject
becomes an
ActionListener
Since mo is an ActionListener, it
can register with JButton b
java.sun
Button Example
• We’re going to start with a very simple example with a Frame that
contains two GUI components
– a button
– a label.
• When the button is pressed, a message is displayed at the console
java.sun
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuiTest extends JFrame {
private Container c;
public GuiTest(String t) {
super(t);
setSize(275, 170);
c = getContentPane();
c.setLayout(new FlowLayout());
JButton b = new JButton("I Believe");
b.addActionListener(new MyActionListener());
c.add(b);
JLabel lab = new JLabel("Push the button!");
c.add(lab);
setVisible(true);
addWindowListener(new MyWindowAdapter());
}
GuiTest extends JFrame
Compare this version to the previous.
Things to note:
 The explicit definition of a JFrame is
gone. GuiTest extends JFrame so it
is the JFrame.
 Most of what was in main is now in
the GuiTest constructor.
 The title for the frame is passed via
the constructor using super.
public static void main(String[] args) {
GuiTest g = new GuiTest("GuiTest Title");
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.out.println("Window closed...Exiting Program");
System.exit(0);
}
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Pressed");
}
}
java.sun
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
GuiTest: Inner Classes
public class GuiTest1 extends JFrame {
private Container c;
public GuiTest1(String t) {
super(t);
setSize(275, 170);
c = getContentPane();
c.setLayout(new FlowLayout());
JButton b = new JButton("I Believe");
b.addActionListener(new MyActionListener());
c.add(b);
JLabel lab = new JLabel("Push the button!");
c.add(lab);
setVisible(true);
addWindowListener(new MyWindowAdapter());
}
public static void main(String[] args) {
GuiTest1 g = new GuiTest1("GuiTest Title");
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.out.println("Window closed...Exiting Program");
System.exit(0);
}
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Pressed");
}
}
}
java.sun
GuiTest: Count the button clicks
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuiTest3 extends JFrame {
private Container c;
private int x = 0;
JLabel lab;
public GuiTest3(String t) {
super(t);
setSize(275, 170);
c = getContentPane();
c.setLayout(new FlowLayout());
JButton b = new JButton("I Believe");
b.addActionListener(new MyActionListener());
c.add(b);
lab = new JLabel("Push the button!");
c.add(lab);
setVisible(true);
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
x++;
if (x == 1) {
lab.setText("First Time!");
System.out.println("Button pressed for
the first time!");
}
else if (x < 10) {
lab.setText(x + " times.");
System.out.println("Button Pressed " +
x + " times.");
}
else {
lab.setText("Enough already...stop!");
System.out.println("Button Pressed " +
x + " times.");
}
}
}
public static void main(String[] args) {
GuiTest3 g = new GuiTest3("GuiTest Title");
g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
java.sun