Session 7a GUI Detailed - Building Java Programs Graphical User Interfaces 1 Chapter outline GUI basics Laying out components FlowLayout GridLayout

Session 7a GUI Detailed - Building Java Programs...

This preview shows page 1 - 12 out of 107 pages.

1 Building Java Building Java Programs Programs Graphical User Interfaces
2 Chapter outline GUI basics Graphical input and output with JOptionPane Frames, buttons, labels, and text fields Changing a frame's layout Handling an action event Laying out components FlowLayout, GridLayout, and BorderLayout Additional components and events Keyboard and mouse events 2D graphics
3 Graphical input and Graphical input and output with output with JOptionPane JOptionPane
4 An option pane is a simple dialog box for graphical input/output advantages: simple flexible (in some ways) looks better than the black box of death disadvantages: created with static methods; not very object-oriented not very powerful (just simple dialog boxes) JOptionPane
5 Types of JOptionPane s showMessageDialog( <parent> , <message> ) Displays a message on a dialog with an OK button. showConfirmDialog( <parent> , <message> ) Displays a message and list of choices Yes, No, Cancel; returns user's choice as an int with one of the following values: JOptionPane.YES_OPTION JOptionPane.NO_OPTION JOptionPane.CANCEL_OPTION showInputDialog( <parent> , <message> ) Displays a message and text field for input; returns the user's value entered as a String . can pass null for the parent to all methods
6 JOptionPane examples 1 showMessageDialog analogous to System.out.println for displaying a simple message import javax.swing.*; public class MessageDialogExample { public static void main(String[] args) { JOptionPane.showMessageDialog (null,"How's the weather?"); JOptionPane.showMessageDialog (null, "Second message"); } }
7 JOptionPane examples 2 showConfirmDialog analogous to a System.out.print that 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."); } } }
8 JOptionPane examples 3 showInputDialog analogous to a System.out.print that 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); } }
9 Onscreen GUI elements Most 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 in a frame. containers : Logical groups of components.
10 JFrame example 1 A 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:
11 JFrame example 2 A program that sets several properties of the JFrame : import java.awt.*; import javax.swing.*;

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture