Computer Science Notes Chapter 13 Page 1 of 7 Chapter 13: GUI Basics These notes are meant to accompany Introduction to Java Programming: Brief Version, seventh edition by Y. Daniel Lang. Book’s Statement of Skills: 1. To distinguish between Swing and AWT. (13.2) 2. To describe the Java GUI API hierarchy. (13.3) 3. To create user interfaces using frames, panels, and simple GUI components. (13.4) 4. To understand the role of layout managers. (13.5) 5. To use the FlowLayout, GridLayout, and BorderLayout managers to lay out components in a container. (13.5) 6. To specify colors and fonts using the Color and Font classes. (13.6 – 13.7) 7. To use JPanel as a subcontainer. (13.8) 8. To apply common features such as borders, tool tips, fonts, and colors on Swing components. (13.9) 9. To use borders to visually group user-interface components. (13.9) 10. To create image icons using the ImageIcon class. (13.10) Section 13.1: Introduction This chapter presents the framework of the Java GUI API and its components so you can create more userfriendly applications. Section 13.2: Swing vs. AWT Original Java GUI classes were in a package known as the Abstract Windowing Toolkit (java.awt) o The AWT used platform-dependent components o prone to platform-specific bugs “Swing” components in the javax.swing package were subsequently developed to be more robust, versatile, and flexible. o Swing GUI components start with a J to distinguish them from AWT components. Components that don’t rely on the native GUI are called “lightweight components.” Components that do rely on the native GUI are called “heavytweight components.” Section 13.3: The Java GUI API GUI classes belong to three groups: o Container classes (like JFrame, JPanel, and JApplet)are used to contain other components o Component classes (like JButton, JTextField, JTextArea, JComboBox, JList, JRadioButton, and JMenu) are subclasses of JComponent and comprise the functioning components of a GUI o Helper classes (like Graphics, Color, Font, FontMetrics, and dimension) are used to support GUI components Computer Science Notes Chapter 13 Page 2 of 7 Computer Science Notes Chapter 13 Page 3 of 7 Section 13.3.1: Swing GUI Components Component is the abstract super class of all user interface classes Container is a concrete subclass of Component, and has an aggregate of many Component objects within it. JComponent is an abstract subclass of Container that is the super class for all Swing components. Section 13.3.2: Container Classes See image on next page Section 13.3.3: GUI Helper Classes See image on next page Computer Science Notes Chapter 13 Page 4 of 7 Computer Science Notes Chapter 13 Page 5 of 7 Section 13.4: Frames To create a GUI, you need to create either a frame or an applet to contain the components. Section 13.4.1: Creating a Frame Use the JFrame class ( http://java.sun.com/javase/6/docs/api/javax/swing/JFrame.html ). See www.cs.armstrong.edu/liang/intro7e/book/MyFrame.java Section 13.4.2: Adding Components to a Frame Use the add method to add a component See www.cs.armstrong.edu/liang/intro7e/book/MyFrameWithComponents.java Use the remove method to remove a component Section 13.5: Layout Managers Layout managers are classes that control how components are added to a frame. Some examples are: o FlowLayout o GridLayout o BorderLayout o GridBagLayout All layout managers implement the LayoutManager interface. Layout managers are set in a container using the setLayoutManager() method. Section 13.5.1: FlowLayout Components are added from left to right in the order they are added, starting at the top row and going down from there. See www.cs.armstrong.edu/liang/intro7e/book/ShowFlowLayout.java Notice in that example that the ShowFlowLayout class both extends JFrame and has a main method which creates an instance of the ShowFlowLayout class. This is the preferred style for creating GUI applications. Section 13.5.2: GridLayout Components are added to a grid whose number of rows and columns is specified in the constructor (as well as any desired horizontal and vertical gap). See www.cs.armstrong.edu/liang/intro7e/book/ShowGridLayout.java Section 13.5.3: BorderLayout Components are added to one of five regions of the window: North, South, East, West, or Center. o North and South expand horizontally o East and West expands vertically o Center expands in both directions See www.cs.armstrong.edu/liang/intro7e/book/ShowBorderLayout.java Section 13.5.4: Properties of Layout Managers The properties can be set dynamically… See the online documentation. Computer Science Notes Chapter 13 Page 6 of 7 Section 13.5.5: The validate and doLayout Methods To change layouts, you must use a two-step process: first, use the setLayout(newLayout) method, then use the validate() method to force the container to re-draw the components To update a current layout after changing its properties, use the doLayout() method Section 13.6: The Color Class Colors are made of red, green, and blue components from 0 to 255 (the RGB model). Example of creating a color object using the public Color(int r, int g, int b) constructor: Color color1 = new Color(128, 100, 100); The setBackground(Color c) and setForeground(Color c) methods (in the java.awt.Component class) can be used to change the colors of a component’s foreground or background. There are 13 pre-defined standard colors… Section 13.7: The Font Class You can create a font using the java.awt.Font class and set fonts for components using the setFont method of the Component class. The Font constructor is: public Font (String name, int style, int size); To find all available fonts on your machine: import java.awt.GraphicsEnvironment; public class PrintAllAvailableFontNames { /**Prints a list of all available fonts for the machine * upon which the program runs. * @param args is not used */ public static void main(String[] args) { GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontNames = e.getAvailableFontFamilyNames(); System.out.println("The fonts available on this system are:"); for (int i = 0; i < fontNames.length; i++) { System.out.println(fontNames[i]); } }//end method main(String[]) }//end class PrintAllAvailableFontNames Computer Science Notes Chapter 13 Page 7 of 7 Section 13.8: Using Panels as Subcontainers See www.cs.armstrong.edu/liang/intro7e/book/TestPanels.java Section 13.9: Common Features of Swing GUI Components See www.cs.armstrong.edu/liang/intro7e/book/TestSwingCommonFeatures.java Section 13.10: Image Icons To display an image icon: o Create a new javax.swing.ImageIcon(fileName) o Construct either a new JLabel(imageIcon) or new JButton(imageIcon) See www.cs.armstrong.edu/liang/intro7e/book/TestImageIcon.java