370 likes | 392 Views
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.
E N D
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
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
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
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
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.
Container Classes Container classes can contain other GUI components.
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.
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); • } • }
Adding Components into a Frame // Add a button into the frame frame.getContentPane().add( new JButton("OK")); Title bar Content pane
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
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"));
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.
JLabel Class A label is a display area for a short text, an image, or both.
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
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).
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.
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.
JButton Class JButton inherits AbstractButton and provides several constructors to create buttons.
JButton Constructors The following are JButton constructors: JButton() JButton(String text) JButton(String text, Icon icon) JButton(Icon icon)
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.
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);
Demo The following program integrates JLabels, JButtons and JTextFields inside a JPanel and uses the Font Class. See: EditFullName.java
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
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
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
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
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
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
Other Examples • Conditionals and loops enhance our ability to generate interesting graphics: • See BullseyePanel.java • See Bullseye.java • See BoxesPanel.java • See Boxes.java