0% found this document useful (0 votes)
3 views7 pages

practical 4

The document contains multiple Java programs demonstrating the use of different layout managers in GUI applications, including CardLayout and GridBagLayout. Each program is accompanied by its code and expected output. The author of the document is Vyankatesh Vijay Barigade from DKTE’s Yashwantrao Chavan Polytechnic, Ichalkaranji.

Uploaded by

gxyz8837
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)
3 views7 pages

practical 4

The document contains multiple Java programs demonstrating the use of different layout managers in GUI applications, including CardLayout and GridBagLayout. Each program is accompanied by its code and expected output. The author of the document is Vyankatesh Vijay Barigade from DKTE’s Yashwantrao Chavan Polytechnic, Ichalkaranji.

Uploaded by

gxyz8837
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/ 7

DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji.

PRACTICAL NO : 4

ENROLLMENT NO: 2215770076


NAME: Vyankatesh Vijay Barigade
Program No.1
Execute the following Program and write the output.
Code:
import java.awt.*;
import
java.awt.event.*;
import javax.swing.*;

public class CardLayoutExample extends JFrame implements ActionListener


{
CardLayout card;
JButton b1, b2, b3;
Container c;
CardLayoutExample()
{ c=getContentPane(); card=new
CardLayout(40,30);
//create CardLayout object with 40 hor space and 30 ver
space c.setLayout(card); b1=new JButton("Apple"); b2=new
JButton("Boy"); b3=new JButton("Cat");
b1.addActionListener(this); b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1);c.add("b",b2);c.add("c",b3); }
public void actionPerformed(ActionEvent e)
{ card.next(c);
}
public static void main(String[] args)
{
CardLayoutExample cl=new CardLayoutExample();

cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

Vyankatesh Vijay Barigade


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji.

}
OUTPUT:

Q.2. program to display following output. Program No.2


Code:
import java.awt.*; import
javax.swing.JFrame;
import java.applet.*;
/*<applet code="GridBagLayoutExample" width=500
height=500></applet>*/

Vyankatesh Vijay Barigade


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji.

Write Java

public class GridBagLayoutExample extends Applet


{ public void init()
{
GridBagLayout g= new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(g);

GridBagLayout layout=new GridBagLayout();


this.setLayout(layout);
gbc.gridx = 0;
gbc.gridy = 0;
this.add(new Button("Button 1"), gbc);

gbc.gridx = 1; gbc.gridy
= 0;
this.add(new Button("Button 2"), gbc);

//gbc.fill =
GridBagConstraints.HORIZONTAL; gbc.ipady
= 20; gbc.gridx = 0; gbc.gridy = 1;
this.add(new Button("Button 3"),
gbc); gbc.gridx = 1; gbc.gridy = 1;
this.add(new Button("Button 4"),
gbc); gbc.gridx = 0; gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
this.add(new Button("Button 5"), gbc);
}
}

OUTPUT:

Vyankatesh Vijay Barigade


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji.

Q.3. Program to display following output. Program No.3


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

public class p4q4 extends JFrame


{ private JTextField textField;
private JTextArea textArea;
private JButton button;
public p4q4()
{ super("GridBagLayout Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

textField = new JTextField(10);


// Create the text area

Vyankatesh Vijay Barigade


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji.

Write Java

textArea= new JTextArea(10, 20);

button = new JButton("Submit");


// Create a GridBagLayout
GridBagLayout layout = new GridBagLayout();
setLayout(layout);

GridBagConstraints constraints = new GridBagConstraints();

constraints.fill = GridBagConstraints. HORIZONTAL ;


JLabel label1 = new JLabel("Name:"); JLabel
label2 = new JLabel("Comments:");
constraints.gridx=0; constraints.gridy
=0;
constraints.insets = new
Insets(5,0,0,0); add(label1,
constraints); constraints.gridx = 0;
constraints.gridy = 1;
constraints.insets = new Insets (5, 0, 0,
0); add(label2, constraints); constraints.
gridx = 1; constraints.gridy = 0;
add(textField, constraints);
constraints.gridx = 1;
constraints.gridy=1; constraints.weighty
= 1.0; add(textArea, constraints);
constraints.gridx=0;
constraints.gridy = 2;
constraints.anchor= GridBagConstraints.LINE_START;
add(button,constraints);
pack(); setVisible(true);
}
public static void main(String[] args)
{ new p4q4();
}
}

Vyankatesh Vijay Barigade


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji.

OUTPUT:

Q.2. program to display playing cards one after one.


Program No.5 Code:
import java.awt.*; import
java.awt.event.*; import
javax.swing.JFrame;
import javax.swing.*;
import java.applet.*;
/*<applet code="CardLayoutDemo" width=400 height=400></applet>*/
public class CardLayoutDemo extends JApplet implements
ActionListener {
CardLayout card;
JButton b1, b2;
ImageIcon img1, img2;
Container c; public
void init()
{ c = getContentPane(); card = new
CardLayout (20, 15);
c.setLayout(card); img1 = new
ImageIcon("Ace.png"); b1 = new
JButton(img1);

img2 = new ImageIcon("heart1.jpg");


b2 = new JButton(img2);

Vyankatesh Vijay Barigade


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji.

Write Java

b1.addActionListener(this);
b2.addActionListener(this);

c.add("a", b1);
c.add("b",
b2); }
public void actionPerformed(ActionEvent e)
{ card.next(c);
}
}

OUTPUT:

Vyankatesh Vijay Barigade

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