Java Applets & Layout Managers



//First Program of applet

 

import java.applet.*;

import java.awt.*;

 

/*

                <applet code = "myapplet" width = 300 height = 400></applet>

*/

public class myapplet extends Applet

{

                public void paint(Graphics g)

                {

                                g.drawString("This is Applet", 100, 100);

                }

}

OUTPUT:


*******************************************************
//Drawshape in java

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

/*
<applet code = "rectangle" width = 500 height = 500></applet>
*/

public class rectangle extends Applet
{
public void paint(Graphics g)
{
g.drawRect(50, 50, 200, 200);
g.setColor(Color.blue);
g.fillRect(250, 250, 50, 50);
}
}
OUTPUT:


******************************************************
//Passing parameter to an appplet

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

/*
<applet code = "passing_parameter_in_applet.class" width = 400 height = 400>
<param name = "message" value = "Applete with parameter ">
<param name = "forebackground" value = "#fff00">
</applet>
*/

public class passing_parameter_in_applet extends Applet
{
String msg, fore;

public void init()
{
msg = getParameter ("message");
fore = getParameter("forebackground");
}
public void paint(Graphics g)
{
g.setColor(Color.decode(fore));

g.drawString(msg, 100, 100);
}
}
OUTPUT:

*******************************************************
//Applet Life Cycle
import java.applet.*;
import java.awt.*;
/*
<applet code = "Applet_Life_Cycle" width = 400 height = 400>
</applet>
*/

public class Applet_Life_Cycle extends Applet
{
String msg;

public void init()
{
msg  = "init";
}
public void start()
{
msg = "start";
}
public void paint(Graphics g)
{
msg += "paint";
g.drawString(msg, 20, 100);
}
public void stop()
{
msg += "Stop";
public void destoy()
{
System.out.println("destoy");
}
}
OUTPUT:


*******************************************************

//AWT Components

 

import java.awt.*;

import java.applet.*;

 

/*

<applet code = "awtcomponent.class" width = 400 height = 400></applet>

*/

 

public class awtcomponent extends Applet

{

                Label lbl1, lbl2;

                TextField txt1, txt2;

                Button btn1;

 

                public void init()

                {

                                lbl1 = new Label("Username");

                                add(lbl1);

 

                                txt1 = new TextField(10);

                                add(txt1);

 

                                lbl2 = new Label("Password");

                                add(lbl2);

 

                                txt2 = new TextField(10);

                                add(txt2);

 

                                btn1 = new Button("Log in");

                                add(btn1);

                }

}

OUTPUT:



*******************************************************

//swing component

 

import java.awt.*;

import javax.swing.*;

 

/*

<applet code = "swing_component.class" width = 400 height = 400></applet>

*/

 

public class swing_component extends JApplet

{

                Container con;

                JLabel lbl1, lbl2;

                JTextField txt1, txt2;

                JButton btn1;

 

                public void init()

                {

                                con = getContentPane();

                                con.setLayout(new FlowLayout());

 

                                lbl1 = new JLabel("Username");

                                con.add(lbl1);

 

                                txt1 = new JTextField(10);

                                con.add(txt1);

 

                                lbl2 = new JLabel("Password");

                                con.add(lbl2);

 

                                txt2 = new JTextField(10);

                                con.add(txt2);

 

                                btn1 = new JButton("LogIn");

                                con.add(btn1);

                }

}

OUTPUT:




*******************************************************

//buttonclick_event

//ActionListener and actionPerformed

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

//<applet code = "buttonclick_event.class" width = 500 height = 500></applet>

 

public class buttonclick_event extends JApplet implements ActionListener

{

                Container con;

                JLabel lbl1;

                JTextField txt1;

                JButton btn1;

 

                public void init()

                {

                                con = getContentPane();

                                con.setLayout(new FlowLayout());

 

                                lbl1 = new JLabel("Name");

                                con.add(lbl1);

 

                                txt1 = new JTextField(10);

                                con.add(txt1);

 

                                btn1 = new JButton("Click");

                                btn1.addActionListener(this);

                                con.add(btn1); 

                }

                public void actionPerformed(ActionEvent ae)

                {

                                String nm = txt1.getText();

 

                                JOptionPane.showMessageDialog(this, " welocme " + nm);

                }

}

OUTPUT:




 


*******************************************************

// calculate square and cube using java swing.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

 //<applet code = "calculate_sq_cu.java" width = 500 height = 500></applet>

public class calculate_sq_cu extends JApplet implements ActionListener
{
Container con;
JLabel lbl1, lbl2, lbl3;
JTextField txt1, txt2, txt3;
JButton btn1;

public void init()
{
con = getContentPane();
con.setLayout(new FlowLayout());

lbl1 = new JLabel("Enter Number");
con.add(lbl1);

txt1 = new JTextField(5);
con.add(txt1);

lbl2 = new JLabel("Square");
con.add(lbl2);

txt2 = new JTextField(5);
txt2.setEditable(false);
con.add(txt2);

lbl3 = new JLabel("Cube");
con.add(lbl3);

txt3 = new JTextField(5);
txt3.setEditable(false);
con.add(txt3);

btn1 = new JButton("Answer");
btn1.addActionListener(this);
con.add(btn1);


}
public void actionPerformed(ActionEvent ae)
{
int n = Integer.parseInt(txt1.getText());

int sq = n * n;
int cu = n * n * n;

txt2.setText(sq + "");
txt3.setText(cu + ""); 
}

}
OUTPUT:


*******************************************************


//calculate Arithmetic operation.
//ActionListener interface and ActionEvent

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

 //<applet code = "calculate_arithmetic_operation" width = 500 height = 500></applet>
public class calculate_arithmetic_operation extends JApplet implements ActionListener
{
Container con;
JLabel lbl1, lbl2, lbl3;
JTextField txt1, txt2, txt3;
JButton btn1, btn2, btn3, btn4;

public void init()
{
con = getContentPane();
con.setLayout(new FlowLayout());

lbl1 = new JLabel("Number1:");
con.add(lbl1);

txt1 = new JTextField(5);
con.add(txt1);

lbl2 = new JLabel("Number2:");
con.add(lbl2);

txt2 = new JTextField(5) ;
con.add(txt2);

lbl3 = new JLabel("Answer : ");
con.add(lbl3);

txt3 = new JTextField(5);
con.add(txt3);

btn1 = new JButton(" + ");
btn1.addActionListener(this);
con.add(btn1);

btn2 = new JButton(" - ");
btn2.addActionListener(this);
con.add(btn2);

btn3 = new JButton(" * ");
btn3.addActionListener(this);
    con.add(btn3);

btn4 = new JButton(" / ");
btn4.addActionListener(this);
con.add(btn4);
}
public void actionPerformed(ActionEvent ae)
{
int a = Integer.parseInt(txt1.getText());
int b = Integer.parseInt(txt2.getText());

int c = 0;

if (ae.getSource() == btn1) 
c = a + b;
if (ae.getSource() == btn2) 
c = a - b;
if (ae.getSource() == btn3) 
c = a * b;
if (ae.getSource() == btn4) 
c = a / b;
txt3.setText(c + "");
}
}
OUTPUT:



*******************************************************
//JcheckBox class
//ItemListener interface and ItemEvent class

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

//<applet code = "jcheckbox_class" width = 500 height = 500></applet>

public class jcheckbox_class extends JApplet implements ItemListener
{
Container con;
JCheckBox chk1, chk2, chk3;
JLabel lbl1;

public void init()
{
con = getContentPane();
con.setLayout(new FlowLayout());

chk1 = new JCheckBox(" Reading ");
chk1.addItemListener(this);
con.add(chk1);

chk2 = new JCheckBox(" Writing ");
chk2.addItemListener(this);
con.add(chk2);

chk3 = new JCheckBox(" Watching ");
chk3.addItemListener(this);
con.add(chk3);

lbl1 = new JLabel("                   ");
con.add(lbl1);

}
public void itemStateChanged(ItemEvent ie)
{
String msg = "";

if (chk1.isSelected() == true)
msg += chk1.getLabel();
if (chk2.isSelected())
msg += chk2.getLabel();
if (chk3.isSelected())
msg += chk3.getLabel();
lbl1.setText(msg);
}
}
OUTPUT:


*******************************************************
//JPassword

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

//<applet code = "jpassword_class" width = 500 height = 500></applet>

public class 
 extends JApplet implements ActionListener
{
Container con;
JLabel lbl1, lbl2;
JTextField txt1;
JPasswordField p1;
JButton btn1;

public void init()
{
con = getContentPane();
con.setLayout(new FlowLayout());

lbl1 = new JLabel("Admin : ");
con.add(lbl1);

txt1 = new JTextField(10);
con.add(txt1);

lbl2 = new JLabel("Password : ");
con.add(lbl2);

p1 = new JPasswordField(10);
con.add(p1);

btn1 = new JButton("Log In");
btn1.addActionListener(this);
con.add(btn1);
}
public void actionPerformed(ActionEvent ae)
{
String user = txt1.getText();
String pass = p1.getText();

if (user.equals("Admin")) {
if (pass.equals("Admin")) {
JOptionPane.showMessageDialog(this, " welcome " + user);
}
else
{
JOptionPane.showMessageDialog(this, " Invalid Password ");

}
}
else
{
JOptionPane.showMessageDialog(this, " Invalid Username ");
}
}
}
OUTPUT:


*******************************************************
        /*****************
        *                  *
* Layout Manager    *
        *  *
        ******************/

//Flow Layout
 import java.awt.*;
 import javax.swing.*;

 //<applet code = "flowlayout.class" width = 500 height = 500></applet>

 public class flowlayout extends JApplet
 {
  Container con;
  JLabel lbl1, lbl2;
  JTextField txt1;
  JPasswordField pass1;

  public void init()
  {
  con = getContentPane();
  con.setLayout(new FlowLayout(FlowLayout.LEFT));

  lbl1 = new JLabel("User");
con.add(lbl1);

txt1 = new JTextField(10);
con.add(txt1);

lbl2 = new JLabel("Password");
con.add(lbl2);

pass1 = new JPasswordField(10);
con.add(pass1);
  }
 }
OUTPUT:



*******************************************************
//Border Layout

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

//<applet code = "borederlayout.class" width = 500 height = 500></applet>

public class borederlayout extends JApplet
{
Container con;
JButton b1, b2, b3, b4, b5;

public void  init()
{
con = getContentPane();
con.setLayout(new BorderLayout(5, 5));

b1 = new JButton("North");
con.add(b1, "North");

b2 = new JButton("West");
con.add(b2, "West");

b3 = new JButton("East");
con.add(b3, "East");

b4 = new JButton("South");
con.add(b4, "South");

b5 = new JButton("Center");
con.add(b5, "Center");
}
public Insets getINSETS()
{
return new Insets(20, 20, 20, 20);
}
}
OUTPUT:


*******************************************************
//no Layout

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

/*<applet code = "nolayout" width = 500
 height = 500></applet>
*/

 public class nolayout extends JApplet
 {
  Container con;
  JLabel lbl1, lbl2;
  JTextField txt1, txt2;
  JButton btn1;

  public void init()
  {
  con = getContentPane();
  con.setLayout(null);

  lbl1 = new JLabel("username");
  lbl1.setBounds(20, 20, 80, 20);
  con.add(lbl1);

  txt1 = new JTextField(10);
  txt1.setBounds(90, 25, 100, 20);
  con.add(txt1);

  lbl2 = new JLabel("username");
  lbl2.setBounds(20, 20, 80, 20);
  con.add(lbl2);

  txt2 = new JTextField(10);
  txt2.setBounds(90, 25, 100, 20);
  con.add(txt2);

  btn1 = new JButton("Login");
  btn1.setBounds(70, 80, 80, 30);
  con.add(btn1);
  }
 }
OUTPUT:


*******************************************************
//GridLayout

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

/*<applet code = "gridlayout" width = 500
 height = 500></applet>
*/

 public class gridlayout extends JApplet
 {
  Container con;
  JButton btn[] = new JButton[6];

  public void init()
  {
  con = getContentPane();
  con.setLayout(new GridLayout(3, 2, 5, 5));
  for (int i = 0; i < 6; i++ ) {
  btn[i] = new JButton("Button - " + i);
  con.add(btn[i]);
  }
  }
 }
OUTPUT:

*******************************************************
//BoxLayout

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

/*<applet code = "boxlayout" width = 500
 height = 500></applet>
*/

 public class boxlayout extends JApplet
 {
  Container con;
JButton b1, b2, b3;

public void  init()
{
con = getContentPane();
con.setLayout(new BoxLayout(con, BoxLayout.LINE_AXIS));

b1 = new JButton("Button -1");
con.add(b1);

b2 = new JButton("Button -2");
con.add(b2);

b3 = new JButton("Button -3");
con.add(b3);
}
 }
OUTPUT:


*******************************************************
//BoxLayout

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

/*<applet code = "boxlayout_PAGE_AXIS" width = 500
 height = 500></applet>
*/

 public class boxlayout_PAGE_AXIS extends JApplet
 {
  Container con;
JButton b1, b2, b3;

public void  init()
{
con = getContentPane();
con.setLayout(new BoxLayout(con, BoxLayout.PAGE_AXIS));

b1 = new JButton("Button -1");
con.add(b1);

b2 = new JButton("Button -2");
con.add(b2);

b3 = new JButton("Button -3");
con.add(b3);
}
 }
OUTPUT:


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

/*<applet code = "jcombobox" width = 500
 height = 500></applet>
*/

 public class jcombobox extends JApplet implements ItemListener
 {
  Container con;
  JComboBox cmb;
  JLabel lbl1;

  public void init()
  {
  con = getContentPane();
  con.setLayout(new FlowLayout());

  cmb = new JComboBox();

  cmb.addItem("Aapdu Junagadh");
  cmb.addItem("Dev Bhumi Dwarka");
  cmb.addItem("Somnath");
  cmb.addItem("kutch");
  cmb.addItem("Rangilu Rajkot");
  cmb.addItemListener(this);
  con.add(cmb);


lbl1 = new JLabel("                   ");
con.add(lbl1);
  }
  public void itemStateChanged(ItemEvent ie)
  {
  lbl1.setText(cmb.getSelectedItem().toString());
  }
 }
OUTPUT:


*******************************************************
//JList

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

/*<applet code = "jlist" width = 500
 height = 500></applet>
*/

public class jlist extends JApplet implements ListSelectionListener
{
Container con;
JList list1;
JTextField txt1;
JScrollPane scrollpane;

String str[] = {"12", "14", "16", "18", "20", "22", "24",
"26", "28", "30", "32", "34" ,"36"};

public void init()
{
con = getContentPane();
con.setLayout(null);

txt1 = new JTextField(5);
txt1.setBounds(80, 10, 50, 20);
con.add(txt1);

list1 = new JList(str);
list1.addListSelectionListener(this);

scrollpane = new JScrollPane(list1);
scrollpane.setBounds(80, 30, 50, 100);
con.add(scrollpane);
}
public void valueChanged(ListSelectionEvent ie)
{
txt1.setText(list1.getSelectedValue().toString());
}
}
OUTPUT:


*******************************************************
//JRadioButton Buttongroup
//ActionListener interface and ActionEvent class

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

//<applet code = "JRadioButton_Buttongroup" width = 500 height = 500></applet>

public class JRadioButton_Buttongroup extends JApplet implements ActionListener
{
Container con;
JRadioButton r1, r2, r3;
ButtonGroup bg;
JLabel lbl1;

public void init()
{
con = getContentPane();
con.setLayout(new FlowLayout());

bg = new ButtonGroup();

r1 = new JRadioButton("Male");
r1.addActionListener(this);
bg.add(r1);
con.add(r1);

r2 = new JRadioButton("Female");
r2.addActionListener(this);
bg.add(r2);
con.add(r2);

r3 = new JRadioButton("Other");
r3.addActionListener(this);
bg.add(r3);
con.add(r3);

lbl1 = new JLabel("        ");
con.add(lbl1);
}
public void actionPerformed(ActionEvent ae)
{
String msg = "";
if (r1.isSelected()  == true)
lbl1.setText(r1.getLabel());
else if (r2.isSelected())
lbl1.setText(r2.getLabel());
else if (r3.isSelected())
lbl1.setText(r3.getLabel());
}
}
OUTPUT:


*******************************************************
//JPanel class
 import java.awt.*;
 import javax.swing.*;

 /*
 <applet code = "jpanel_class" width = 500
 height = 500></applet>
 */

 public class jpanel_class extends JApplet
 {
  Container con;
  JPanel p1, p2;
  JRadioButton r1, r2, r3, r4;
  JCheckBox chk1, chk2, chk3;
  ButtonGroup bg;

  public void init()
  {
  con = getContentPane();
  con.setLayout(new GridLayout(1, 2));

  p1 = new JPanel();
  p1.setLayout(new GridLayout(3, 1));
  chk1 = new JCheckBox("Reading");
  p1.add(chk1);
  chk2 = new JCheckBox("Writng");
  p1.add(chk2);
  chk3 = new JCheckBox("Playing");
  p1.add(chk3);

  p2 = new JPanel();
  p2.setLayout(new GridLayout(4,1));

  bg = new ButtonGroup();

  r1 = new JRadioButton("SC");
  bg.add(r1);
  p2.add(r1);
  r2 = new JRadioButton("ST");
  bg.add(r2);
  p2.add(r2);
  r3 = new JRadioButton("OBC");
  bg.add(r3);
  p2.add(r3);
  r4 = new JRadioButton("Other");
  bg.add(r4);
  p2.add(r4);

  con.add(p1);
  con.add(p2);
  }
 }
OUTPUT:


*******************************************************
//CardLayout

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

/*
<applet code = "cardlayout" width = 200
height = 200></applet
*/
public class cardlayout extends JApplet implements ItemListener
{
Container con;
JPanel p1, p2, p3, p4;
JButton b1, b2, b3;
JLabel lbl1, lbl2, lbl3;

CardLayout card;
JComboBox cmb;

String Items[] = {"ButtonPanel", "Labelpanel"};

public void init()
{
con = getContentPane();
con.setLayout(new BorderLayout());

p1 = new JPanel();
p1.setLayout(new FlowLayout());

b1 = new JButton("Button - 1");
p1.add(b1);
b2 = new JButton("Button - 2");
p1.add(b2);
b3 = new JButton("Button - 3");
p1.add(b3);

p2 = new JPanel();
p2.setLayout(new FlowLayout());

lbl1 = new JLabel("Label - 1");
p2.add(lbl1);
lbl2 = new JLabel("Label - 2");
p2.add(lbl2);
lbl3 = new JLabel("Label - 3");
p2.add(lbl3);

p3 = new JPanel();
p3.setLayout(new FlowLayout());
cmb = new JComboBox(Items);
cmb.addItemListener(this);
p3.add(cmb);

card = new CardLayout();
p4 = new JPanel();
p4.setLayout(card);
p4.add(p1, "ButtonPanel");
p4.add(p2, "Labelpanel");

con.add(p3, "North");
con.add(p4, "South");
}
public void itemStateChanged(ItemEvent ie)
{
CardLayout c = (CardLayout) p4.getLayout();
c.show(p4, cmb.getSelectedItem().toString());
}
}
OUTPUT:



*******************************************************
//GroupLayout 

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

//<applet code = "grouplayout.class" width = 400
// height = 300></applet>

public class grouplayout extends JApplet
{
Container con;
JLabel lbl1;
JButton btn1;
GroupLayout group;

public void init()
{
con = getContentPane();
group = new GroupLayout(con);
con.setLayout(group);

lbl1 = new JLabel("Click Button =>");

btn1 = new JButton("Click");


group.setHorizontalGroup(
group.createSequentialGroup()
.addComponent(lbl1)
.addGap(10,20,30)
.addComponent(btn1)
);

group.setVerticalGroup(
group.createParallelGroup(
GroupLayout.Alignment.BASELINE)
.addComponent(lbl1)
.addComponent(btn1)
);
}
OUTPUT:


*******************************************************
//SpringLayout class
import java.awt.*;
import javax.swing.*;

/*<applet code = "springclass" 
width = 100 height = 100></applet>*/

public class springclass extends JApplet
{
Container con;
JLabel lbl1;
JTextField txt1;
SpringLayout spring;

public void init()
{
con = getContentPane();
spring = new SpringLayout();
con.setLayout(spring);

lbl1 = new JLabel("Username");
txt1 = new JTextField(10);

con.add(lbl1);
con.add(txt1);

spring.putConstraint(SpringLayout.WEST,lbl1,10,
SpringLayout.WEST, con);
spring.putConstraint(SpringLayout.NORTH,lbl1,10,
SpringLayout.NORTH, con);

spring.putConstraint(SpringLayout.WEST,txt1,10,
SpringLayout.EAST, lbl1);
spring.putConstraint(SpringLayout.NORTH, txt1, 10,
SpringLayout.NORTH, con);

spring.putConstraint(SpringLayout.EAST, con, 10,
SpringLayout.EAST, txt1);
spring.putConstraint(SpringLayout.SOUTH, con, 10,
SpringLayout.SOUTH, txt1);
}
}
OUTPUT:


*******************************************************
//GridBagLayout class

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

/*
<applet  code = "gridbaglayout" width = 300
 height = 200></applet>
*/

 public class gridbaglayout extends JApplet
 {
  Container con;
  JButton b1, b2, b3, b4, b5;
    GridBagLayout grid; 
    GridBagConstraints gbc;

    public void init()
    {
    grid = new GridBagLayout();
    con = getContentPane();
    con.setLayout(grid);
    gbc = new GridBagConstraints();

    b1 = new JButton("First");
    b2 = new JButton("Second");
    b3 = new JButton("Third");
    b4 = new JButton("Fourth");
    b5 = new JButton("Five");

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 0;
    con.add(b1, gbc);

   
    gbc.gridx = 1;
    gbc.gridy = 0;
    con.add(b2, gbc);

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.ipady = 20;
    gbc.gridx = 0;
    gbc.gridy = 1;
    con.add(b3, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    con.add(b4, gbc);

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    con.add(b5, gbc);

    }
 }
OUTPUT:


*******************************************************








Comments

Popular posts from this blog

7.Write a program to read a list containing item name, item code and cost interactively and produce a three-column output as shown below. NAME CODE COST Turbo C++ 1001 250.95 C Primer 905 95.70 ------------- ------- ---------- ------------- ------- ---------- Note that the name and code are left-justified and the cost is right-justified with a precision of two digits. Trailing zeros are shown.

Quetion 6 : Consider the "in-order-issue/in-order-completion" execution sequence shown in f In Figure Decode OWE Execute 12 12 12 14 16 13 16 13 15 15 16 Write 024/06/02 11 3 4 11 12 13 13 N 15 16 a. Identify the most likely reason why I could not enter the execute fourth cycle. stage until the [2] b. Will "in-order issue/out-of-order completion" or "out-of-order issue/out-of-order completion" fix this? If so, which? Explain

8.odd and even number using if else.