Java Swing TutorialJava Swing tutorial is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java. Unlike AWT, Java Swing provides platform-independent and lightweight components. The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc. Difference between AWT and SwingThere are many differences between java awt and swing that are given below.
What is JFCThe Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications. Hierarchy of Java Swing classesThe hierarchy of java swing API is given below. Commonly used Methods of Component classThe methods of Component class are widely used in java swing that are given below.
Java Swing ExamplesThere are two ways to create a frame:
We can write the code of swing inside the main(), constructor or any other method. Simple Java Swing ExampleLet's see a simple swing example where we are creating one button and adding it on the JFrame object inside the main() method. File: FirstSwingExample.java Example of Swing by Association inside constructorWe can also write all the codes of creating JFrame, JButton and method call inside the java constructor. File: Simple.java The setBounds(int xaxis, int yaxis, int width, int height)is used in the above example that sets the position of the button. Simple example of Swing by inheritanceWe can also inherit the JFrame class, so there is no need to create the instance of JFrame class explicitly. File: Simple2.java Next TopicJava JButton Class |