1 / 36

Mastering Java GUI Development: AWT vs. Swing Components Guide

Explore the world of Java GUI development using AWT and Swing libraries. Understand GUI components, containers, and objects creation to design interactive applications. Learn about lightweight vs. heavyweight containers, Swing components, and the evolution from AWT to Swing. Dive into GUI class hierarchy, container classes, and helper classes for advanced GUI development.

jkhoury
Download Presentation

Mastering Java GUI Development: AWT vs. Swing Components Guide

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. Creating GUIs in Java UsingAWT and SwingLibraries

  2. GUI Components • A GUI component is an object that represents a screen element such as a button or a text field • GUI-related classes are defined primarily in the java.awt and the javax.swing packages • The Abstract Windowing Toolkit (AWT) was the original Java GUI package • The Swing package provides additional and more versatile components • Both packages are needed to create a Java GUI-based program

  3. GUI Containers • A GUI container is a component that is used to hold and organize other components • A frame is a container that is used to display a GUI-based Java application • A frame is displayed as a separate window with a title bar – it can be repositioned and resized on the screen as needed • A panel is a container that cannot be displayed on its own but is used to organize other components • A panel must be added to another container to be displayed

  4. GUI Containers • A GUI container can be classified as either heavyweight or lightweight • A heavyweight container is one that is managed by the underlying operating system • A lightweight container is managed by the Java program itself • Occasionally this distinction is important • A frame is a heavyweight container and a panel is a lightweight container

  5. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your name: " JLabel jlblName = new JLabel("Enter your name: "); // Create a text field with text "Type Name Here" JTextField jtfName = new JTextField("Type Name Here"); // Create a check box with text bold JCheckBox jchkBold = new JCheckBox("Bold"); // Create a radio button with text red JRadioButton jrbRed = new JRadioButton("Red"); // Create a combo box with choices red, green, and blue JComboBox jcboColor = new JComboBox(new String[]{"Red", "Green", "Blue"}); Radio Button Label Text field Check Box Button Combo Box

  6. Swing vs. AWT So why do the GUI component classes have a prefix J? Instead of JButton, why not name it simply Button? In fact, there is a class already named Button in the java.awt package. When Java was introduced, the GUI classes were bundled in a library known as the Abstract Windows Toolkit (AWT).For every platform on which Java runs, the AWT components are automatically mapped to the platform-specific components through their respective agents, known as peers. AWT is fine for developing simple graphical user interfaces, but not for developing comprehensive GUI projects. Besides, AWT is prone to platform-specific bugs because its peer-based approach relies heavily on the underlying platform. With the release of Java 2, the AWT user-interface components were replaced by a more robust, versatile, and flexible library known as Swing components. Swing components are painted directly on canvases using Java code, except for components that are subclasses of java.awt.Window or java.awt.Panel, which must be drawn using native GUI on a specific platform. Swing components are less dependent on the target platform and use less of the native GUI resource. For this reason, Swing components that don’t rely on native GUI are referred to as lightweight components, and AWT components are referred to as heavyweight components.

  7. GUI Class Hierarchy (Swing)

  8. Container Classes Container classes can contain other GUI components.

  9. GUI Helper Classes The helper classes are not subclasses of Component. They are used to describe the properties of GUI components such as graphics context, colors, fonts, and dimension.

  10. Swing GUI Components

  11. AWT GUI Components

  12. Creating Frames • import javax.swing.*; • public class MyFrame { • public static void main(String[] args) { • JFrame frame = new JFrame("Test Frame"); • frame.setSize(400, 300); • frame.setVisible(true); • frame.setDefaultCloseOperation( • JFrame.EXIT_ON_CLOSE); • } • }

  13. Adding Components into a Frame // Add a button into the frame frame.getContentPane().add( new JButton("OK")); Title bar Content pane

  14. Content Pane Delegation in JDK 1.5 // Add a button into the frame frame.getContentPane().add( new JButton("OK")); Title bar // Add a button into the frame frame.add( new JButton("OK")); Content pane

  15. JFrame Class

  16. JPanel Class • You use JPanels to create a panel that acts as a container for multiple types of GUI components. A frame only takes one component, but a panel can have many in different arrangements. There is only one frame per window, there can be many panels in one window. • Use the add(Component) method to add a component to the panel. For example, JPanel p = new JPanel(); p.add(new JButton("OK"));

  17. JPanel Class • JPanels act as sub-containers for grouping user interface components. • It is recommended that you place the user interface components in panels and place the panels in a frame. You can also place panels in a panel. • To add a component to JFrame, you add it to the content pane of JFrame. To add a component to a panel, you add it directly to the panel using the add method.

  18. JLabel Class A label is a display area for a short text, an image, or both.

  19. JLabel Class • A JLabel is a GUI component that displays a line of text • Labels are usually used to display information or identify other components in the interface • Let's look at a program that organizes two labels in a panel and displays that panel in a frame • See Authority.java • This program is not interactive, but the frame can be repositioned and resized

  20. JTextField Class A text field is an input area where the user can type in characters. Text fields are useful in that they enable the user to enter in variable data (such as a name or a description).

  21. Some JTextField Methods • getText() Returns the string from the text field. • setText(String text) Puts the given string in the text field. • setEditable(boolean editable) Enables or disables the text field to be edited. By default, editable is true.

  22. Buttons A button is a component that triggers an action event when clicked. Swing provides regular buttons, toggle buttons, check box buttons, and radio buttons. The common features of these buttons are generalized in javax.swing.AbstractButton.

  23. AbstractButton

  24. JButton Class JButton inherits AbstractButton and provides several constructors to create buttons.

  25. JButton Constructors The following are JButton constructors: JButton() JButton(String text) JButton(String text, Icon icon) JButton(Icon icon)

  26. Some JButton Methods • getText() Returns the string shown on the button. • setText(String text) Puts the given string on the button. • getIcon() Returns the icon shown on the button. • setIcon(Icon icon) Puts the given icon image on the button.

  27. The Font Class Font Names • Standard font names that are supported in most platforms are: SansSerif, Serif, Monospaced, Dialog, or DialogInput. Font Style • Font.PLAIN (0), Font.BOLD (1), Font.ITALIC (2), and Font.BOLD + Font.ITALIC (3) Font myFont = new Font(name, style, size); Example: Font myFont = new Font("SansSerif ", Font.BOLD, 16); Font myFont = new Font("Serif", Font.BOLD+Font.ITALIC, 12); JButton jbtOK = new JButton("OK“); jbtOK.setFont(myFont);

  28. Demo The following program integrates JLabels, JButtons and JTextFields inside a JPanel and uses the Font Class. See: EditFullName.java

  29. Nested Panels • Containers that contain other components make up the containment hierarchy of an interface • This hierarchy can be as intricate as needed to create the visual effect desired • The following example nests two panels inside a third panel – note the effect this has as the frame is resized • See NestedPanels.java

  30. Common Features of Swing Components

  31. Modifying the JPanel • Every Swing component class (like JFrame, JPanel and JLabel) has a paintComponent method • The paintComponent method accepts a Graphics object that represents the graphics context for that component just like the paint method for applets. • To create a version of the Snowman program that is an application instead of an applet we could create an inherited version of the JPanel class and then appropriately modify the paintComponent method. • This is done in one file in: SnowmanApplication.java

  32. Modifying the JPanel • You can only have one public class per class file since the name of the public class is the name of the file. Other classes in the file must be private. • If we wanted our SnowmanPanel to be visible to other classes so it could also be used by other classes, we would have to make it public. But to do that, we would have to put it in its own file, as in the following example: • See SnowmanPanel2.java • See SnowmanApplication2.java

  33. Smiling Face Example • The SmilingFace program draws a face by overriding the paintComponent method of a panel • See SmilingFacePanel.java • See SmilingFace.java • The main method of the SmilingFace class instantiates a SmilingFacePanel and displays it • The SmilingFacePanel class is derived from the JPanel class using inheritance

  34. Smiling Face Example • Every Swing component has a paintComponent method • The paintComponent method accepts a Graphics object that represents the graphics context for the panel • We define the paintComponent method to draw the face with appropriate calls to the Graphics methods • Note the difference between drawing on a panel and adding other GUI components to a panel

  35. Splat Example • The Splat example is structured a bit differently • It draws a set of colored circles on a panel, but each circle is represented as a separate object that maintains its own graphical information • The paintComponent method of the panel "asks" each circle to draw itself • See Circle.java • See SplatPanel.java • See Splat.java

  36. Other Examples • Conditionals and loops enhance our ability to generate interesting graphics: • See BullseyePanel.java • See Bullseye.java • See BoxesPanel.java • See Boxes.java

More Related