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:
//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:
Comments
Post a Comment