Download presentation
Presentation is loading. Please wait.
1
Welcome To java
2
Graphics and imaging tools, including shape, color, and font classes.
AWT (Abstract Window ToolKit) It is a portable GUI library for stand-alone applications and/or applets. The Abstract Window Toolkit provides the connection between your application and the native GUI. AWT features include: A rich set of user interface components. A robust event-handling model. Graphics and imaging tools, including shape, color, and font classes. Layout managers, for flexible window layouts that don't depend on a particular window size or screen resolution. Data transfer classes, for cut-and-paste through the native platform clipboard. The AWT components depend on native code counterparts (called peers) to handle their functionality. Thus, these components are often called "heavyweight" components.
3
Swing Swing implements a set of GUI components that build on AWT technology and provide a pluggable look and feel. Swing is implemented entirely in the Java programming language, and is based on the JDK 1.1 Lightweight UI Framework. Swing features include: All the features of AWT. 100% Pure Java certified versions of the existing AWT component set (Button, Scrollbar, Label, etc.). A rich set of higher-level components (such as tree view, list box, and tabbed panes). Pure Java design, no reliance on peers. Pluggable Look and Feel. Swing components do not depend on peers to handle their functionality. Thus, these components are often called "lightweight" components.
4
AWT Swing AWT stands for Abstract Window Toolkit. Swing is a part of Java Foundation Class (JFC). AWT components are heavy weight. Swing components are light weight. AWT components are platform dependent so there look and feel changes according to OS. Swing components are platform independent so there look and feel remains constant. AWT components are not very good in look and feel as compared to Swing components. See the button in below image, its look is not good as button created using Swing. Swing components are better in look and feel as compared to AWT. See the button in below image, its look is better than button created using AWT.
6
Commonly used Methods of Component class
Description public void add(Component c) add a component on another component. public void setSize(int width,int height) sets size of the component. public void setLayout(LayoutManager m) sets the layout manager for the component. public void setVisible(boolean b) sets the visibility of the component. It is by default false.
7
Java Swing Examples There are two ways to create a frame: By creating the object of Frame class (association) By extending Frame class (inheritance) We can write the code of swing inside the main(), constructor or any other method.
8
Simple Java Swing Example
import javax.swing.*; public class FirstSwingExample { public static void main(String[] args) { JFrame f=new JFrame();//creating instance of JFrame JButton b=new JButton("click");//creating instance of JButton b.setBounds(130,100,100, 40);//x axis, y axis, width, height f.add(b);//adding button in JFrame f.setSize(400,500);//400 width and 500 height f.setLayout(null);//using no layout managers f.setVisible(true);//making the frame visible }
10
Event and Listener (Java Event Handling)
Java Event classes and Listener interfaces Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces for event handling.
11
Event Classes Listener Interfaces ActionEvent ActionListener MouseEvent MouseListener and MouseMotionListener MouseWheelEvent MouseWheelListener KeyEvent KeyListener ItemEvent ItemListener TextEvent TextListener AdjustmentEvent AdjustmentListener WindowEvent WindowListener ComponentEvent ComponentListener ContainerEvent ContainerListener FocusEvent FocusListener
12
Steps to perform Event Handling
Following steps are required to perform event handling: Register the component with the Listener
13
Registration Methods For registering the component with the Listener, many classes provide the registration methods. For example: Button public void addActionListener(ActionListener a){} MenuItem TextField public void addTextListener(TextListener a){} TextArea Checkbox public void addItemListener(ItemListener a){} Choice List
14
Java Event Handling Code
We can put the event handling code into one of the following places: Within class Other class Anonymous class
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.