Download presentation
Presentation is loading. Please wait.
1
CPSC150 Week 12 Graphical User Interfaces Chapter 11
2
CPSC150 Rest of GUIs JOptionPane example MenuBars
3
CPSC150 The JOptionPane Class Java comes with simple ready-made dialogs showConfirmDialog int result = JOptionPane.showConfirmDialog( frame, "These are your options", "confirm dialog", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE ); int result = JOptionPane.showConfirmDialog( frame, "These are your options", "confirm dialog", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE ); parent frame message option type (one of): DEFAULT_OPTION YES_NO_OPTION YES_NO_CANCEL_OPTION OK_CANCEL_OPTION option type (one of): DEFAULT_OPTION YES_NO_OPTION YES_NO_CANCEL_OPTION OK_CANCEL_OPTION title message type result value (one of): OK_OPTION YES_OPTION NO_OPTION CANCEL_OPTION CLOSE_OPTION result value (one of): OK_OPTION YES_OPTION NO_OPTION CANCEL_OPTION CLOSE_OPTION Ready-to-use Dialogs: showConfirmDialog
4
CPSC150 The JOptionPane Class Java comes with simple ready-made dialogs showOptionDialog String[] options = {"BMW","Mini","Mustang","Porsche"}; int result = JOptionPane.showOptionDialog( frame, "Choose a car:", "options dialog", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, new ImageIcon("car.gif"), options, options[1]); String[] options = {"BMW","Mini","Mustang","Porsche"}; int result = JOptionPane.showOptionDialog( frame, "Choose a car:", "options dialog", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, new ImageIcon("car.gif"), options, options[1]); parent frame option type title message result value (one of): 0…options.length-1 CLOSE_OPTION result value (one of): 0…options.length-1 CLOSE_OPTION message type icon options selection ( options) options one label per button options one label per button showOptionDialogs
5
CPSC150 Rest of GUIs Overview of Swing JOptionPane example MenuBars (from BlueJ book notes)
6
CPSC150 Frame (whole window) menu bar title bar
7
CPSC150 Adding menus JMenuBar –Displayed below the title. –Contains the menus. JMenu –e.g. File. Contains the menu items. JMenuItem –e.g. Open. Individual items.
8
CPSC150 private void makeMenuBar(JFrame frame) { JMenuBar menubar = new JMenuBar(); frame.setJMenuBar(menubar); // create the File menu JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); JMenuItem openItem = new JMenuItem("Open"); fileMenu.add(openItem); JMenuItem quitItem = new JMenuItem("Quit"); fileMenu.add(quitItem); }
9
CPSC150 Anonymous action listener JMenuItem openItem = new JMenuItem("Open"); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openFile(); } });
10
CPSC150 Anonymous class elements openItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { openFile(); } ); Anonymous object creation Actual parameter Class definition
11
CPSC150 Struts and Glue Invisible components used as spacing. Available from the Box class. Strut: fixed size. –Component createHorizontalStrut(int width) –Component createVerticalStrut(int height) Glue: fills available space. –Component createHorizontalGlue() –Component createVerticalGlue()
12
CPSC150 Pushing a menu to the right menu = new JMenu("File"); menubar.add(menu); menu = new JMenu("Filter"); menubar.add(menu); menubar.add(Box.createHorizontalGlue()); menu = new JMenu("Help"); menubar.add(menu); Glue (invisible)
13
CPSC150 Buttons and nested layouts Use JPanels and embedded layouts A GridLayout inside a FlowLayout inside a BorderLayout.
14
CPSC150 Borders Used to add decoration around components. Defined in javax.swing.border –BevelBorder, CompoundBorder, EmptyBorder, EtchedBorder, TitledBorder.
15
CPSC150 Adding spacing (for information only) JPanel contentPane = (JPanel)frame.getContentPane(); contentPane.setBorder(new EmptyBorder(6, 6, 6, 6)); // Specify the layout manager with nice spacing contentPane.setLayout(new BorderLayout(6, 6)); imagePanel = new ImagePanel(); imagePanel.setBorder(new EtchedBorder()); contentPane.add(imagePanel, BorderLayout.CENTER);
16
CPSC150 Other components (for information only) Slider Spinner Tabbed pane Scroll pane
17
CPSC150 Quiz 11 Adding JOptionPanes –Go to http://java.sun.com/j2se/1.4.2/docs/api/javax/s wing/package-summary.html http://java.sun.com/j2se/1.4.2/docs/api/javax/s wing/package-summary.html –Find JOptionPane and read it –Create the following 4 types of JOptionPanes a showConfirmDialog a ShowInputDialog (print the resulting message using System.out.println) a ShowMessageDialog a showOptionDialog
18
CPSC150 More (extra quiz credit) Add icons to the JOptionPane (you can find ones you like or use jpegs in your programming assignments) Make a JFrame with 5 buttons. Use different layout managers to arrange them. Add a JLabel to the bottom of your button window Loop up another component (e.g., JRadioButton) and implement it in your JFrame Look up borders and read the border tutorial at http://java.sun.com/docs/books/tutorial/uiswing/misc/bord er.html http://java.sun.com/docs/books/tutorial/uiswing/misc/bord er.html Add a border to your frame
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.