Showing posts with label Swing. Show all posts
Showing posts with label Swing. Show all posts

Thursday, February 24, 2011

CheckBox ItemListener

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

class CheckBox extends JFrame implements ItemListener
{
    JCheckBox ch1,ch2;
    JPanel p1,p2;
    Container conn;
 
    CheckBox()
    {    
        conn=getContentPane();
        conn.setBackground(Color.WHITE);
   
        p1=new JPanel();p1.setBackground(Color.yellow);   
        p2=new JPanel();p2.setBackground(Color.red);   
     
        ch1=new JCheckBox("Blue",false);ch1.setOpaque(false);
        ch2=new JCheckBox("Red",false);ch2.setOpaque(false);

        conn.add(ch1);conn.add(ch2);
  
        ch1.addItemListener(this);ch2.addItemListener(this);
  
    }
 
    public void itemStateChanged(ItemEvent e )
    {
        if (ch1.isSelected()==true) 
        {
            conn.add(p1);p1.setBounds(30,40,50,30);
            this.repaint();    
        }
        if(ch1.isSelected()==false) 
        {
            conn.remove(p1);p1.setBounds(30,40,50,30);
            this.repaint();    
        }
        if (ch2.isSelected()==true) 
        {
            conn.add(p2);p2.setBounds(100,40,50,30);
            this.repaint();
        }
   if (ch2.isSelected()==false) 
        {
            conn.remove(p2);p2.setBounds(100,40,50,30);
            this.repaint();
        }
    } 
  
    public static void main(String[] args) 
    {
        JFrame win = new CheckBox();
        win.setTitle("CheckBox");
        win.setSize(200,120);
        win.setLayout(new FlowLayout());
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setVisible(true);
    }  
}

Output


RadioButton ItemListener

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

class Radiobutton extends JFrame implements ItemListener
{
    JRadioButton first,second,third,fourth;
    JPanel p1,p2,p3,p4;
    Container conn;
 
    Radiobutton()
    {    
        conn=getContentPane();
        conn.setBackground(Color.ORANGE);
   
        p1=new JPanel();
        p1.setBackground(Color.blue);
   
        p2=new JPanel();
        p2.setBackground(Color.red);
   
        p3=new JPanel();
        p3.setBackground(Color.black);
   
        p4=new JPanel();
        p4.setBackground(Color.yellow);
   
   
        first = new JRadioButton("Blue");
        second = new JRadioButton("red");
        third = new JRadioButton("Black");
        fourth = new JRadioButton("Yellow");
 
    
        //Add on Frame
        conn.add(first);conn.add(second);conn.add(third);conn.add(fourth);
  
  
        ButtonGroup bg = new ButtonGroup();
        bg.add(first);bg.add(second);bg.add(third);bg.add(fourth);
  
        first.addItemListener(this);
        second.addItemListener(this);
        third.addItemListener(this);
        fourth.addItemListener(this);
  
    }
 
    public void itemStateChanged(ItemEvent e )
    {
        if (first.isSelected()) 
        {
            conn.add(p1);
            conn.remove(p2);conn.remove(p3);conn.remove(p4);
            p1.setBounds(40,60,30,30);
            this.repaint();
    
        }
        if (second.isSelected()) 
        {
            conn.add(p2);
            conn.remove(p1);conn.remove(p3);conn.remove(p4);
            p2.setBounds(100,60,30,30);
            this.repaint();
        }
        if (third.isSelected()) 
        {
            conn.add(p3);
            conn.remove(p2);conn.remove(p1);conn.remove(p4);
            p3.setBounds(160,60,30,30);
            this.repaint();
        }
        if (fourth.isSelected()) 
        {
            conn.add(p4);
            conn.remove(p2);conn.remove(p3);conn.remove(p1);
            p4.setBounds(220,60,30,30);
            this.repaint();
        }
   
    } 
    public static void main(String[] args) 
    {
        JFrame win = new Radiobutton();
  
        win.setTitle("Radiobutton");
        win.setSize(300,200);
        win.setLayout(new FlowLayout());
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setVisible(true);
    }  
}
 
Output


Tuesday, January 4, 2011

RadioButton

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

class Radiobutton extends JFrame
{
 JRadioButton first,second,third,fourth,fifth;
 Radiobutton()
 {    
     first = new JRadioButton("First");
     second = new JRadioButton("Second");
     third = new JRadioButton("Third");
     fourth = new JRadioButton("Fourth");
     fifth = new JRadioButton("Fifth");
    
     //Add on Frame
     add(first);
     add(second);
     add(third);
     add(fourth);
     add(fifth);
  
     ButtonGroup bg = new ButtonGroup();
     bg.add(first);
     bg.add(second);
     bg.add(third);
     bg.add(fourth);
     bg.add(fifth); 
 }
  
 public static void main(String[] args) 
 {
     JFrame win = new Radiobutton();
  
     win.setTitle("Radiobutton");
     win.setSize(400,100);
     win.setLayout(new FlowLayout());
     win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     win.setVisible(true);
 }  
}


Output

CheckBox


import javax.swing.*;

class CheckBox extends JFrame
{
    JCheckBox ch1,ch2,ch3,ch4;
 
 CheckBox()
 {    
     ch1=new JCheckBox("Cricket",false);
     ch2=new JCheckBox("Football",false);
     ch3=new JCheckBox("Baseball",false);
     ch4=new JCheckBox("Swimming",false);  
     add(ch1);add(ch3);add(ch4);add(ch2);  
 }
  
 public static void main(String[] args) 
 {
     JFrame win = new CheckBox();
   
     win.setTitle("CheckBox");
     win.setSize(400,100);
     win.setLayout(new FlowLayout());
     win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     win.setVisible(true);
 }  
}


Output

Saturday, January 1, 2011

Add Label

import javax.swing.*;

class Label extends JFrame
{
   JLabel l;
 
   Label()
   {    
       add(l=new JLabel("Have a Nice Day"));
       l.setBounds(30,60,100,20);
   }
  
   public static void main(String[] args) 
   {
      JFrame win = new Label();
   
      win.setTitle("Label");
      win.setSize(200,200);
      win.setLayout(null);
      win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      win.setVisible(true);
   }  
}

Output

Create Button on Frame

import javax.swing.*;

class Button extends JFrame
{
   JButton b1;
 
   Button()
   {    
      add(b1=new JButton("ok"));
      b1.setBounds(60,60,50,50);
   }
  
   public static void main(String[] args) 
   {
      JFrame win = new Button();
   
      win.setTitle("Button");
      win.setSize(200,200);
      win.setLayout(null);
      win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      win.setVisible(true);
   }  
}

Output

Tuesday, December 28, 2010

Create a Frame

import javax.swing.*;

class Frame1 extends JFrame
{
    public static void main(String s[])
    {

        JFrame frame=new JFrame();
        frame.setSize(200,200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);      
    }

}
 
Output