1) What is Java Swing?
It is a part of JFC (Java Foundation Classess) that is used to create window-based applications.
Java Swing components are platform independent and lightweight .
2) What is difference between AWT and Swing?
The differences between AWT and Swing are:
AWT |
Swing |
Platform-dependent |
Platform-independent |
Heavyweight components |
Lightweight components |
Doesn't support pluggable look and feel |
Supports pluggable look and feel |
Doesn't Follow MVC (Model View Controller) |
Follow MVC (Model View Controller) |
Less components |
More components |
3) What are the methods of component class in Java Swing?
There are four types of methods of component class are:
- public void add(Component c)
- public void setSize(int width, int height)
- public void setLayout(LayoutManager m)
- public void setVisible(boolean b)
4) How many ways to create a frame in Java Swing ?
There are two ways to create a frame:
- By Association(creating the object of Frame class)
- By Inheritance(extending Frame class)
5) Write a simple Java Swing program to create a JButton and adding it on the JFrame object inside the main() method?
Example:
import javax.swing.*;
public class SwingExample
{
public static void main(String[] args)
{
JFrame f=new JFrame("SIMPLE FRAME");
JButton b=new JButton("Click Me");
b.setBounds(50, 50, 100, 50);
f.add(b);
f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
}
}
6) What are the methods of AbstractButton class in Java Swing?
There are several types of methods of AbstractButton class are:
- void setIcon(Icon b)
- Icon getIcon()
- void setText(String s)
- String getText()
- void setEnabled(boolean b)
- void setMnemonic(int a)
- void addActionListener(ActionListener a)
7) Write a simple Java Swing program to create a JButton by inherit the JFrame Class?
Example:
import javax.swing.*;
public class SwingExample2 extends JFrame
{
SwingExample2()
{
JButton b= new JButton("Click Me");
b.setBounds(50, 50, 100, 50);
add(b);
setSize(500,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args)
{
new SwingExample2();
}
}
8) Write a simple Java Swing program to create a JButton With ActionListener?
Example of JButton with ActionListener:
import java.awt.event.*;
import javax.swing.*;
public class SwingExample3
{
public static void main(String[] args) {
JFrame f=new JFrame();
final JTextField tf=new JTextField();
tf.setBounds(100, 50, 200, 30);
JButton b=new JButton("Click Here");
b.setBounds(100, 100, 95, 35);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
tf.setText("Welcome JavaSwing Expert");
}
});
f.add(tf) ;f.add(b);
f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
}
}
9) Write a simple Java Swing program of displaying image on the button?
Example:
import javax.swing.*;
public class SwingExample4 {
SwingExample4()
{
JFrame f=new JFrame();
JButton b=new JButton(new ImageIcon("C:\\132.jpg"));
b.setBounds(125, 125, 125, 75);
f.add(b);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new SwingExample4();
}
10) What are the methods of JLabel class in Java Swing?
Methods of JLabel class are:
- void setText(String text)
- String getText()
- Icon getIcon()
- int getHorizontalAlignment()
- void setHorizontalAlignment(int alignment)