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

Exp 4

The document shows how to use the GridBagLayout in Java to create a GUI with multiple buttons arranged in a grid. It creates a JFrame, sets the layout to GridBagLayout, and adds JButtons to different cells of the grid by setting the gridx and gridy values of the GridBagConstraints.

Uploaded by

gaming ALPHA
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)
22 views7 pages

Exp 4

The document shows how to use the GridBagLayout in Java to create a GUI with multiple buttons arranged in a grid. It creates a JFrame, sets the layout to GridBagLayout, and adds JButtons to different cells of the grid by setting the gridx and gridy values of the GridBagConstraints.

Uploaded by

gaming ALPHA
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

v.

import java.awt.*;
import javax.swing.*;
public class Border1
{
JFrame jframe;
Border1()
{
jframe = new JFrame();
JButton btn2 = new JButton("DOWN");
JButton btn3 = new JButton("RIGHT");
JButton btn4 = new JButton("LEFT");
JButton btn5 = new JButton("TOP");

jframe.setLayout(new BorderLayout());
jframe.add(btn2, BorderLayout.SOUTH);
jframe.add(btn3, BorderLayout.EAST);
jframe.add(btn4, BorderLayout.WEST);
jframe.add(btn5, BorderLayout.NORTH);
jframe.setSize(300,300);
jframe.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new Border1();
}
}
Output:

X.
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);
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 c1 = new CardLayoutExample();
c1.setSize(400,400);
c1.setVisible(true);
c1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

}
Output:

XIII.
Q.1
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class GridBagLayoutExample1 extends JFrame{
public static void main(String[] args) {
GridBagLayoutExample1 a = new GridBagLayoutExample1();
}
public GridBagLayoutExample1() {

GridBagLayout GridBagLayoutgrid = new GridBagLayout();


GridBagConstraints gbc = new GridBagConstraints();
setLayout(GridBagLayoutgrid );
setTitle("GridBag Layout Example");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
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);
setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Output:

Q.2
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutExample2 extends JFrame{
public static void main(String[] args) {
GridBagLayoutExample2 a = new GridBagLayoutExample2();
}
public GridBagLayoutExample2() {

GridBagLayout GridBagLayoutgrid = new GridBagLayout();


GridBagConstraints gbc = new GridBagConstraints();
setLayout(GridBagLayoutgrid );
setTitle("GridBag Layout Example");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(new Label("Name"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
this.add(new TextField(""), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
this.add(new Label("Comments"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
this.add(new TextArea(""), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
this.add(new Button("Submit"), gbc);
setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}
Output:

You might also like