Java Swing Complete examples step by step(make a project with swing)

 FIRST PROGRAM(JFRAME):

import java.awt.*;
import javax.swing.*;

class MyFirstFrame{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("My First frame");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(50,100,800,500);
//for changing the icon we need create object of ImageIcon
ImageIcon img = new ImageIcon("bikas.jpg");
frame.setIconImage(img.getImage());
Container c = frame.getContentPane();
//c.setBackground(Color.RED);
c.setBackground(new Color(80, 149, 80));
}
}
OUTPUT:

 Second  PROGRAM(JLABEL):

import java.awt.*;
import javax.swing.*;
class MyLabel{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("JAVA");
frame.setBounds(50,250,1000,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Container c=frame.getContentPane();
c.setLayout(null);
//Color color = new  Color(255,0,0);
//c.setBackground(color);
//frame.setResizable(true);

JLabel lable1 = new JLabel();
lable1.setText("Username");
lable1.setBounds(100,50,200,50);
Font font = new Font("Aerial",Font.PLAIN,30);
lable1.setFont(font);
c.add(lable1);
//ImageIcon img = new ImageIcon("bikas.jpeg");
//JLabel label2 = new JLabel(img,JLabel.CENTER);
//label2.setBounds(300,50,img.getIconWidth(),img.getIconHeight() );
JLabel lable2 = new JLabel();
        lable2.setText("password");
lable2.setBounds(100,300,200,50);
//Font font = new Font("Aerial",Font.PLAIN,30);
lable2.setFont(font);
c.add(lable2);
}

}

THIRD  PROGRAM(JTEXTFIELD):

import java.awt.*;
import javax.swing.*;
class MyTextField{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("TextFiled Demonstration");
frame.setBounds(100,100,800,600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container c = frame.getContentPane();
c.setLayout(null);

Font font = new Font("Aerial",Font.BOLD,30);
//creating jtextfield
JTextField t1= new JTextField();
c.add(t1);
t1.setBounds(100,75,200,55);
t1.setText("Username");
t1.setFont(font);
}

}

FOURTH  PROGRAM(JPASSWORDFIELD):

import java.awt.*;
import javax.swing.*;
class MyPassword{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100,150,800,600);
frame.setTitle("JPasswordField Demonstation");

Container c = frame.getContentPane();
c.setLayout(null);

Font font = new Font("Arial",Font.BOLD,32);

JPasswordField  p1 = new JPasswordField();
p1.setBounds(50,200,320,50);
p1.setFont(font);
c.add(p1);
p1.setEchoChar('*');
}
}

FIFTH PROGRAM(JBUTTON):

import javax.swing.*;
import java.awt.*;

class MyButton{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("JButton Demonstatrion");
frame.setBounds(100,100,800,600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container c = frame.getContentPane();
c.setLayout(null);

Font font = new Font("Verdana",Font.BOLD,30);
Cursor cur= new Cursor(Cursor.WAIT_CURSOR);

JButton btn = new JButton();
btn.setText("Click");
c.add(btn);
btn.setSize(200,75);
btn.setLocation(100,200);
btn.setFont(font);
btn.setForeground(Color.RED);
btn.setBackground(Color.YELLOW);
btn.setCursor(cur);
//btn.setEnabled(false);

}
}

SIXTH PROGRAM(#1ACTION LISTENER):

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class MyFrame extends JFrame implements ActionListener{

JButton btn1,btn2;

Container c;

MyFrame(){

c=this.getContentPane();

c.setLayout(null);

btn1 = new JButton();

btn2 = new JButton();

btn1.setText("CLickme");

btn1.setBounds(50,100,200,100);

c.add(btn1);

btn2.setText("RESULT");

btn2.setBounds(200,300,200,100);

c.add(btn2);

btn1.addActionListener(this);

}

public void actionPerformed(ActionEvent e){

//c.setBackground(Color.RED);

btn2.setEnabled(false);

}

}

class Action{

public static void main(String[] args) {

MyFrame f = new MyFrame();

f.setTitle("Action Listener Demonstration");

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setBounds(100,50,900,500);

}

}

7th program(#2Multiple ACTION LISTENER):


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyFrame extends JFrame implements ActionListener{
Container c;
JButton btn1,btn2;
JTextField t1,t2;

MyFrame(){
c=this.getContentPane();
c.setLayout(null);

btn1 = new JButton();
btn2 = new JButton();
t1= new JTextField();
t2= new JTextField();

btn1.setText("CLICK HERE");
btn2.setText("CLICK HERE");

btn1.setBounds(50,100,200,50);
btn2.setBounds(300,100,200,50);

t1.setBounds(50,300,200,50);
t2.setBounds(300,300,200,50);

c.add(btn1);
c.add(btn2);
c.add(t1);
c.add(t2);

btn1.addActionListener(this);
btn2.addActionListener(this);
//Font font = new Font("Arial",Font.BOLD,20);
}
public void actionPerformed(ActionEvent e)

{
if(e.getSource()==btn1){
Font font = new Font("Arial",Font.BOLD,20);

t1.setText("HappyBirthday");
t1.setFont(font);
c.setBackground(Color.PINK);
}
if(e.getSource()==btn2){
Font font = new Font("Arial",Font.BOLD,20);
t2.setText("I love you");
t2.setFont(font);
c.setBackground(Color.RED);
}
}
}
class Action2{
public static void main(String[] args) {
MyFrame f = new MyFrame();
f.setTitle("Action Listener Demonstration-2");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100,100,600,600);
f.setVisible(true);
}
}

8th program(#3inner class ActionListener):

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Action3{
public static Container c;
public static void main(String[] args) {
JFrame f= new JFrame();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("action listener with innerclass demonstration");
f.setBounds(200,140,500,500);

c = f.getContentPane();
c.setLayout(null);

JButton red = new JButton("RED");
JButton yellow = new JButton("YELLOW");
JButton green = new JButton("GREEN");
JButton pink = new JButton("PINK");

red.setBounds(50,50,150,75);
yellow.setBounds(300,50,150,75);
green.setBounds(50,300,150,75);
pink.setBounds(300,300,150,75);

c.add(red);
c.add(yellow);
c.add(green);
c.add(pink);

red.addActionListener(new RedClass());
yellow.addActionListener(new YellowClass());
green.addActionListener(new GreenClass());
pink.addActionListener(new PinkClass());
}
}
class RedClass implements ActionListener{
public void actionPerformed(ActionEvent e){
Action3.c.setBackground(Color.RED);
}
}
class YellowClass implements ActionListener{
public void actionPerformed(ActionEvent e){
Action3.c.setBackground(Color.YELLOW);
}
}
class GreenClass implements ActionListener{
public void actionPerformed(ActionEvent e){
Action3.c.setBackground(Color.GREEN);
}
}
class PinkClass implements ActionListener{
public void actionPerformed(ActionEvent e){
Action3.c.setBackground(Color.PINK);
}
}
OUTPUT:

9th simple login form that displays username and password in console:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


class MyFrame extends JFrame implements ActionListener{
JLabel user,pass;
JTextField t1;
JPasswordField t2;
JButton btn;
Container c;
MyFrame(){
c=this.getContentPane();
c.setLayout(null);
user = new JLabel();
pass= new JLabel();

user.setText("Username");
user.setBounds(50,100,100,50);
c.add(user);

pass.setText("Password");
pass.setBounds(50,200,200,50);
c.add(pass);

t1= new JTextField();
t1.setBounds(200,100,200,50);
c.add(t1);

t2= new JPasswordField();
t2.setBounds(200,200,200,50);
c.add(t2);

btn = new JButton("Login");
btn.setBounds(250,300,100,50);
c.add(btn);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
System.out.println("Username"+t1.getText());
System.out.println("Password"+t2.getText());
}
}
class Loging{
public static void main(String[] args){
MyFrame f= new MyFrame();
//f.setTitle("Logn");
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100,150,500,500);
f.setVisible(true);
}
}
OUTPUT:

9th program(A simple calculator):

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyFrame extends JFrame implements ActionListener{
JLabel l1,l2,result;
JTextField t1,t2;
JButton plus,sub,mul,div;
Container c;
MyFrame(){

c = this.getContentPane();
c.setLayout(null);

result = new JLabel("RESULT: ");
result.setBounds(10,5,380,70);
c.add(result);

l1= new JLabel("Insert FirstNumber");
l1.setBounds(10,70,420,70);
c.add(l1);

l2= new JLabel("Inser SecondNumber");
l2.setBounds(10,95,420,70);
c.add(l2);

t1 = new JTextField();
t1.setBounds(150,95,180,20);
c.add(t1);

t2 = new JTextField();
t2.setBounds(180,120,180,20);
c.add(t2);

plus = new JButton("+");
plus.setBounds(10,150,50,50);
c.add(plus);

sub = new JButton("-");
sub.setBounds(70,150,50,50);
c.add(sub);

mul = new JButton("x");
mul.setBounds(130,150,50,50);
c.add(mul);
div = new JButton("/");
div.setBounds(190,150,50,50);
c.add(div);

plus.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);

}
public void actionPerformed(ActionEvent e){
try{if(e.getSource()==plus){
//c.setBackground(Color.RED);
int a= Integer.parseInt(t1.getText());
int b= Integer.parseInt(t2.getText());
int c= a+b;
result.setText("RESULT:  "+c);
}
if(e.getSource()==sub){
//c.setBackground(Color.RED);
int a= Integer.parseInt(t1.getText());
int b= Integer.parseInt(t2.getText());
int c= a-b;
result.setText("RESULT: "+c);
}
if(e.getSource()==mul){
//c.setBackground(Color.RED);
int a= Integer.parseInt(t1.getText());
int b= Integer.parseInt(t2.getText());
int c= a*b;
result.setText("RESULT: "+c);
}
if(e.getSource()==div){
//c.setBackground(Color.RED);
int a= Integer.parseInt(t1.getText());
int b= Integer.parseInt(t2.getText());
//{result.setText("cannot divide by zero");}
try{int c= a/b;

result.setText("RESULT: "+c);}
catch(ArithmeticException en){
result.setText("cannot divide by zero");}
}}
catch(NumberFormatException en){
result.setText("enter number only");
}
}
}

class Calculator1{
public static void main(String[] args) {
MyFrame f = new MyFrame();
f.setBounds(100,100,400,400);
f.setTitle("Calculator");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);    
}
}

10th programJRadioButton:

import javax.swing.*;
import java.awt.*;

class Radio{
public static void main(String[] args) {
JFrame f = new JFrame("Radio button");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(200,100,800,600);

Container c = f.getContentPane();
c.setLayout(null);
JRadioButton r1 = new JRadioButton("male");
r1.setBounds(20,50,100,50);
c.add(r1);


JRadioButton r2 = new JRadioButton("female");
r2.setBounds(20,100,100,50);
c.add(r2);

ButtonGroup gender = new ButtonGroup();
gender.add(r1);
gender.add(r2);

r1.setSelected(true);
f.setVisible(true);
}

}

11th programJCheckBOX:

import javax.swing.*;
import java.awt.*;
class CheckBox{
public static void main(String[] args) {
JFrame f = new JFrame("CheckBox");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(150,150,500,400);

Container c = f.getContentPane();
c.setLayout(null);

JCheckBox b1 = new JCheckBox("Nepali");
b1.setBounds(20,50,100,50);
c.add(b1);
JCheckBox b2 = new JCheckBox("Math");
b2.setBounds(20,90,100,50);
c.add(b2);
JCheckBox b3 = new JCheckBox("Science");
b3.setBounds(20,140,100,50);
c.add(b3);

b1.setSelected(true);
f.setVisible(true);
}

}

12th programJComboBOX with labels:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class Combo{
public static void main(String[] args) {
JFrame f = new JFrame("ComboBOx");
f.setSize(500,400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container c = f.getContentPane();
c.setLayout(null);

String[] fruits={"apple","banana","orange","mango"};


JComboBox c1= new JComboBox(fruits);
c1.setBounds(50,100,120,30);
c.add(c1);

JButton btn = new JButton("Select");
btn.setBounds(200,100,120,30);
c.add(btn);

JLabel l1= new JLabel("thikxa");
l1.setBounds(350,100,120,30);
c.add(l1);

btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//c.setBackground(Color.RED);
String item = (String)c1.getSelectedItem();
l1.setText(item);
}
});
f.setVisible(true);
}
}

13th program Registration form:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class MyFrame extends JFrame implements ActionListener{
JLabel l1,l2,l3,l4,msg;
JTextField t1,t2;
JTextArea a1,screen;
JRadioButton r1,r2;
JComboBox c1;
JComboBox c2;
JComboBox c3;
Container c;
ButtonGroup gender;
JCheckBox b;
JButton btn;
MyFrame(){
c=this.getContentPane();
c.setLayout(null);

l1 = new JLabel("NAME");
l1.setBounds(20,50,80,50);
c.add(l1);

l2 = new JLabel("MOBILE NO.");
l2.setBounds(20,130,100,50);
c.add(l2);

l3 = new JLabel("GENDER");
l3.setBounds(20,200,80,50);
c.add(l3);

l4 = new JLabel("DOB");
l4.setBounds(20,300,80,50);
c.add(l4);

l4 = new JLabel("ADDRESS");
l4.setBounds(20,400,80,50);
c.add(l4);

t1= new JTextField();
t1.setBounds(105,50,150,50);
c.add(t1);

t2= new JTextField();
t2.setBounds(105,130,150,50);
c.add(t2);

r1= new JRadioButton("Male");
r1.setBounds(105,200,80,50);
c.add(r1);

r2= new JRadioButton("Female");
r2.setBounds(200,200,80,50);
c.add(r2);

gender = new ButtonGroup();
gender.add(r1);
gender.add(r2);

String[] day ={"1","2","3","4","5","6","7","8"};
c1 = new JComboBox(day);
c1.setBounds(120,300,50,30);
c.add(c1);

String[] month ={"1","2","3","4","5","6","7","8","9","10","11","12"};
c2 = new JComboBox(month);
c2.setBounds(200,300,50,30);
c2.setSelectedIndex(5);
c.add(c2);


String[] year ={"1999","2000","2001","2002","2003","6","7","8"};
c3 = new JComboBox(year);
c3.setBounds(280,300,80,30);
c.add(c3);
c3.setSelectedIndex(3);

a1 = new JTextArea();
a1.setBounds(105,400,280,70);
c.add(a1);

b = new JCheckBox();
b.setText("accept terms and conditions");
b.setBounds(105,480,250,50);
c.add(b);

btn = new JButton("SUBMIT");
btn.setBounds(125,540,90,30);
c.add(btn);

msg = new JLabel("Live Result");
msg.setBounds(105,600,300,30);
c.add(msg);

screen = new JTextArea();
screen.setBounds(450,50,400,600);
screen.setEnabled(false);
c.add(screen);


btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(b.isSelected()){
screen.setEnabled(true);
String name = t1.getText();
String number = t2.getText();

//try{String number = t2.getText();}
//catch(NumberFormatException en){screen.setText("use numbers");}
String gen = "Male";
if(r2.isSelected()){
gen="Female";
}
String address = a1.getText();
String dob = c1.getSelectedItem()+"-"+c2.getSelectedItem()+"-"+c3.getSelectedItem()+"-";
screen.setText("Name: "+name +"\n"+"Mobile Number: "+number+"\n"+"Gender: "+gen+"\n"+"Date of Birth: "+dob+"\n"+"Address: "+address+"\n"+"---SAVED---");

msg.setText("succesfull");
}else{
msg.setText("Please accept our terms & conditions");
screen.setText("Try again");
}
}
}
class Registration{
public static void main(String[] args) {
MyFrame f = new MyFrame();
f.setTitle("Registration form");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(900,700);
f.setLocationRelativeTo(null);
f.setVisible(true);
}

}

14th program JMENU and Items:

import javax.swing.*;
import java.awt.*;

class Menu{
public static void main(String[] args) {
JFrame f = new JFrame("MenuBar Demonstration");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100,70,500,400);
JMenuBar bar = new JMenuBar();

JMenu file = new JMenu("File");
JMenuItem i1 = new JMenuItem("New");
JMenuItem i2 = new JMenuItem("Open");
JMenuItem i3 = new JMenuItem("Save");
file.add(i1);
file.add(i2);
file.add(i3);

bar.add(file);
JMenu insert = new JMenu("Insert");
JMenuItem i4 = new JMenuItem("Picture");
JMenuItem i5 = new JMenuItem("PDF");
insert.add(i4);
insert.add(i5);
file.add(insert);
//bar.add(insert);
f.setJMenuBar(bar);

JMenu select = new JMenu("Select");
JMenuItem i6 = new JMenuItem("me");
JMenuItem i7 = new JMenuItem("you");
select.add(i6);
select.add(i7);

bar.add(select);
f.setVisible(true);
}

}








Comments

Popular posts from this blog

Java Program to find average of Three numbers

Java Program any letter to upper case Letter(JavaSWING) ActionListener