Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L."— Presentation transcript:

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

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

3 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

4 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

5 Create GUI USC CSCI 201L5/25 Menus

6 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: "); 20 21 add(principal); 22 add(jtf); 23 add(apr); 24 add(jtf1); 25 add(jb); 26 add(jl); 27 28 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); 34 35 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 }

7 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); 46 47 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 48 setSize(250, 225); 49 setLocationRelativeTo(null); 50 setVisible(true); 51 } 52 53 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: "); 14 15 add(principal); 16 add(jtf); 17 add(apr); 18 add(jtf1); 19 add(jb); 20 add(jl); 21 22 JMenuBar jmb = new JMenuBar(); 23 JMenu helpMenu = new JMenu("Help"); 24 JMenuItem aboutMenuItem = new JMenuItem("About");

8 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

9 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); 48 49 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 50 setSize(250, 225); 51 setLocationRelativeTo(null); 52 setVisible(true); 53 } 54 55 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: "); 14 15 add(principal); 16 add(jtf); 17 add(apr); 18 add(jtf1); 19 add(jb); 20 add(jl); 21 22 JMenuBar jmb = new JMenuBar(); 23 JMenu helpMenu = new JMenu("Help"); 24 helpMenu.setMnemonic(‘H’); 25 JMenuItem aboutMenuItem = new JMenuItem("About"); 26 aboutMenuItem.setMnemonic(‘A’);

10 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

11 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); 50 51 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 52 setSize(250, 225); 53 setLocationRelativeTo(null); 54 setVisible(true); 55 } 56 57 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: "); 14 15 add(principal); 16 add(jtf); 17 add(apr); 18 add(jtf1); 19 add(jb); 20 add(jl); 21 22 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);

12 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

13 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); 51 52 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 53 setSize(250, 225); 54 setLocationRelativeTo(null); 55 setVisible(true); 56 } 57 58 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: "); 14 15 add(principal); 16 add(jtf); 17 add(apr); 18 add(jtf1); 19 add(jb); 20 add(jl); 21 22 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));

14 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

15 Menu Example with Popup Menu USC CSCI 201L15/19 Menus 47 helpMenu.add(aboutMenuItem); 48 jmb.add(helpMenu); 49 setJMenuBar(jmb); 50 51 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 } 64 65 private void showPopupMenu(MouseEvent me) { 66 if (me.getButton() == MouseEvent.BUTTON3) { 67 jpm.show(me.getComponent(), me.getX(), me.getY()); 68 } 69 } 70 71 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 });

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

17 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

18 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); 65 66 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 } 79 80 private void showPopupMenu(MouseEvent me) { 81 if (me.getButton() == MouseEvent.BUTTON3) { 82 jpm.show(me.getComponent(), me.getX(), me.getY()); 83 } 84 } 85 86 } 87 88 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: "); 14 15 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); 26 27 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’);

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

20 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


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

Similar presentations


Ads by Google