1 / 17

Lecture 19 Graphics User Interfaces (GUIs)

Lecture 19 Graphics User Interfaces (GUIs). 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

oren-henry
Download Presentation

Lecture 19 Graphics User Interfaces (GUIs)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 19Graphics User Interfaces(GUIs)

  2. 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

  3. 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; } }

  4. 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)); } }

  5. Powerball – Graphical User Interface

  6. 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

  7. Swing Components Frames Scroll Panes Panels Tabbed Panes Split Panes Sun’s Java Tutorial

  8. Swing Components Progress Bars Lists Buttons Combo Boxes Labels Text Boxes Menus Spinners Sliders Sun’s Java Tutorial

  9. Anatomy of a Frame

  10. Swing ComponentsInteract with your Program PowerballMachine getOdds(int n, int k)

  11. Object A Object B Object C Event Handling Graphical User Interface Event Event Event Widget Registered Listeners Object A Object B Object C Event Listeners or Event Handlers

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

  13. By virtue of being a JButton, b generates ActionEvents Since mo is an ActionListener, it can register with JButton b 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); • 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

  14. 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

  15. 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. 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()); } 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"); } }

  16. GuiTest: Inner Classes import javax.swing.*; import java.awt.*; import java.awt.event.*; 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"); } } }

  17. GuiTest: Count the button clicks 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."); } } } 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); } public static void main(String[] args) { GuiTest3 g = new GuiTest3("GuiTest Title"); g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

More Related