Categories

Sep 3, 2019 in JAVA

Q: java swing tutorial

1 Answer

Sep 4, 2019

next →← prev

Java Swing Tutorial

Java 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.

next →← prev

Java Swing Tutorial

Java 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 Swing

There are many differences between java awt and swing that are given below.

No. Java AWT Java Swing

1) AWT components are platform-dependent. Java swing components are platform-independent.

2) AWT components are heavyweight. Swing components are lightweight.

3) AWT doesn't support pluggable look and feel. Swing supports pluggable look and feel.

4) AWT provides less components than Swing. Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc.

5) AWT doesn't follows MVC(Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view. Swing follows MVC.

What is JFC

The Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications.

Do You Know

How to create runnable jar file in java?

How to display image on a button in swing?

How to change the component color by choosing a color from ColorChooser ?

How to display the digital watch in swing tutorial ?

How to create a notepad in swing?

How to create puzzle game and pic puzzle game in swing ?

How to create tic tac toe game in swing ?

Sep 4, 2019
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.

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  

}  

}  

Example of Swing by Association inside constructor

We can also write all the codes of creating JFrame, JButton and method call inside the java constructor.

File: Simple.java

import javax.swing.*;  

public class Simple {  

JFrame f;  

Simple(){  

f=new JFrame();//creating instance of JFrame  

          

JButton b=new JButton("click");//creating instance of JButton  

b.setBounds(130,100,100, 40);  

          

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  

}  

  

public static void main(String[] args) {  

new Simple();  

}  

}
Click here to read more about JAVA
Click here to read more about Insurance

Related questions

Madanswer
Sep 3, 2019 in JAVA
Jun 17, 2019 in JAVA
Jun 17, 2019 in JAVA
...