Friday, January 31, 2014

Example of JTextArea in java.

import java.awt.*;
import javax.swing.*;
 
public class TArea
{
    JTextArea area;
    JFrame f;
   
TArea()
{
f=new JFrame();
         
area=new JTextArea(300,300);
area.setBounds(30,30,300,300);
     
area.setBackground(Color.black);
area.setForeground(Color.white);
         
f.add(area);
     
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
    public static void main(String surat[])
{
new TArea();
    }
}

Output:

Thursday, January 30, 2014

Example of JClorChooser in java.

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

class mahi1 implements ActionListener
{
JFrame f;
JButton b;
JPanel p;
mahi1(String s)
{
f=new JFrame(s);

b=new JButton("Color");
b.setBackground(Color.yellow);
b.addActionListener(this);
f.add(b,BorderLayout.NORTH);

p = new JPanel();
p.setLayout(new FlowLayout());
f.add(p);

f.setResizable(false);
f.setSize(300,200);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Color bg = JColorChooser.showDialog(f,"Choose background color", Color.white);
       
if(bg != null)
{
               p.setBackground(bg);
               f.getContentPane().setBackground(bg);
        }
}
public static void main(String surat[])
{
new mahi1("MaHi1");
}
}

Output:

Wednesday, January 29, 2014

Image on Frame Java.

import java.awt.*;
import javax.swing.*;
public class imageOnFrame extends JFrame
{
    public imageOnFrame()
{
JFrame frame = new JFrame("MaHi");

ImageIcon icon = new ImageIcon("socials.png");
        JLabel l1 = new JLabel(icon,JLabel.CENTER);
       
        add(l1);

setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String surat[])
{
new imageOnFrame();
    }
}

Output:

Example of image button in java frame.

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

public class imageButton
{
imageButton()
{
JFrame f=new JFrame("MaHi");

JButton b=new JButton(new ImageIcon("socials.png"));
b.setBounds(110,140,142,71);
f.add(b);

JLabel l=new JLabel();
l.setText("Social Image Button");
l.setBounds(120,60,150,30);
f.add(l);

f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
public static void main(String surat[])
{
new imageButton();
}
}

Output:

Change Default icon of the Java Frame.

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

public class SettingIconFrame
{
public static void main(String surat[])
{
JFrame f = new JFrame("MaHi");

f.setIconImage(Toolkit.getDefaultToolkit().getImage("socials.png"));

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200,100);
f.setVisible(true);
}
}

Output:

Tuesday, January 28, 2014

Example of JProgressBar in Java.

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

public class MyProgress extends JFrame
{
JProgressBar pb;
int i=0, n=0;

MyProgress()
{
super("ProgressBar");
pb=new JProgressBar();
pb.setBounds(40,100,200,30);

pb.setValue(0);
pb.setStringPainted(true);

Border b = BorderFactory.createTitledBorder("DownLoding...");
pb.setBorder(b);

add(pb, BorderLayout.NORTH);
setSize(400,80);
setResizable(false);
setVisible(true);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void iterate()
{
while(i<=2000)
{
pb.setValue(i);
i=i+5;
try
{
Thread.sleep(500);
}
catch(Exception e){}
}
}
public static void main(String surat[])
{
MyProgress m=new MyProgress();
m.setVisible(true);
m.iterate();
}
}

Output:

Java Example using HTML Tags.

import java.awt.*;
import javax.swing.*;
class HtmlJava
{
JFrame f;
HtmlJava()
{
f=new JFrame("MaHi");

Font f1=new Font ("Sherif",Font.PLAIN,50);
f.setFont(f1);

String s="<html> I am Me "
+" <p> " +
"My Friends are : "+
"<ul>" +
"<li> Bipin"+
"<li> Jignesh"+
"<li> Ashish"+
"<li> Satish"+
"<li> Dhanno"+
"<li> etc."+
"</ul></html>";

JLabel l1=new JLabel(s,JLabel.CENTER);
f.add(l1);

f.setLayout(new GridLayout(1,1));
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String surat[])
{
new HtmlJava();
}
}

Output:

Use of HTML Tags in Java

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

public class HtmlTagsInJava extends JFrame
{
public static void main(String surat[])
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
catch(Exception e)
{
System.out.println(e);
}
new HtmlTagsInJava();
}
public HtmlTagsInJava()
{
super("Using HTML in JLabel");

Container c=getContentPane();

Font f=new Font("Serif",Font.PLAIN,35);
c.setFont(f);

String LabelText="<html> <font color=red> Red </font> and"
+"<font color=blue> Blue </font> Text </html>";

JLabel coloredLabel=new JLabel(LabelText,JLabel.CENTER);

coloredLabel.setBorder(BorderFactory.createTitledBorder("Mixed colors"));

c.add(coloredLabel,BorderLayout.NORTH);

LabelText="<html> <b> Bold </b> and" + "<i> Italic </i> Text </html>";

JLabel boldLabel=new JLabel(LabelText,JLabel.CENTER);

boldLabel.setBorder(BorderFactory.createTitledBorder("Mixed Fonts"));

c.add(boldLabel,BorderLayout.CENTER);

LabelText= "<html> India is good Country " + "<p> " +
"Major Cities of India : " +
"<UL>" +
"<li> Delhi " +
"<li> Surat " +
"<li> Mumbai " +
"<li> Banglore " +
"<li> Jaipur " +
"<li> etc " +
"</ul>";

JLabel fancyLabel=new JLabel(LabelText,JLabel.CENTER);

fancyLabel.setBorder(BorderFactory.createTitledBorder("Multi Line Html"));

c.add(fancyLabel,BorderLayout.SOUTH);
setSize(400,400);
setVisible(true);
}
}

Output:

Monday, January 27, 2014

Example Of CheckBox in java.

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

public class CheckBoxEx extends JFrame  implements ItemListener
{
JCheckBox c1,c2,c3;
JTextField t1;

CheckBoxEx()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());

c1=new JCheckBox("Check1");
c2=new JCheckBox("Check2");
c3=new JCheckBox("Check3");

c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);

c.add(c1);
c.add(c2);
c.add(c3);

t1=new JTextField(20);
c.add(t1);

setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getItemSelectable()==c1)
t1.setText("You clicked CheckBox 1");
if(e.getItemSelectable()==c2)
t1.setText("You clicked CheckBox 2");
if(e.getItemSelectable()==c3)
t1.setText("You clicked CheckBox 3");
}
public static void main(String surat[])
{
new CheckBoxEx();
}
}

Output:

Example of Dimension in Java.

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

public class DimensionDemo
{
    public static void main(String surat[])
{
                JFrame f = new JFrame("Dimension");

                Container c = f.getContentPane();
                c.setLayout(new FlowLayout());
                c.add(new JButton("Button A"));

                JButton buttonB = new JButton("Button B");
                buttonB.setPreferredSize(new Dimension(200, 40));
                c.add(buttonB);
                c.add(new JButton("Button C"));
                c.add(new JButton("Button D"));

                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(400, 200);
                f.setVisible(true);
    }
}

Output:

Saturday, January 25, 2014

Example of Frame Location in java.

////This will center the Frame in the middle of the screen.

import java.awt.*;
class mahi1
{
mahi1()
{
Frame f=new Frame("MaHi");
Label l1=new Label();
l1.setText("Best since day one");
l1.setAlignment(Label.CENTER);
f.add(l1);

f.setSize(300,300);
f.setVisible(true);
f.setLayout(new GridLayout(1,1));
f.setLocationRelativeTo(null);
}
public static void main(String surat[])
{
new mahi1();
}
}

Output:

Friday, January 24, 2014

Example of BoxLayout in java.

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

class MyBoxLayout extends Frame
{
Button b[];

MyBoxLayout()
{
b=new Button[5];

for(int i=0;i<5;i++)
{
b[i]=new Button("Button "+ (i+1));
add(b[i]);
}
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
setSize(300,300);
setVisible(true);
}
public static void main(String surat[])
{
new MyBoxLayout();
}
}

Output:

Example of CardLayout in java.

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

class MyCardLayout extends JFrame implements ActionListener
{
CardLayout card;
JButton b1,b2,b3;
Container c;

MyCardLayout()
{
c=getContentPane();
card=new CardLayout(5,3);
c.setLayout(card);

b1=new JButton("Mahi");
b2=new JButton("Piyu");
b3=new JButton("Shilu");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

c.add("A",b1);
c.add("B",b2);
c.add("C",b3);

setSize(400,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
card.next(c);
}
public static void main(String surat[])
{
new MyCardLayout();
}
}

Output:

Example of GridLayout in java.

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

class MyGridLayout
{
JFrame f;
MyGridLayout()
{
f=new JFrame("MaHi");

JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b11=new JButton("*");
JButton b0=new JButton("0");
JButton b22=new JButton("#");

f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(b11);
f.add(b0);
f.add(b22);

f.setLayout(new GridLayout(4,3));
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
public static void main(String surat[])
{
new MyGridLayout();
}
}

Output:

Example of FlowLayout in java.

import java.awt.*;
import javax.swing.*;
class MyFlowLayout
{
JFrame f;
MyFlowLayout()
{
f=new JFrame();

JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");

f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);

f.setLayout(new FlowLayout(FlowLayout.RIGHT));

f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
public static void main(String surat[])
{
new MyFlowLayout();
}
}

Output:

Example of BorderLayout in java.

import java.awt.*;
import javax.swing.*;
class MyBorderLayout
{
JFrame f;
MyBorderLayout()
{
f=new JFrame();

JButton b1=new JButton("NORTH");
JButton b2=new JButton("SOUTH");
JButton b3=new JButton("EAST");
JButton b4=new JButton("WEST");
JButton b5=new JButton("CENTER");

f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);
f.add(b5,BorderLayout.CENTER);

f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
public static void main(String surat[])
{
new MyBorderLayout();
}
}

Output:

Example of getSource() Event Handling in Java.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class mahi1 implements ActionListener
{
JFrame f;
JButton b1,b2,b3;
Label l1,l2;
Panel p;

mahi1()
{
f=new JFrame("MaHi");
p=new Panel();
f.setLayout(new GridLayout(1,1));
f.setSize(400,400);
f.setVisible(true);

l1=new Label();
l1.setAlignment(Label.CENTER);
l1.setText("Students of LCVIT");
f.add(l1);

b1=new JButton("MaHi");
b2=new JButton("Bips");
b3=new JButton("Anju");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

p.add(b1);
p.add(b2);
p.add(b3);
f.add(p);

l2=new Label();
l2.setAlignment(Label.CENTER);
l2.setSize(350,100);
f.add(l2);

f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
l2.setText("Mahi comes in Action");
if(e.getSource()==b2)
l2.setText("Bipin comes in Action");
if(e.getSource()==b3)
l2.setText("Anjali comes in Action");
}
public static void main(String surat[])
{
new mahi1();
}
}


Example of setActionCommand and getActionCommand in java.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class mahi1 implements ActionListener
{
JFrame f;
Label l1,l2;
JButton b1,b2,b3;
Panel p;
mahi1()
{
f=new JFrame("MaHi");
f.setSize(400,400);
f.setLayout(new GridLayout(3, 1));
f.setVisible(true);
l1=new Label();
l1.setText("Press Any Key");
l1.setAlignment(l1.CENTER);
f.add(l1);
p = new Panel();
p.setLayout(new FlowLayout());
b1=new JButton("Ok");
b2=new JButton("Submit");
b3=new JButton("Cancel");
b1.setActionCommand("OK");
b2.setActionCommand("Submit");
b3.setActionCommand("Cancel");
b1.addActionListener(this); 
b2.addActionListener(this); 
b3.addActionListener(this);
p.add(b1);
p.add(b2);
p.add(b3);
f.add(p);
l2 = new Label();        
l2.setAlignment(Label.CENTER);
l2.setSize(350,100);
f.add(l2);
}
public void actionPerformed(ActionEvent e)
{
        String command = e.getActionCommand();  
        if( command.equals( "OK" ))
{
l2.setText("Ok Button clicked.");
        }
        else if( command.equals( "Submit" ) )
{
l2.setText("Submit Button clicked.");
}
        else
{
            l2.setText("Cancel Button clicked.");
        }
    }
public static void main(String surat[])
{
new mahi1();
}
}

Output:

Example of label alignment in java awt.

import java.awt.*;
import java.awt.event.*;
class mahi1
{
Frame f;
Label l,k;
Panel p;
mahi1()
{
f=new Frame("Mahi");
f.setLayout(new GridLayout(3, 2));
l=new Label();
l.setAlignment(l.CENTER);
k=new Label();
k.setAlignment(k.CENTER);
p=new Panel();
p.setLayout(new FlowLayout());
f.add(l);
f.add(p);
f.add(k);
f.setSize(400,200);
f.setVisible(true);
}
public void mahi2()
{
k.setText("MaHi");
Label l=new Label();
l.setText("Kem chho?");
l.setAlignment(l.CENTER);
l.setBackground(Color.pink);
l.setForeground(Color.black);
p.add(l);
f.setVisible(true);
}
public static void main(String surat[])
{
mahi1 m=new mahi1();
m.mahi2();
}
}


Output:

Thursday, January 23, 2014

Java awt exit(close) example.

import java.awt.*;
import java.awt.event.*;

class exitPro
{
exitPro()
{
Frame f=new Frame();
f.setSize(300,300);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}      
});
}
public static void main(String surat[])
{
new exitPro();
}
}

Frame button background colour example.

import java.awt.*;
import java.awt.event.*;

class ChangeButtonBackground
{
public static void main(String surat[])
{
Frame f=new Frame("ChangeButtonBackground");
Button b = new Button("Color Button");
b.setBackground(Color.yellow);
f.add(b,BorderLayout.NORTH);
f.pack();
f.setSize(300,300);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}

Output:

Wednesday, January 22, 2014

Example of no effect on frame Size.

import java.awt.*;
class mahi
{
mahi(String s)
{
Frame f=new Frame(s);
f.setSize(300,300);
f.setResizable(false);
f.setVisible(true);
}
public static void main(String surat[])
{
new mahi("MaHi");
}
}

Output:

Real example of toString() method in java.

class ToStringEx1
{
int rollno;
String name;
String city;
 
ToStringEx1(int rollno, String name, String city)
{
this.rollno=rollno;
this.name=name;
this.city=city;
}

public String toString()
{
return rollno+" "+name+" "+city;
}
 
public static void main(String surat[])
{
ToStringEx1 s1=new ToStringEx1(1,"Mahi","Surat");
ToStringEx1 s2=new ToStringEx1(2,"Kinjal","Ahmedabad");
   
System.out.println(s1);//compiler writes here s1.toString()
System.out.println(s2);//compiler writes here s2.toString()
}
}

Output:

Simple Example of toString() method in java.

class ToStringEx
{
int rollno;
String name;
String city;
 
ToStringEx(int rollno, String name, String city)
{
this.rollno=rollno;
this.name=name;
this.city=city;
}
 
public static void main(String surat[])
{
ToStringEx s1=new ToStringEx(1,"Mahi","Surat");
ToStringEx s2=new ToStringEx(2,"Kinjal","Ahmedabad");
   
System.out.println(s1);
System.out.println(s2);
}
}

Output:

Example of trim() method in java.

class TRIM
{
public static void main(String surat[])
{
String s="  MaHi  ";
System.out.println(s);
System.out.println(s.trim());
}
}

Output:

Event Handling (Example 3)

import java.awt.*;
import java.awt.event.*;
class kali implements ActionListener
{
Frame f;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
TextField t;
kali(String s)
{
f=new Frame(s);
t=new TextField();
t.setBounds(100,80,260,30);
f.add(t);
b1=new Button("MaHi");
b2=new Button("Bips");
b3=new Button("Swami");
b4=new Button("Ashu");
b5=new Button("Jigs");
b6=new Button("Dhanno");
b7=new Button("Anil");
b8=new Button("Piyu");
b9=new Button("Bhavu");

b1.setBounds(100,140,60,40);
b2.setBounds(100,220,60,40);
b3.setBounds(100,300,60,40);
b4.setBounds(200,140,60,40);
b5.setBounds(200,220,60,40);
b6.setBounds(200,300,60,40);
b7.setBounds(300,140,60,40);
b8.setBounds(300,220,60,40);
b9.setBounds(300,300,60,40);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);

f.setLayout(null);
f.setSize(500,420);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
t.setText("Good Wise and Kind Person");
if(e.getSource()==b2)
t.setText("BD ki Deepika");
if(e.getSource()==b3)
t.setText("Khodamba Ka vir Bikshuk");
if(e.getSource()==b4)
t.setText("Appu ka Big Brother and Appa Love");
if(e.getSource()==b5)
t.setText("Ghaghra no big Dealer");
if(e.getSource()==b6)
t.setText("IANT ni Ramayan no Hero");
if(e.getSource()==b7)
t.setText("Khovayeli Duniya no king");
if(e.getSource()==b8)
t.setText("BakBak Leader");
if(e.getSource()==b9)
t.setText("Sagai pachi ni GulliMaru");
}
public static void main(String surat[])
{
new kali("Kali");
}
}

Output:

Event Handling Example(2)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Event3 implements ActionListener
{
JFrame f;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
TextField t;
Event3(String s)
{
f=new JFrame(s);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
t=new TextField();
t.setBounds(100,40,280,40);
f.add(t);

b1=new Button("1");
b1.setBounds(100,100,40,40);

b2=new Button("2");
b2.setBounds(180,100,40,40);

b3=new Button("3");
b3.setBounds(260,100,40,40);

b4=new Button("4");
b4.setBounds(340,100,40,40);

b5=new Button("5");
b5.setBounds(140,180,40,40);

b6=new Button("6");
b6.setBounds(220,180,40,40);

b7=new Button("7");
b7.setBounds(300,180,40,40);

b8=new Button("8");
b8.setBounds(180,260,40,40);

b9=new Button("9");
b9.setBounds(260,260,40,40);

b0=new Button("0");
b0.setBounds(220,340,40,40);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);

f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(b0);

f.setLayout(null);
f.setSize(500,500);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
t.setText("1");
if(e.getSource()==b2)
t.setText("2");
if(e.getSource()==b3)
t.setText("3");
if(e.getSource()==b4)
t.setText("4");
if(e.getSource()==b5)
t.setText("5");
if(e.getSource()==b6)
t.setText("6");
if(e.getSource()==b7)
t.setText("7");
if(e.getSource()==b8)
t.setText("8");
if(e.getSource()==b9)
t.setText("9");
if(e.getSource()==b0)
t.setText("0");
}
public static void main(String surat[])
{
new Event3("MaHi");
}
}

Output:

Event Handling Example:1 (textField)

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

class Event2 implements ActionListener
{
JFrame f;
Button b1,b2;
TextField t;
Event2(String s)
{
f=new JFrame(s);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

b1=new Button("Ok");
b2=new Button("Cancel");

b1.setBounds(20,100,50,40);
b2.setBounds(20,180,50,40);

b1.addActionListener(this);
b2.addActionListener(this);

f.add(b1);
f.add(b2);

t=new TextField();
t.setBounds(20,40,250,40);
f.add(t);

f.setLayout(null);
f.setSize(400,400);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
t.setText("Okyzzz");
if(e.getSource()==b2)
t.setText("Cancelzz");
}
public static void main(String surat[])
{
new Event2("MaHi");
}
}

Output: