Java AWT, Swing_notes
Java AWT, Swing_notes
Java AWT
Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface
(GUI) or windows-based applications in Java.
The java.awt package provides classes for AWT API such as TextField, Label,
TextArea, RadioButton, CheckBox, Choice, List etc.
Container
The Container is a component in AWT that can contain another components like
buttons, textfields, labels etc. The classes that extends Container class are known
as container such as Frame, Dialog and Panel.
ActionEvent ActionListener
KeyEvent KeyListener
TextEvent TextListener
Java Swing
Java Swing was introduced as part of the Java Foundation Classes (JFC) in the
late 1990s, aiming to address the limitations of the earlier Abstract Window
Toolkit (AWT). Unlike AWT, that relies on the native platform's components for
rendering, Swing is entirely written in Java, offering a consistent look and feel
across different operating systems.
Rich Set of Components: Swing offers a wide range of components that can be
used to create complex GUIs. Developers can choose from basic components like
buttons and labels to advanced components such as tables, trees, and scroll
panes.
Event Handling: Swing provides a robust event handling mechanism that allows
developers to respond to user interactions, such as button clicks and mouse
movements. This enables the creation of interactive and responsive applications.
Output:
Example of Swing by Inheritance
We can also inherit the JFrame class, so there is no need to create the instance of
JFrame class explicitly.
import javax.swing.*;
public class DemoSwing extends JFrame{//inheriting JFrame
JFrame f;
Simple2(){
JButton b=new JButton("click");//create button
b.setBounds(130,100,100, 40);
add(b);//adding button on frame
setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new Simple2();
}}
Java JButton
The JButton class is used to create a labeled button that has platform
independent implementation. The application result in some action when the
button is pushed. It inherits AbstractButton class.
A key element of graphical user interfaces (GUIs) in Java that is used to create
interactive buttons is the JButton class. Users can click these labelled buttons to
initiate particular operations within the application. Because JButton offers a
platform-independent implementation, it can be used in a variety of settings and
operating systems. It is descended from the AbstractButton class, which offers
shared functionality for all button kinds in the Swing GUI framework and Java's
Abstract Window Toolkit (AWT). Developers can improve the usability and
interactivity of their Java programmes by adding sensible user interface
components to their JButton objects through configuration.
import java.awt.event.*;
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Java.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}