150 likes | 306 Vues
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.
E N D
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
Overview • Hierarchy • Overview Swing Components • Swing Components
Hierarchy java.lang Object java.awt Component Abstract Window Toolkit (AWT) Container javax.swing JComponent JCheckBox JMenuBar JTextField JLabel JComboBox JTextArea JTable JPanel JButton
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
Overview Swing Components • JPanel • JScrollBar • JSlider • JSplitPane • JButton • JCheckBox • JComboBox • JRadioButton • JPopupMenu Divider between panels can be drag left and right
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
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
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
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.
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?
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)
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 ...
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);
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/