150 likes | 312 Views
Swing Refresher. David Merrill 5/14/2002 http://cs377a.stanford.edu/. What is Swing?. The Java Foundation Classes (JFC) API extends the original Abstract Window Toolkit (AWT) by adding a comprehensive set of graphical user interface class libraries. JFC/Swing components include:
E N D
Swing Refresher David Merrill 5/14/2002 http://cs377a.stanford.edu/ CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
What is Swing? • The Java Foundation Classes (JFC) API extends the original Abstract Window Toolkit (AWT) by adding a comprehensive set of graphical user interface class libraries. JFC/Swing components include: • Pluggable Look and Feel • Accessibility API • Java 2DTM API (Java 2 only) • Drag and Drop (Java 2 only) • AWT • Internationalization CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JFC/Swing GUI Components • Written in the Java programming language • Customizable look-and-feel • No reliance on the native windowing system • Incorporated in the Java 2 platform CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JFC/Swing GUI Components Top-Level Containers Applet Dialog Frame CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JFC/Swing GUI Components General-Purpose Containers Tabbed Pane Split Pane Scroll Pane Toolbar Panel CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JFC/Swing GUI Components Special-Purpose Containers Internal Frame Layered Pane Root Pane CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JFC/Swing GUI Components Basic Controls List Combobox Buttons Slider Menu Text Fields CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JFC/Swing GUI Components Uneditable Information Displays Progress Bar Label Tooltip CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JFC/Swing GUI Components Editable Displays of Formatted Information Tree Text Table Color Chooser File Chooser CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JFrame public static void main(String s[]) { JFrame frame = new JFrame("FrameDemo"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); //...create a blank label, set its preferred size... frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JTabbedPane ImageIcon icon = new ImageIcon("images/middle.gif"); JTabbedPane tabbedPane = new JTabbedPane(); Component panel1 = makeTextPanel("Blah"); //(returns a JPanel) tabbedPane.addTab("One", icon, panel1, "Does nothing"); tabbedPane.setSelectedIndex(0); Component panel2 = makeTextPanel("Blah blah"); tabbedPane.addTab("Two", icon, panel2, "Does twice as much nothing"); Component panel3 = makeTextPanel("Blah blah blah"); tabbedPane.addTab("Three", icon, panel3, "Still does nothing"); Component panel4 = makeTextPanel("Blah blah blah blah"); tabbedPane.addTab("Four", icon, panel4, "Does nothing at all"); CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JTabbedPane ImageIcon icon = new ImageIcon("images/middle.gif"); JTabbedPane tabbedPane = new JTabbedPane(); Component panel1 = makeTextPanel("Blah"); //(returns a JPanel) tabbedPane.addTab("One", icon, panel1, "Does nothing"); tabbedPane.setSelectedIndex(0); Component panel2 = makeTextPanel("Blah blah"); tabbedPane.addTab("Two", icon, panel2, "Does twice as much nothing"); Component panel3 = makeTextPanel("Blah blah blah"); tabbedPane.addTab("Three", icon, panel3, "Still does nothing"); Component panel4 = makeTextPanel("Blah blah blah blah"); tabbedPane.addTab("Four", icon, panel4, "Does nothing at all"); CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JGlassPane private void redispatchMouseEvent(MouseEvent e, boolean repaint) { // get the point from the MouseEvent int eventID = e.getID(); if (containerPoint.y < 0) { inMenuBar = true; //...set container and containerPoint accordingly... testForDrag(eventID); } component = SwingUtilities.getDeepestComponentAt( container, containerPoint.x, containerPoint.y); if (component.equals(liveButton)) { inButton = true; testForDrag(eventID); } if (inMenuBar || inButton || inDrag) { ...//Redispatch the event to component... } Etc… CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
JMenuBar //in the constructor for a JFrame subclass: JMenuBar menuBar; JMenu menu, submenu; JMenuItem menuItem; JCheckBoxMenuItem cbMenuItem; JRadioButtonMenuItem rbMenuItem; ... //Create the menu bar. menuBar = new JMenuBar(); setJMenuBar(menuBar); //Build the first menu. menu = new JMenu("A Menu"); menu.setMnemonic(KeyEvent.VK_A); menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items"); menuBar.add(menu); //a group of JMenuItems menuItem = new JMenuItem("A text-only menu item",KeyEvent.VK_T); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription(“This doesn't really do anything"); menu.add(menuItem); CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002
URL http://java.sun.com/docs/books/tutorial/uiswing/components/components.html CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002