Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces (Part IV)
Advertisements

User Interface Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
1 lecture 14 GUI PROGRAMMING IVLecture 15 Graphical User Interfaces (cont.) Overview  GUI Programming (cont.)  Example 1: Creating Multi-layout GUIs.
1 Chapter 23 Menus, Toolbars, Dialogs, and Internal Frames.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
1 lecture 12Lecture 15 Graphical User Interfaces (cont.) Overview  GUI Programming (cont.)  Example 1: Creating Multi-layout GUIs with Tooltips.  Example.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
Java Swing © Walter Milner 2005: Slide 1 Java Swing Walter Milner.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
Tables CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
JFrame and JPanel CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Java's Graphical User Interface Toolkit
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Layout Managers CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Computer Science 209 GUIs Model/View/Controller Layouts.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
GUI Components CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Advanced Java Class Events. change in state initiated by system or user java.util.EventObject java.awt.event java.swing.event.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Menus Pull-downs and popups. Tooltips –a ToolTip is a context-sensitive text string that is displayed in a popup window when the mouse rests over a particular.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Swing. Note - this presentation.. often needs to refer to source code which is too big to put on a slide So the source code is in a separate Word.
A First Look at GUI Applications
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Steps to Creating a GUI Interface
Advanced Programming in Java
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/19 ▪M▪Menus ▪T▪Toolbars ▪P▪Program

Menus Overview ▪Menus are extensively used in window applications ›Easy selection ›Hidden when not in use ›Usually logically organized ›Creates additional functionality when not otherwise known i.e. if you don’t know what Ctrl-X does, you may know what Cut does ▪Java contains a number of classes that help to support menus, with the top-level class being a JMenuBar USC CSCI 201L3/19 Menus

Creating Menus ▪Here are the steps for creating menus in Java ›Create a menu bar JMenuBar jmb = new JMenuBar(); ›Associate the menu bar with a frame this.setJMenuBar(jmb); ›Create a menu JMenu helpMenu = new JMenu(“Help”); ›Associate the menu with a menu bar jmb.add(helpMenu); ›Create a menu item JMenuItem aboutMenuItem = new JMenuItem(“About”); ›Associate the menu item with a menu helpMenu.add(aboutMenuItem); Note: Menus can be added as menu items, creating sub-menus ›Add an action to your menu item aboutMenuItem.addActionListener(…); ▪I would perform all of the associations after performing all of the other steps USC CSCI 201L4/19 Menus

Create GUI USC CSCI 201L5/25 Menus

Menu Example 1 import java.awt.FlowLayout; 2 import javax.swing.JButton; 3 import javax.swing.JFrame; 4 import javax.swing.JLabel; 5 import javax.swing.JMenu; 6 import javax.swing.JMenuBar; 7 import javax.swing.JMenuItem; 8 import javax.swing.JTextField; 9 10 public class Test extends JFrame { 11 public Test() { 12 super("Loan Calculator"); 13 setLayout(new FlowLayout(FlowLayout.LEFT)); 14 JLabel principal = new JLabel("Principal"); 15 JLabel apr = new JLabel("Annual Interest Rate"); 16 JButton jb = new JButton("Calculate"); 17 JTextField jtf = new JTextField("", 20); 18 JTextField jtf1 = new JTextField("", 20); 19 JLabel jl = new JLabel("Amount Paid: "); add(principal); 22 add(jtf); 23 add(apr); 24 add(jtf1); 25 add(jb); 26 add(jl); JMenuBar jmb = new JMenuBar(); 29 JMenu helpMenu = new JMenu("Help"); 30 JMenuItem aboutMenuItem = new JMenuItem("About"); 31 helpMenu.add(aboutMenuItem); 32 jmb.add(helpMenu); 33 setJMenuBar(jmb); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 36 setSize(250, 225); 37 setLocationRelativeTo(null); 38 setVisible(true); 39 } USC CSCI 201L6/19 Menus 40 public static void main(String args[]) { 41 Test t = new Test(); 42 } 43 }

Menu Example with ActionListener USC CSCI 201L7/19 Menus 25 aboutMenuItem.addActionListener(new ActionListener() { 26 public void actionPerformed(ActionEvent ae) { 27 JDialog jd = new JDialog(); 28 jd.setTitle("About"); 29 jd.setLocationRelativeTo(Test.this); 30 jd.setSize(150, 100); 31 jd.setModal(true); 32 JPanel jp = new JPanel(); 33 BoxLayout bl = new BoxLayout(jp, BoxLayout.Y_AXIS); 34 jp.setLayout(bl); 35 JLabel jl = new JLabel("Created for CSCI 201L"); 36 JLabel jl1 = new JLabel("Prof. Miller"); 37 jp.add(jl); 38 jp.add(jl1); 39 jd.add(jp); 40 jd.setVisible(true); 41 } 42 }); 43 helpMenu.add(aboutMenuItem); 44 jmb.add(helpMenu); 45 setJMenuBar(jmb); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 48 setSize(250, 225); 49 setLocationRelativeTo(null); 50 setVisible(true); 51 } public static void main(String args[]) { 54 Test t = new Test(); 55 } 56 } 1 import java.awt.*; // should import individually 2 import javax.swing.*; // should import individually 3 4 public class Test extends JFrame { 5 public Test() { 6 super("Loan Calculator"); 7 setLayout(new FlowLayout(FlowLayout.LEFT)); 8 JLabel principal = new JLabel("Principal"); 9 JLabel apr = new JLabel("Annual Interest Rate"); 10 JButton jb = new JButton("Calculate"); 11 JTextField jtf = new JTextField("", 20); 12 JTextField jtf1 = new JTextField("", 20); 13 JLabel jl = new JLabel("Amount Paid: "); add(principal); 16 add(jtf); 17 add(apr); 18 add(jtf1); 19 add(jb); 20 add(jl); JMenuBar jmb = new JMenuBar(); 23 JMenu helpMenu = new JMenu("Help"); 24 JMenuItem aboutMenuItem = new JMenuItem("About");

Menu Item Mnemonics ▪Mnemonics allow us to set shortcuts so the user doesn’t have to use the mouse to click on menus ›Mnemonics in Java appear as underscores on a letter ▪In Windows, if you press the Alt key within a program, you will see the mnemonics on the menus USC CSCI 201L8/19 Menus

Menu Example with Mnemonic USC CSCI 201L9/19 Menus 27 aboutMenuItem.addActionListener(new ActionListener() { 28 public void actionPerformed(ActionEvent ae) { 29 JDialog jd = new JDialog(); 30 jd.setTitle("About"); 31 jd.setLocationRelativeTo(Test.this); 32 jd.setSize(150, 100); 33 jd.setModal(true); 34 JPanel jp = new JPanel(); 35 BoxLayout bl = new BoxLayout(jp, BoxLayout.Y_AXIS); 36 jp.setLayout(bl); 37 JLabel jl = new JLabel("Created for CSCI 201L"); 38 JLabel jl1 = new JLabel("Prof. Miller"); 39 jp.add(jl); 40 jp.add(jl1); 41 jd.add(jp); 42 jd.setVisible(true); 43 } 44 }); 45 helpMenu.add(aboutMenuItem); 46 jmb.add(helpMenu); 47 setJMenuBar(jmb); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 50 setSize(250, 225); 51 setLocationRelativeTo(null); 52 setVisible(true); 53 } public static void main(String args[]) { 56 Test t = new Test(); 57 } 58 } 1 import java.awt.*; // should import individually 2 import javax.swing.*; // should import individually 3 4 public class Test extends JFrame { 5 public Test() { 6 super("Loan Calculator"); 7 setLayout(new FlowLayout(FlowLayout.LEFT)); 8 JLabel principal = new JLabel("Principal"); 9 JLabel apr = new JLabel("Annual Interest Rate"); 10 JButton jb = new JButton("Calculate"); 11 JTextField jtf = new JTextField("", 20); 12 JTextField jtf1 = new JTextField("", 20); 13 JLabel jl = new JLabel("Amount Paid: "); add(principal); 16 add(jtf); 17 add(apr); 18 add(jtf1); 19 add(jb); 20 add(jl); JMenuBar jmb = new JMenuBar(); 23 JMenu helpMenu = new JMenu("Help"); 24 helpMenu.setMnemonic(‘H’); 25 JMenuItem aboutMenuItem = new JMenuItem("About"); 26 aboutMenuItem.setMnemonic(‘A’);

Menu Item Icons ▪Icons appear to the left of the menu item ▪Icons are just for aesthetic appeal and don’t have a function outside of that ▪Relative paths in Eclipse will start from the project directory within your workspace USC CSCI 201L10/19 Menus

Menu Example with Mnemonic USC CSCI 201L11/19 Menus 29 aboutMenuItem.addActionListener(new ActionListener() { 30 public void actionPerformed(ActionEvent ae) { 31 JDialog jd = new JDialog(); 32 jd.setTitle("About"); 33 jd.setLocationRelativeTo(Test.this); 34 jd.setSize(150, 100); 35 jd.setModal(true); 36 JPanel jp = new JPanel(); 37 BoxLayout bl = new BoxLayout(jp, BoxLayout.Y_AXIS); 38 jp.setLayout(bl); 39 JLabel jl = new JLabel("Created for CSCI 201L"); 40 JLabel jl1 = new JLabel("Prof. Miller"); 41 jp.add(jl); 42 jp.add(jl1); 43 jd.add(jp); 44 jd.setVisible(true); 45 } 46 }); 47 helpMenu.add(aboutMenuItem); 48 jmb.add(helpMenu); 49 setJMenuBar(jmb); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 52 setSize(250, 225); 53 setLocationRelativeTo(null); 54 setVisible(true); 55 } public static void main(String args[]) { 58 Test t = new Test(); 59 } 60 } 1 import java.awt.*; // should import individually 2 import javax.swing.*; // should import individually 3 4 public class Test extends JFrame { 5 public Test() { 6 super("Loan Calculator"); 7 setLayout(new FlowLayout(FlowLayout.LEFT)); 8 JLabel principal = new JLabel("Principal"); 9 JLabel apr = new JLabel("Annual Interest Rate"); 10 JButton jb = new JButton("Calculate"); 11 JTextField jtf = new JTextField("", 20); 12 JTextField jtf1 = new JTextField("", 20); 13 JLabel jl = new JLabel("Amount Paid: "); add(principal); 16 add(jtf); 17 add(apr); 18 add(jtf1); 19 add(jb); 20 add(jl); JMenuBar jmb = new JMenuBar(); 23 JMenu helpMenu = new JMenu("Help"); 24 helpMenu.setMnemonic(‘H’); 25 JMenuItem aboutMenuItem = new JMenuItem("About"); 26 aboutMenuItem.setMnemonic(‘A’); 27 ImageIcon ii = new ImageIcon(“uscshield.png”); 28 aboutMenuItem.setIcon(ii);

Menu Item Key Accelerators ▪Key accelerators (shortcuts) allow users to type a sequence of characters to activate a specific menu item’s action ▪This usually includes using Ctrl, Shift, Alt, the Function keys, or a combination of them ›Key accelerators appear to the right of menu items in a different color typically ▪Operating system accelerators will still function, such as Ctrl-X, Ctrl-C, and based on the component, you may not be able to override that USC CSCI 201L12/19 Menus

Menu Example with Accelerator USC CSCI 201L13/19 Menus 30 aboutMenuItem.addActionListener(new ActionListener() { 31 public void actionPerformed(ActionEvent ae) { 32 JDialog jd = new JDialog(); 33 jd.setTitle("About"); 34 jd.setLocationRelativeTo(Test.this); 35 jd.setSize(150, 100); 36 jd.setModal(true); 37 JPanel jp = new JPanel(); 38 BoxLayout bl = new BoxLayout(jp, BoxLayout.Y_AXIS); 39 jp.setLayout(bl); 40 JLabel jl = new JLabel("Created for CSCI 201L"); 41 JLabel jl1 = new JLabel("Prof. Miller"); 42 jp.add(jl); 43 jp.add(jl1); 44 jd.add(jp); 45 jd.setVisible(true); 46 } 47 }); 48 helpMenu.add(aboutMenuItem); 49 jmb.add(helpMenu); 50 setJMenuBar(jmb); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 53 setSize(250, 225); 54 setLocationRelativeTo(null); 55 setVisible(true); 56 } public static void main(String args[]) { 59 Test t = new Test(); 60 } 61 } 1 import java.awt.*; // should import individually 2 import javax.swing.*; // should import individually 3 4 public class Test extends JFrame { 5 public Test() { 6 super("Loan Calculator"); 7 setLayout(new FlowLayout(FlowLayout.LEFT)); 8 JLabel principal = new JLabel("Principal"); 9 JLabel apr = new JLabel("Annual Interest Rate"); 10 JButton jb = new JButton("Calculate"); 11 JTextField jtf = new JTextField("", 20); 12 JTextField jtf1 = new JTextField("", 20); 13 JLabel jl = new JLabel("Amount Paid: "); add(principal); 16 add(jtf); 17 add(apr); 18 add(jtf1); 19 add(jb); 20 add(jl); JMenuBar jmb = new JMenuBar(); 23 JMenu helpMenu = new JMenu("Help"); 24 helpMenu.setMnemonic(‘H’); 25 JMenuItem aboutMenuItem = new JMenuItem("About"); 26 aboutMenuItem.setMnemonic(‘A’); 27 ImageIcon ii = new ImageIcon(“uscshield.png”); 28 aboutMenuItem.setIcon(ii); 29 aboutMenuItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK));

Popup Menus ▪A menu bar is associated with a frame ▪A popup menu is associated with a component ›When you click the popup trigger (such as the right mouse button in Windows), the popup menu will be displayed ›Everything we can do with menu items and a menu bar, we can do with popup menu items and a popup menu ▪Steps for creating a popup menu ›Create an instance of JPopupMenu JPopupMenu jpm = new JPopupMenu(); ›Add menu items to the popup menu jpm.add(new JMenuItem(“New”)); ›Specify the parent component and the location at which to display the popup menu jpm.show(component, x, y); USC CSCI 201L14/19 Menus

Menu Example with Popup Menu USC CSCI 201L15/19 Menus 47 helpMenu.add(aboutMenuItem); 48 jmb.add(helpMenu); 49 setJMenuBar(jmb); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 52 setSize(250, 225); 53 setLocationRelativeTo(null); 54 setVisible(true); 55 jpm = new JPopupMenu(); 56 jpm.add(new JMenuItem("Clear")); 57 jtf.addMouseListener(new MouseAdapter() { 58 public void mousePressed(MouseEvent me) { 59 System.out.println("mouseevent: " + me.isPopupTrigger()); 60 showPopupMenu(me); 61 } 62 }); 63 } private void showPopupMenu(MouseEvent me) { 66 if (me.getButton() == MouseEvent.BUTTON3) { 67 jpm.show(me.getComponent(), me.getX(), me.getY()); 68 } 69 } public static void main(String args[]) { 72 Test t = new Test(); 73 } 74 } 1 import java.awt.*; // should import individually 2 import javax.swing.*; // should import individually 3 4 public class Test extends JFrame { 5 JPopupMenu jpm; 6 public Test() { 7 super("Loan Calculator"); 8 setLayout(new FlowLayout(FlowLayout.LEFT)); 9 JLabel principal = new JLabel("Principal"); 10 JLabel apr = new JLabel("Annual Interest Rate"); 11 JButton jb = new JButton("Calculate"); 12 JTextField jtf = new JTextField("", 20); 13 JTextField jtf1 = new JTextField("", 20); 14 JLabel jl = new JLabel("Amount Paid: "); 15 add(principal); 16 add(jtf); 17 add(apr); 18 add(jtf1); 19 add(jb); 20 add(jl); 21 JMenuBar jmb = new JMenuBar(); 22 JMenu helpMenu = new JMenu("Help"); 23 helpMenu.setMnemonic(‘H’); 24 JMenuItem aboutMenuItem = new JMenuItem("About"); 25 aboutMenuItem.setMnemonic(‘A’); 26 ImageIcon ii = new ImageIcon(“uscshield.png”); 27 aboutMenuItem.setIcon(ii); 28 aboutMenuItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK)); 29 aboutMenuItem.addActionListener(new ActionListener() { 30 public void actionPerformed(ActionEvent ae) { 31 JDialog jd = new JDialog(); 32 jd.setTitle("About"); 33 jd.setLocationRelativeTo(Test.this); 34 jd.setSize(150, 100); 35 jd.setModal(true); 36 JPanel jp = new JPanel(); 37 BoxLayout bl = new BoxLayout(jp, BoxLayout.Y_AXIS); 38 jp.setLayout(bl); 39 JLabel jl = new JLabel("Created for CSCI 201L"); 40 JLabel jl1 = new JLabel("Prof. Miller"); 41 jp.add(jl); 42 jp.add(jl1); 43 jd.add(jp); 44 jd.setVisible(true); 45 } 46 });

Outline USC CSCI 201L16/19 ▪M▪Menus ▪T▪Toolbars ▪O▪Option Panes ▪P▪Program

Toolbars ▪Toolbars are used in applications to hold frequently used commands ›They often correspond to a menu item, so there are not typically additional actions that must be created ▪In Swing, there is a JToolBar class ›The default layout manager of a JToolBar is BoxLayout ▪Icons cannot be placed into the JToolBar directly, so JButtons must be created with images added to them ▪Toolbars can be floatable or stationary USC CSCI 201L17/19 Toolbars

Menu Example with Toolbar USC CSCI 201L18/19 Menus 39 JMenuItem aboutMenuItem = new JMenuItem("About"); 40 aboutMenuItem.setMnemonic(‘A’); 41 ImageIcon ii = new IMageIcon(“uscshield.png”); 42 aboutMenuItem.setIcon(ii); 43 aboutMenuItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK)); 44 aboutMenuItem.addActionListener(new ActionListener() { 45 public void actionPerformed(ActionEvent ae) { 46 JDialog jd = new JDialog(); 47 jd.setTitle("About"); 48 jd.setLocationRelativeTo(Test.this); 49 jd.setSize(150, 100); 50 jd.setModal(true); 51 JPanel jp = new JPanel(); 52 BoxLayout bl = new BoxLayout(jp, BoxLayout.Y_AXIS); 53 jp.setLayout(bl); 54 JLabel jl = new JLabel("Created for CSCI 201L"); 55 JLabel jl1 = new JLabel("Prof. Miller"); 56 jp.add(jl); 57 jp.add(jl1); 58 jd.add(jp); 59 jd.setVisible(true); 60 } 61 }); 62 helpMenu.add(aboutMenuItem); 63 jmb.add(helpMenu); 64 setJMenuBar(jmb); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 67 setSize(250, 225); 68 setLocationRelativeTo(null); 69 setVisible(true); 70 jpm = new JPopupMenu(); 71 jpm.add(new JMenuItem("Clear")); 72 jtf.addMouseListener(new MouseAdapter() { 73 public void mousePressed(MouseEvent me) { 74 System.out.println("mouseevent: " + me.isPopupTrigger()); 75 showPopupMenu(me); 76 } 77 }); 78 } private void showPopupMenu(MouseEvent me) { 81 if (me.getButton() == MouseEvent.BUTTON3) { 82 jpm.show(me.getComponent(), me.getX(), me.getY()); 83 } 84 } } public static void main(String args[]) { 89 Test t = new Test(); 90 } 91 } 1 import java.awt.*; // should import individually 2 import javax.swing.*; // should import individually 3 4 public class Test extends JFrame { 5 JPopupMenu jpm; 6 public Test() { 7 super("Loan Calculator"); 8 JLabel principal = new JLabel("Principal"); 9 JLabel apr = new JLabel("Annual Interest Rate"); 10 JButton jb = new JButton("Calculate"); 11 JTextField jtf = new JTextField("", 20); 12 JTextField jtf1 = new JTextField("", 20); 13 JLabel jl = new JLabel("Amount Paid: "); JButton uscButton = new JButton(new ImageIcon("uscshield.png")); 16 JButton uscFacebook = new JButton(new ImageIcon("uscfacebook.png")); 17 uscButton.setToolTipText("USC"); 18 uscButton.setBorderPainted(false); 19 uscFacebook.setToolTipText("Facebook"); 20 uscFacebook.setBorderPainted(false); 21 JToolBar jtb = new JToolBar(); 22 jtb.setFloatable(false); 23 jtb.add(uscButton); 24 jtb.add(uscFacebook); 25 add(jtb, BorderLayout.NORTH); JPanel jp = new JPanel(); 28 jp.setLayout(new FlowLayout(FlowLayout.LEFT)); 29 jp.add(principal); 30 jp.add(jtf); 31 jp.add(apr); 32 jp.add(jtf1); 33 jp.add(jb); 34 jp.add(jl); 35 add(jp, BorderLayout.CENTER); 36 JMenuBar jmb = new JMenuBar(); 37 JMenu helpMenu = new JMenu("Help"); 38 helpMenu.setMnemonic(‘H’);

Outline USC CSCI 201L19/19 ▪M▪Menus ▪T▪Toolbars ▪P▪Program

Program ▪Create the following GUI, modified from the ConcatenateGUI. When the USC button is clicked, perform the same action as the Concatenate button. When the Slideshow button is clicked, concatenate the text in the opposite order. In the Help menu, there is an About menu item that will display the dialog box on the right. USC CSCI 201L20/19 Program