BIT205-HO6-GUI_3b050936dab8b95dfbf79d428a3b083b.ppt -...

This preview shows page 1 - 11 out of 48 pages.

Graphical User InterfacesObject Oriented ProgrammingBIT205Handout # 6All handouts are available and accessible as soft copies at the followingaddress: Covers Course Learning Outcome(s): 5-Use GUIobjects to provide user friendly interface.6.Use controlsin creating forms
Graphical input and output with JOptionPane
3An option pane is a simple dialog box for graphical input/outputadvantages:simpleflexible (in some ways)looks better than the black box of deathdisadvantages:created with static methods; not very object-orientednot very powerful (just simple dialog boxes)JOptionPane
4Types of JOptionPanesshowMessageDialog(<parent>, <message>)Displays a message on a dialogwith an OK button.showConfirmDialog(<parent>, <message>)Displays a message and list ofchoices Yes, No, Cancel;returns user's choice as an intwith one of the following values:JOptionPane.YES_OPTIONJOptionPane.NO_OPTIONJOptionPane.CANCEL_OPTIONshowInputDialog(<parent>, <message>)Displays a message and textfield for input; returns the user'svalue entered as a String.can pass nullfor the parent to all methods
5JOptionPaneexamples 1showMessageDialoganalogous to System.out.printlnto display a messageimport javax.swing.*;public class MessageDialogExample {public static void main(String[] args) {JOptionPane.showMessageDialog(null,"How's the weather?");JOptionPane.showMessageDialog(null, "Second message");}}
6JOptionPaneexamples 2showConfirmDialoganalogous to a System.out.printthat prints a question, then reading an input value from the user (can only be one of the provided choices)import javax.swing.*;public class ConfirmDialogExample {public static void main(String[] args) {int choice = JOptionPane.showConfirmDialog(null, "Erase your hard disk?");if (choice == JOptionPane.YES_OPTION) {JOptionPane.showMessageDialog(null, "Disk erased!");} else {JOptionPane.showMessageDialog(null, "Cancelled.");}}}
7JOptionPaneexamples 3showInputDialoganalogous to a System.out.printthat prints a question, then reading an input value from the user (can be any value)import javax.swing.*;public class InputDialogExample {public static void main(String[] args) {String name = JOptionPane.showInputDialog(null,"What's yer name, pardner?");JOptionPane.showMessageDialog(null, "Yeehaw, " + name);}}
8Onscreen GUI elementsMost GUIs are not composed of option panes; they are too limited. Instead, complex GUIs contain the following elements:frame: A graphical window on the screen.components: GUI widgets such as buttons or text fields.containers: Logical groups of components.
9JFrame example 1A simple program that creates and shows a JFrame:import javax.swing.*;public class SimpleFrame {public static void main(String[] args) {JFrame frame = new JFrame();frame.setVisible(true);}}Graphical output:
10JFrame example 2import java.awt.*;import javax.swing.*;public class SimpleFrame2 {public static void main(String[] args) {JFrame frame = new JFrame();frame.setForeground(Color.WHITE);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setLocation(new Point(10, 50));frame.setSize(new Dimension(300, 120));frame.setTitle("A frame");frame.setVisible(true);}}Graphical output:

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture