Professional Documents
Culture Documents
What is JFC
The Java Foundation Classes (JFC) are a set of GUI components which
simplify the development of desktop applications.
They all are derived from JComponent class. All these components are
lightweight components.
Container classes are classes that can have other components on it. So for
creating a Java Swing GUI, we need at least one container object.
Panel
JPanel is the simplest container. It provides space in which any other
component can be placed, including other panels.
Frame
A JFrame is a top-level window with a title and a border.
Dialog: It can be thought of like a pop-up window that pops out when a
message has to be displayed. It is not a fully functioning window like the
Frame
Window
A JWindow object is a top-level window with no borders and no menubar.
o import javax.swing.*;
o public class FirstSwingExample {
o public static void main(String[] args) {
o JFrame f=new JFrame();//creating instance of JFrame
o
o JButton b=new JButton("click");//creating instance of JButton
o b.setBounds(130,100,100, 40);//x axis, y axis, width, height
o
o f.add(b);//adding button in JFrame
o
o f.setSize(400,500);//400 width and 500 height
o f.setLayout(null);//using no layout managers
o f.setVisible(true);//making the frame visible
o } }
2.By extending Frame class (inheritance)
import javax.swing.*;
public class Simple2 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();
}}
output:
JFrame:
Unlike Frame, JFrame has the option to hide or close the window with the
help of setDefaultCloseOperation(int) method.
1. import java.awt.FlowLayout;
2. import javax.swing.JButton;
3. import javax.swing.JFrame;
4. import javax.swing.JLabel;
5. import javax.swing.JPanel;
6. public class JFrameExample {
7. public static void main(String s[]) {
8. JFrame frame = new JFrame("JFrame Example");
9. JPanel panel = new JPanel();
10. panel.setLayout(new FlowLayout());
11. JLabel label = new JLabel("JFrame By Example");
12. JButton button = new JButton();
13. button.setText("Button");
14. panel.add(label);
15. panel.add(button);
16. frame.add(panel);
17. frame.setSize(200, 300);
18. frame.setLocationRelativeTo(null);
19. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20. frame.setVisible(true);
21. } }
Output
JWindow:
JWindow is a part of Java Swing and it can appear on any part of the users
desktop. It is different from JFrame in the respect that JWindow does not
have a title bar or window management buttons like minimize, maximize,
and close. JWindow can contain several components such as buttons and
labels.
import java.awt.*;
import javax.swing.*;
window.setBounds(100,50,100,50);
window.add(label);
window.setVisible(true);
}}
Output:
JDialog:
The JDialog control represents a top level window with a border and a title
used to take some form of input from the user. It inherits the Dialog class.
JPanel:
Output:
JButton:
The JButton class is used to create a labeled button that has platform
independent implementation.
Output:
JCheckBox:
Output:
JRadioButton:
Output:
JLabel:
1. import javax.swing.*;
2. class LabelExample
3. {
4. public static void main(String args[])
5. {
6. JFrame f= new JFrame("Label Example");
7. JLabel l1,l2;
8. l1=new JLabel("First Label.");
9. l1.setBounds(50,50, 100,30);
10. l2=new JLabel("Second Label.");
11. l2.setBounds(50,100, 100,30);
12. f.add(l1); f.add(l2);
13. f.setSize(300,300);
14. f.setLayout(null);
15. f.setVisible(true);
16. } }
Output:
JTextField:
JTextField is used for taking input of single line of text. It is most widely
used text component.
Class declaration:
1. import javax.swing.*;
2. class TextFieldExample
3. {
4. public static void main(String args[])
5. {
6. JFrame f= new JFrame("TextField Example");
7. JTextField t1,t2;
8. t1=new JTextField("Welcome to Javatpoint.");
9. t1.setBounds(50,100, 200,30);
10. t2=new JTextField("AWT Tutorial");
11. t2.setBounds(50,150, 200,30);
12. f.add(t1); f.add(t2);
13. f.setSize(400,400);
14. f.setLayout(null);
15. f.setVisible(true);
16. } }
Output:
JTextArea:
The object of a JTextArea class is a multi line region that displays text. It
allows the editing of multiple line text. It inherits JTextComponent class.
1. import javax.swing.*;
2. public class TextAreaExample
3. {
4. TextAreaExample(){
5. JFrame f= new JFrame();
6. JTextArea area=new JTextArea("Welcome to javatpoint");
7. area.setBounds(10,30, 200,200);
8. f.add(area);
9. f.setSize(300,300);
10. f.setLayout(null);
11. f.setVisible(true);
12. }
13. public static void main(String args[])
14. {
15. new TextAreaExample();
16. }}
Output:
JList:
The object of JList class represents a list of text items. The list of text items
can be set up so that the user can choose either one item or multiple items.
It inherits JComponent class.
1. import javax.swing.*;
2. public class ListExample
3. {
4. ListExample(){
5. JFrame f= new JFrame();
6. DefaultListModel<String> l1 = new DefaultListModel<>();
7. l1.addElement("Item1");
8. l1.addElement("Item2");
9. l1.addElement("Item3");
10. l1.addElement("Item4");
11. JList<String> list = new JList<>(l1);
12. list.setBounds(100,100, 75,75);
13. f.add(list);
14. f.setSize(400,400);
15. f.setLayout(null);
16. f.setVisible(true);
17. }
18. public static void main(String args[])
19. {
20. new ListExample();
21. }}
Output:
JComboBox:
The object of Choice class is used to show popup menu of choices. Choice
selected by user is shown on the top of a menu. It
inherits JComponent class.
1. import javax.swing.*;
2. public class ComboBoxExample {
3. JFrame f;
4. ComboBoxExample(){
5. f=new JFrame("ComboBox Example");
6. String country[]={"India","Aus","U.S.A","England","Newzealand"};
JScrollPane:
1. import java.awt.FlowLayout;
2. import javax.swing.JFrame;
3. import javax.swing.JScrollPane;
4. import javax.swing.JtextArea;
5. public class JScrollPaneExample {
6. private static final long serialVersionUID = 1L;
7. private static void createAndShowGUI() {
8. // Create and set up the window.
9. final JFrame frame = new JFrame("Scroll Pane Example");
10. // Display the window.
11. frame.setSize(500, 500);
12. frame.setVisible(true);
13. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
14. // set flow layout for the frame
15. frame.getContentPane().setLayout(new FlowLayout());
16. JTextArea textArea = new JTextArea(20, 20);
17. JScrollPane scrollableTextArea = new JScrollPane(textArea
);
18. scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.H
ORIZONTAL_SCROLLBAR_ALWAYS);
19. scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VER
TICAL_SCROLLBAR_ALWAYS);
20. frame.getContentPane().add(scrollableTextArea);
21. }
22. public static void main(String[] args) {
23. javax.swing.SwingUtilities.invokeLater(new Runnable() {
24. public void run() {
25. createAndShowGUI();
26. }
27. });
28. } }
Output:
JToggleButton:
1. import java.awt.FlowLayout;
2. import java.awt.event.ItemEvent;
3. import java.awt.event.ItemListener;
4. import javax.swing.JFrame;
5. import javax.swing.JToggleButton;
6.
7.public class JToggleButtonExample extends JFrame implements Ite
mListener {
8. public static void main(String[] args) {
9. new JToggleButtonExample();
10. }
11. private JToggleButton button;
12. JToggleButtonExample() {
13. setTitle("JToggleButton with ItemListener Example");
14. setLayout(new FlowLayout());
15. setJToggleButton();
16. setAction();
17. setSize(200, 200);
18. setVisible(true);
19. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20. }
21. private void setJToggleButton() {
22. button = new JToggleButton("ON");
23. add(button);
24. }
25. private void setAction() {
26. button.addItemListener(this);
27. }
28. public void itemStateChanged(ItemEvent eve) {
29. if (button.isSelected())
30. button.setText("OFF");
31. else
32. button.setText("ON");
33. } }
Output:
APPLETS
Applet: A small application written in Java programming language is
embedded into a web page known as applet.
Most applets are coded in the Java language and use the Java Virtual
Machine to run.
Applet is a special type of program that is embedded in the webpage to
generate the dynamic content. It runs inside the browser and works at
client side.
Any applet in Java is a class that extends the java.applet.Applet class.
An Applet class does not have any main() method. It is viewed using JVM.
The JVM can use either a plug-in of the Web browser or a separate runtime
environment to run an applet application.
In order to implement the Applet we need to import awt package :
java.awt.applet.*;
Applets can be used to create games, simulations, and other interactive
applications.
Hierarchy of Applet:
Life cycle of an Applet:
1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
init
start
stop
destroy
paint
1. By html file.
To execute the applet by html file, create an applet and , After that create an
html file and place the applet code in html file. Now click the html file.
//First.java
import java.awt.applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome",150,150);
} }
myapplet.html
<html>
<body>
<applet code="First" width="300" height="300">
</applet>
</body>
</html>
2.By appletViewer tool
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome to applet",150,150);
} }
1. /*
2. <applet code="First.class" width="300" height="300">
3. </applet>
4. */
c:\>javac First.java
c:\>appletviewer First.java
simple applet:
1. /*Java Program to Create a Basic Applet*/
2. import java.applet.*;
3. import java.awt.*;
4. public class Basic_Applet extends Applet
5. {
6. //Function to initialize the applet
7. public void init()
8. {
9. setBackground(Color.white);
10. setLayout(new FlowLayout(FlowLayout.CENTER));
11. }
12. //Function to draw the string
13. public void paint(Graphics g)
14. {
15. g.drawString("Hello World",100,200);
16. }
17. }
18. /*
19. <applet code=Basic_Applet.class width=400 height=400>
20. </applet>
21. */
To compile and run the program use the following commands :
>>>javac Basic_Applet.java
>>>appletviewer Basic_Applet.java
Output: