Java Practical4
Java Practical4
Program Code:-
package hello;
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();
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(b1, "a");
c.add(b2, "b");
c.add(b3, "c");
}
Exercise:-
Q.1) Write Java program to display following output.
Program:-
import java.awt.*;
import javax.swing.*;
public GridBagLayoutE1(){
setLayout(grid);
setTitle("GridBagLayoutExample");
this.setLayout(layout);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 0;
gc.gridx = 1;
gc.gridy = 0;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.ipadx = 20;
gc.gridx = 0;
gc.gridy = 1;
this.add(newButton("Butto Three"),gc);
gc.gridx = 1;
gc.gridy = 1;
gc.gridx = 0;
gc.gridy = 2;
gc.fill= GridBagConstraints.HORIZONTAL;
gc.gridwidth = 2;
setSize(200,200);
setPreferredSize(getSize());
setVisible(true);
}}
Output:-
Program:-
import java.awt.*;
GridBagLayoutE2() {
setLayout(new GridBagLayout());
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
add(l1, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 2;
add(tf, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
add(l2, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
add(t1, gbc);
gbc.gridy = 2;
gbc.gridwidth = 1;
add(b1, gbc);
setTitle("GridBagLayout Demo");
setSize(400, 300);
setVisible(true);
new GridBagLayoutE2();
}
}
Output:-