Monday, May 11, 2020

write java program using Grid Bag Layout

import java.awt.Button;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
public class AWTGridBagLayoutDemo extends JFrame {
public static void main(String[] args) {
AWTGridBagLayoutDemo gbld = new AWTGridBagLayoutDemo();
}
public AWTGridBagLayoutDemo() {
setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbcnt = new GridBagConstraints();
setLayout(gbl);
setTitle("Java Layout Manager: GridBag Layout");
GridBagLayout layout = new GridBagLayout(); //create grid bag layout
this.setLayout(layout);
//below code is customization of grid fashion
gbcnt.fill = GridBagConstraints.HORIZONTAL;
gbcnt.gridx = 1;
gbcnt.gridy = 0;
this.add(new Button("Smart Phone"), gbcnt);
gbcnt.gridx = 2;
gbcnt.gridy = 0;
this.add(new Button("Laptop"), gbcnt);
gbcnt.fill = GridBagConstraints.HORIZONTAL;
gbcnt.ipady = 20;
gbcnt.gridx = 1;
gbcnt.gridy = 1;
this.add(new Button("Tablet"), gbcnt);
gbcnt.gridx = 2;
gbcnt.gridy = 1;
this.add(new Button("Desktop"), gbcnt);
gbcnt.gridx = 1;
gbcnt.gridy = 2;
gbcnt.fill = GridBagConstraints.HORIZONTAL;
gbcnt.gridwidth = 2;
this.add(new Button("Computer"), gbcnt);
}
}

No comments:

Post a Comment