1 / 15

CS202 Java Object Oriented Programming GUI Programming – More GUI Components

CS202 Java Object Oriented Programming GUI Programming – More GUI Components. Chengyu Sun California State University, Los Angeles. Important stuff Menu bar, menu, and menu items Toolbar Scroll Pane Dialog Box FileChooser. Optional stuff Tabbed Pane Popup Menu ChangeListener

lcraig
Télécharger la présentation

CS202 Java Object Oriented Programming GUI Programming – More GUI Components

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS202 Java Object Oriented ProgrammingGUI Programming – More GUI Components Chengyu Sun California State University, Los Angeles

  2. Important stuff Menu bar, menu, and menu items Toolbar Scroll Pane Dialog Box FileChooser Optional stuff Tabbed Pane Popup Menu ChangeListener KeyListener … JEdit – A Simple Text Editor

  3. Swing Components HOWTO • http://java.sun.com/docs/books/tutorial/uiswing/components/componentlist.html

  4. Top-level Container Layout Content Pane Title Menu

  5. Menu Bar JMenuBar() add( JMenu ) Menu JMenu( String ) add( JMenuItem ) Menu Bar and Menu JMenuBar mb = new JMenuBar(); JMenu fileMenu = new JMenu (“File”); mb.add( fileMenu ); setMenuBar( mb ); // add a menu bar fileMenu.setMnemonic( KeyEvent.VK_F ); // add a hotkey

  6. Menu Items • JMenuItem( String ) • JMenuItem( String, int ) JMenuItem miOpen = new JMenuItem( “Open...”, KeyEvent.VK_O ); JMenuItem miSave = new JMenuItem( “Save...”, KeyEvent.VK_S ); JMenuItem miExit = new JMenuItem( “Exit” ); fileMenu.add( miOpen ); // add a menu item fileMenu.add( miSave ); fileMenu.addSeparator(); // add a menu separator fileMenu.add( miExit );

  7. Tool Bar and Tool Tip • JToolBar( String title ) • setToolTipText( String ) in JComponent JToolBar tb = new JToolBar( “Standard” ); Container cp = getContentPane(); cp.add( tb, BorderLayout.NORTH ); JButton ob = new JButton( "o" ); ob.setTipTipText( “Open File” );

  8. Scroll Pane • Provides a scrollable view of a component • JScrollPane( Component ) Container cp = getContentPane(); JTextArea ta = new JTextArea(); cp.add( (new JScrollPane(ta)), BorderLayout.CENTER );

  9. File Chooser • JFileChooser() • showDialog( Component, String ) • showOpenDialog( Component ) • showSaveDailog( Component ) • getSelectedFile() JFileChooser fc = new JFileChooser(); int status = fc.showOpenDialog( this ); if( status == JFileChooser.APPROVE_OPTION ) { File f = fc.getSelectedFile(); }

  10. Dialog Boxes • JColorChooser, JFileChooser • JDialog for complex and/or non-modal dialog boxes • A dialog is modal if it blocks the input to all other windows of the program • JOptionPane for simple dialog boxes

  11. JOptionPane • showConfirmDialog() – ask a confirming question, like yes/no/cancel • showInputDialog() – prompt for input • showMessageDialog() – display a message • showOptionDialog() – all of above icon mesasge input buttons

  12. Tabbed Pane • JTabbedPane() • addTab( String, Component ) • getSelectedIndex() • addChangeListener( ChangeListener ) JTabbedPane tp = new JTabbedPane(); JTextArea ta1 = new JTextArea(); JTextArea ta2 = new JTextArea(); tp.addTab( “file1”, new JScrollPane(ta1) ); tp.addTab( “file2”, new JScrollPane(ta2) ); getContentPane().add( tp, BorderLayout.CENTER );

  13. Popup Menu • JPopupMenu() • add( JMenuItem ) • addSeparator() • show( Component invoker, int x, int y )

  14. Bring up a Popup Menu • Register a MouseListener on the component • Check isPopupTrigger() of the mouse event • In mousePressed() and mouseReleased() • But not in mouseClicked() • Example: http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#popup

  15. Use of a GUI Builder • Netbeans • JBuilder • Eclipse • Visual Editor • Help  Software Updates  Find and Install…

More Related