Java Swing Tutorial

Java Swing Tutorial

Java Swing TutorialJava Swing Tutorial will mainly focus on swing package which is part of  Java Foundation Class(JFC).JFC is used to create window based applications with graphical user interface.
Java Swing is built on AWT(Abstract Window ToolKit).


Differences between  Swing and AWT

  • Swing is platform independent whereas AWT its dependent.
  • Swing is lightweight whereas AWT is heavyweight.
  • Swing supports Look And Feel that can be plugged whereas AWT does not.
  • AWT has fewer components compared to Swing.
  • Swing follows Model View Controller whereas AWT does not.

Java Swing Class Hierarchy 

Java Swing Tutorial 2


Commonly used methods in Swing classes

  • [ void add(Component c)]-add component to another.
  • [void setSize(int w,int h)]-sets component height and width.
  • [void setLayout(LayoutManager l)]-determines how component are displayed.
  • [void setVisible(boolean )]-makes component visible.Set by default false.

Java Swing  JFrame Example

[
import javax.swing.JFrame;
import javax.swing.JButton;
public class FirstSwingExample {
    public static void main(String args[])
    {
        JFrame frame=new JFrame("JFrame example");
        JButton button=new JButton("Click ME");
        button.setBounds(50,50,100,30);

        frame.add(button);
        frame.setLayout(null);
        frame.setSize(400,400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}
]

There are two ways of creating JFrame class.
  • One is by extending JFrame class(Inheritance).
  • Creating JFrame object (association).

Java Swing  JFrame Inheritance Example

[
import javax.swing.JFrame;
import javax.swing.JButton;
public class FirstSwingExample  extends JFrame{
    FirstSwingExample()
    {
        super("Jframe Example");

        JButton button=new JButton("Click ME");
        button.setBounds(50,50,100,30);

        add(button);
        setLayout(null);
        setSize(400,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String args[])
    {
        new FirstSwingExample();

    }
}
]

Java Swing  JFrame Association  Example

[
import javax.swing.JFrame;
import javax.swing.JButton;
public class FirstSwingExample {
    FirstSwingExample()
    {

        JFrame frame=new JFrame("JFrame example");//creates JFrame instance 
        JButton button=new JButton("Click ME");//creates JButton class
        button.setBounds(50,50,100,30);//positions button on JFrame

        frame.add(button);//adds JButton to the JFrame
        frame.setLayout(null);//sets layout 
        frame.setSize(400,400);//sets JFrame Size
        frame.setLocationRelativeTo(null);//centers the Jframe at the center of the screen
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String args[])
    {
        new FirstSwingExample();

    }
}
]

COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: Java Swing Tutorial
Java Swing Tutorial
Java Swing Tutorial
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiT11wzIFINiTSU87JGYivR9y4Mzz0y1acrPNn0_o8lDadSvkEDrxnaQmcBL-Ep8tN9nrkPU5EYAzofAtn4PdIu8Txln6dkHm-dFdEXZvs0pp0NF34kMYiuYd6Dk5gQTlLxJ812FAnPNEy_/s200/java-swing-jframe-example-min.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiT11wzIFINiTSU87JGYivR9y4Mzz0y1acrPNn0_o8lDadSvkEDrxnaQmcBL-Ep8tN9nrkPU5EYAzofAtn4PdIu8Txln6dkHm-dFdEXZvs0pp0NF34kMYiuYd6Dk5gQTlLxJ812FAnPNEy_/s72-c/java-swing-jframe-example-min.PNG
Code WHIZZ
https://code-whizz.blogspot.com/2017/03/java-swing-tutorial.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/03/java-swing-tutorial.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy