Thursday, August 22, 2013

PROGRAM FOR RADIO BUTTON

PROGRAM:
~~~~~~~~~~~
import javax.swing.*;
import java.awt.*;

class radio extends JFrame
{
private static final long serialVersionUID = 1L;
Container con;
JRadioButton r1,r2;
ButtonGroup bg;

radio()
{
con=getContentPane();
con.setLayout(null);
r1=new JRadioButton("Male");
r2=new JRadioButton("Female");
bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);
r1.setBounds(200,200,90,30);
r2.setBounds(200,250,90,30);
con.add(r1);
con.add(r2);
}

public static void main(String[] arg)
{
radio obj=new radio();
obj.setVisible(true);
obj.setSize(400,400);
obj.setTitle("This is my frame");
}
}

PROGRAM FOR COMBOBOX

PROGRAM:
~~~~~~~~~~~
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class JCombo extends JApplet implements ItemListener
{
public void init()
{
String country[]={"India","Pakistan","Austrailia","Europe","SouthAfrica"};
setLayout(new FlowLayout());
               
JLabel jl=new JLabel("SELECT  CONTRY");
JComboBox jcb=new JComboBox(country);
jcb.addItemListener(this);
                add(jl);
add(jcb);
}
public void itemStateChanged(ItemEvent ie)
{
}

}

PROGRAM FOR JLIST

PROGRAM:
````````````````
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

import java.awt.event.*;


public class jlist extends JApplet implements ListSelectionListener
{
JList jlist;
String cities[]={"New York","Bangalore", "Mumbai","Chennai", "Delhi","Tokyo"};
JLabel jl=new JLabel("Select an item");
public void init()
{
setLayout(new GridBagLayout());
jlist=new JList(cities);
jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane jscp=new JScrollPane(jlist);
add(jscp);
add(jl);
}

@Override
public void valueChanged(ListSelectionEvent le)
{
int index=jlist.getSelectedIndex();
if(index!=-1)
{
// jl.setText("Current selection is "+index+".");
showStatus("Current selection is "+cities[index]+".");
}
else
jl.setText("Choose a city.");

}
}

OUTPUT:
~~~~~~~


Wednesday, August 21, 2013

PROGRAM FOR CHECK BOX

import javax.swing.*;
import java.awt.*;
class check extends JFrame
{
Container con;
JCheckBox c1,c2;
     check( )
{
  con=getContentPane();
         con.setLayout(null);
          JLabel jl=new JLabel("HOBBIES");
         jl.setBounds(100,80,90,30);
c1=new JCheckBox("reading")
        c2=new JCheckBox("writing");
        c1.setBounds(100,100,90,30);
c2.setBounds(100,150,90,30);
        con.add(c1);
  con.add(c2);
  con.add(jl);

}
   public static void main(String args[]) throws Exception
{
    check obj=new check();
     obj.setVisible(true);
    obj.setSize(400,400);
    obj.setTitle("this is my Frame");

}
}

OUTPUT:
`````````````

NOTE:
It uses main method so it supports only the standalone application. 

Tuesday, August 20, 2013

PROGRAM FOR JTOGGLE BUTTON

JTOGGLE BUTTON:
```````````````````````````
Here ItemListener interface is used instead of ActionListener
Here we use ItemEvent\ instead of ActionEvent.
Here we use itemStateChanged is used instead of  ActionListener method.

STATES OF TOGGLE BUTTON
`````````````````````````````````````````````
1) Pressed state
2)Release state

PROGRAM:
````````````````
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class toggle extends JApplet implements ItemListener
{

JToggleButton jtb=new JToggleButton("ON/OFF");
JLabel jl= new JLabel("PRESS THE BUTTON");
public void init()
{
setLayout(new FlowLayout());
add(jl);
add(jtb);
showStatus("PRESS A BUTTON");//to display the given msg in status bar
jtb.addItemListener(this);

}


public void itemStateChanged(ItemEvent arg0)
{
if(jtb.isSelected())
{
jl.setText("BUTTON PRESSED");//replace the label by this message using setText() after the button is toggled
showStatus("BUTTON PRESSED");//displays the message in statusbar after the button is toggled
}
}
}

OUTPUT:
SCREEN: BEFORE BUTTON PRESSED

SCREEN: AFTER BUTTON PRESSED

Sunday, August 18, 2013

APPLET PROGRAM TO ADD TWO INTEGERS

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

    public class AppletSum extends Applet implements ActionListener{
      TextField text1,text2,output;
      Label label1,label2,label3;
      Button button;
      public void init(){
        setLayout(null);
        label1 = new Label("Enter Number1: ");
        label1.setBounds(20,20,100,20);
        add(label1);

        text1 = new TextField(5);
        text1.setBounds(150,20,100,20);
        add(text1);

        label2 = new Label("Enter Number2: ");
        label2.setBounds(20,50,100,20);
        add(label2);

        text2 = new TextField(5);
        text2.setBounds(150,50,100,20);
        add(text2);

        label3 = new Label("Sum of Two Numbers: ");
        label3.setBounds(20,80,130,20);
        add(label3);

        output = new TextField(5);
        output.setBounds(150,80,100,20);
        add(output);

        button = new Button("Sum");
        button.setBounds(150,110,100,20);
        add(button);

        button.addActionListener(this);
        }
        public void actionPerformed(ActionEvent ae){
        int num1=Integer.parseInt(text1.getText());
        int num2=Integer.parseInt(text2.getText());
        int sum=num1+num2;
        output.setText(Integer.toString(sum));
        }
    }

OUTPUT:
~~~~~~~

Saturday, August 17, 2013

PROGRAM TO CREATE BUTTON


PROGRAM:
~~~~~~~~~
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class A extends JApplet implements ActionListener
{
public void init()
{
   
    JButton jb=new JButton("SUBMIT");
    setLayout(new FlowLayout());
   
    add(jb);
    setVisible(true);
}

    public void actionPerformed(ActionEvent e)
    {
   
    }  
 }
NOTE:
~~~~~

add(jlb)--> To add the component Button to the container.
init( )--> Life cycle function of the Applet
setLayout--> Type in Layout Manager.Responsible to display components in manner
JApplet-->Predefined class in Java
setVisible(true)-->to make the components of the container visible in screen
ActionListener-->Interface in the java.awt.event.*; package
   public void actionPerformed(ActionEvent e)-->Abstract method in ActionListener Interface


OUTPUT:
~~~~~~~


PROGRAM FOR DISPLAY TEXTBOX AND LABEL

PROGRAM:
~~~~~~~~~
import javax.swing.*;
import java.awt.*;

public class A extends JApplet
{
public void init()
{
    JLabel jl=new JLabel("NAME");//to create Label
    JTextField jt=new JTextField(10);//to create textbox.Allows only 10 characters
    setLayout(new FlowLayout());
    add(jl);
    add(jt);
    setVisible(true);
}

       }

NOTE:
~~~~~
add(jl)--> To add the component Label to the container.
init( )--> Life cycle function of the Applet
setLayout--> Type in Layout Manager.Responsible to display components in manner
JApplet-->Predefined class in Java
setVisible(true)-->to make the components of the container visible in screen

 OUTPUT:
~~~~~~~

Monday, August 12, 2013

EXAMPLE GUI PROGRAM

NOTE: Standalone application has void main( ) but Applet swing doesnot consist it.

PROGRAM:
~~~~~~~~~~

import javax.swing.*;

public class A extends JApplet
{

public static void main(String args[])

{

JLabel jl=new JLabel("MY FIRST SWING APPLICATION");

JFrame jf=new JFrame("SWING FRAME");

jf.setSize(500,500);

jf.setVisible(true);

jf.add(jl);


}

}

OUTPUT:


Animated Social Gadget - Blogger And Wordpress Tips