0% found this document useful (0 votes)
64 views4 pages

Experiment No. 4

The document contains 3 Java program code examples that demonstrate using different layout managers and GUI components in Java applets and applications. The first example uses a GridBagLayout to position 5 buttons in a grid. The second enhances the first by adding labels and text fields. The third example uses a CardLayout to flip between different card panels containing buttons on button clicks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views4 pages

Experiment No. 4

The document contains 3 Java program code examples that demonstrate using different layout managers and GUI components in Java applets and applications. The first example uses a GridBagLayout to position 5 buttons in a grid. The second enhances the first by adding labels and text fields. The third example uses a CardLayout to flip between different card panels containing buttons on button clicks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1)

Program code :-

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

public class Ex4_1 extends Applet


{
public void init()
{
setLayout(new GridBagLayout());
GridBagConstraints g=new GridBagConstraints();
g.fill=GridBagConstraints.HORIZONTAL;
Button b1=new Button("Button One");
g.gridx=0;
g.gridy=0;
add(b1,g);
Button b2=new Button("Button Two");
g.gridx=1;
g.gridy=0;
add(b2,g);
Button b3=new Button("Button Three");
g.fill=GridBagConstraints.HORIZONTAL;
g.ipady = 20;
g.gridx=0;
g.gridy=1;
g.gridheight=2;
add(b3,g);
Button b4=new Button("Button Four");
g.gridx=1;
g.gridy=1;
g.gridheight=2;
add(b4,g);
Button b5=new Button("Button Five");
g.fill=GridBagConstraints.HORIZONTAL;
g.gridx=0;
g.gridy=3;
g.gridheight=3;
g.gridwidth=2;
add(b5,g);
}
}
Output :-

2)

Program Code:-

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

public class Ex4_2 extends Applet


{
public void init()
{
setLayout(new GridBagLayout());
GridBagConstraints g=new GridBagConstraints();
Label l1=new Label("Name");
add(l1,g,0,0,1,1,0,0);

Label l2=new Label("Comments");


add(l2,g,0,1,1,1,0,0);

TextField t1=new TextField(10);


add(t1,g,1,0,1,1,0,20);

TextArea ta1=new TextArea(6,15);


add(ta1,g,1,1,1,1,0,60);

Button b1=new Button("Submit");


add(b1,g,1,2,2,1,0,20);
}
public void add(Component comp,GridBagConstraints g,int x,int y,int w,int h,int
wx,int wy)
{
g.gridx=x;
g.gridy=y;
g.gridwidth=w;
g.gridheight=h;
g.weightx=wx;
g.weighty=wy;
add(comp,g);
}
}
Output :-

3)

Program code :-

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

public class New1 extends JFrame implements ActionListener


{

CardLayout card;

Container c;

New1()
{
c=getContentPane();
card=new CardLayout(40,30);
c.setLayout(card);
JButton b1,b2,b3;
ImageIcon i1,i2;
i1=new ImageIcon("Chrysanthemum.jpg");
b1=new JButton("Apple");
b1.addActionListener(this);
c.add("a",b1);

b2=new JButton(i1);
b2.addActionListener(this);
c.add("a",b2);
b3=new JButton("Cat",i1);
b3.addActionListener(this);
c.add("a",b3);

public void actionPerformed(ActionEvent e)


{
card.next(c);

}
public static void main(String args[])
{
New1 f=new New1();
f.setSize(400,400);
f.setVisible(true);
}
}
Output :-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy