1 / 15

GUI Programming II: Components

23. GUI Programming II: Components. Previously. Hello World as GUI Layout Design Criteria In GUI Human Interface Concepts Requirements Use Cases Design GUI Java Foundation Classes. Add record View record Edit record Save record Delete record. Database. User. actor. actor.

Télécharger la présentation

GUI Programming II: 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. 23 GUI Programming II: Components

  2. Previously • Hello World as GUI • Layout Design • Criteria In GUI • Human Interface Concepts • Requirements • Use Cases • Design GUI • Java Foundation Classes Add record View record Edit record Save record Delete record Database User actor actor

  3. Overview • Hierarchy • Overview Swing Components • Swing Components

  4. Hierarchy java.lang Object java.awt Component Abstract Window Toolkit (AWT) Container javax.swing JComponent JCheckBox JMenuBar JTextField JLabel JComboBox JTextArea JTable JPanel JButton

  5. Swing Components • Position  Layout Managers • Look  how the control is displayed on the screen • Feel - base functionality • Do you click on them. e.g. button (JButton) • Do you type on them, e.g. text field (JTextField) ... • Functionality Events • What happen when the corresponding action has been executed by the user, e.g. clicking on the "Save" button save the data

  6. Overview Swing Components • JPanel • JScrollBar • JSlider • JSplitPane • JButton • JCheckBox • JComboBox • JRadioButton • JPopupMenu Divider between panels can be drag left and right

  7. Swing Components • JTree • JLabel • Fix text on the display • JDialog • JTabel SubSuperClasses • JMenuBar • JMenu • JMenuItem • JTextField • JTabbedPane • JTextArea bin doc readme.txt javadoc src Application title Data was saved. Tab 2 Tab 3 Tab 1 Panel

  8. Swing Components importjavax.swing.JFrame; importjavax.swing.JLabel; public class HelloWorldGUIextendsJFrame { public static void main(String[] args) { newHelloWorldGUI(); } // main() HelloWorldGUI() { super("Hello World"); // the title JLabeljlbHelloWorld = newJLabel(" Hello World!"); add(jlbHelloWorld); // add label to the GUI setSize(150, 80); // size of the window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); // show the GUI } // Constructor () } // end class HelloWorldGUI

  9. Swing Components • Extend JFrame to get the general look of a GUI • JLabel corresponds to fix text to add to the GUI public class HelloWorldGUIextendsJFrame { ... HelloWorldGUI() { super("Hello World"); // the title JLabeljlbHelloWorld = newJLabel(" Hello World!"); add(jlbHelloWorld); // add label to the GUI setSize(150, 80); // size of the window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); // show the GUI } // Constructor () } // end class HelloWorldGUI height Hello World width

  10. Swing Components • Creating and Showing Simple Dialogs • It is a accomplished by using the class JDialog • Different ones depending of the message to display • Default title and icon JOptionPane.showMessageDialog(frame, "Data was saved.", "Message"); • Custom title, warning icon JOptionPane.showMessageDialog(frame, “Data was saved.", "Warning!", JOptionPane.WARNING_MESSAGE); • Custom title, error icon JOptionPane.showMessageDialog(frame, "Data wasn’t saved.", “Error!", JOptionPane.ERROR_MESSAGE); Data was saved. Warning! Data was saved. Error! Data wasn’t saved.

  11. Swing Components • Yes/No dialog with default icon, custom title int iAnswer = JOptionPane.showConfirmDialog(frame, "Would you like to save the data?", "Question!", JOptionPane.YES_NO_OPTION); Valid return values • JOptionPane.YES_OPTION: user has selected 'YES' • JOptionPane.NO_OPTION: user has selected 'NO' • JOptionPane.CLOSED_OPTION: user closed the dialog window explicitly, rather than by choosing a button inside the option pane Question! Would you like to save the data?

  12. Swing Components • JMenuBar and JMenuItem //Create the menu bar JMenuBarmenuBar = new JMenuBar(); //Build the first menu JMenu menu = newJMenu("1st Menu"); menuBar.add(menu); // add menu to menu bar // Add menu items JMenuItemmenuItem = newJMenuItem("1st Option"); menuItem.addItemListener(this); // register the listener menu.add(menuItem);// add item to a container menuItem = newJMenuItem("2nd Option"); menuItem.addItemListener(this); // register the listener menu.add(menuItem); // add item to a container ... jFrame.setJMenuBar(menuBar); // set menu bar • JMenuBar • add(JMenu) • JMenu • add(JMenuItem) • add(JMenu) • JMenuItem • addItemListener(ItemListener) • JFrame • setJMenuBar(JMenuBar)

  13. Swing Components • JTextField • JButton JTextFieldjText = newJTextField(15); ... add(jText); // add to a container ... jText.getText(); // get text on the text fields JButtonjButton = newJButton("OK"); jButton.setActionCommand ("OK"); // assign an action identifier jButton.addActionListerner(this); // register the listener add(jButton); // add to a container ...

  14. Swing Components • JTable // Header String[] astrColumnNames = {"First name", "Last name", "Num", "Road", "Alive"}; Object[][] aoRows = { {"Debby", "Smith", new Integer(5), "Parliament", new Boolean(false)}, {"John", "Rainbow", new Integer(3), "GreatAv.", new Boolean(true)}, {"Jason","Wood", new Integer(2), "Lower line", new Boolean(false)}, {"Jane", "White", new Integer(13),"High Street", new Boolean(true)} }; // Build table using these roes and column names JTable table = newJTable(aoRows, astrColumnNames);

  15. References A Brief Introduction to the Swing Package http://download.oracle.com/javase/tutorial/ui/overview/index.html Using Swing Components http://download.oracle.com/javase/tutorial/uiswing/components/index.html Java API http://download.oracle.com/javase/6/docs/api/ Course Resources Website http://workspace.nottingham.ac.uk/display/G54ICP/Resources Java look and feel Graphics Repository http://java.sun.com/developer/techDocs/hi/repository/

More Related