In: Computer Science
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridBagLayoutDemo {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button, c);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I need someone to go through this program and right more comments that explain a lot more Please. from top to bottom Thanks
First let's have a look at the output
The code creates a java swing gui with 5 buttons placed at some specific places , such as shown above.
Three buttons are at top plced size by size , the fourth one is just below the three buttons and occupies teh entire width , the last adn teh fifth one is ateh bottom right corner.
These buttons expand or collapse as per the window size , now let's have a look at the code
Go through tehc ode , everything has been explained via comments in the code below
Code
//these three libraries are needed for creating the swing gui
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
//class
public class GridBagLayoutDemo {
//declaring some constants varibles
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
//method which sets up the content pane
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
//this means that the gui will be based on grid layout
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
// natural height, maximum width
//this specifies to fill any horizontal extra space ,
c.fill = GridBagConstraints.HORIZONTAL;
}
//create the first button
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
//this means to fill any extra horizontal space
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;//specifies that this button will be on the first coloumn
c.gridy = 0;///specifes the row number
pane.add(button, c);//add the button
//second button
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;//column
c.gridy = 0;//row
pane.add(button, c); //place the button
//thirs button
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;//column
c.gridy = 0;//row
pane.add(button, c);
//forth button
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; // specifies the padding that is the gaps between the button name adn its surrounding
c.weightx = 0.0;
c.gridwidth = 3;//this specifies it to occupy the entire width
c.gridx = 0;//column number
c.gridy = 1;//row number
pane.add(button, c);
// the fifth button
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; // reset to default
c.weighty = 1.0; // request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; // bottom of space
c.insets = new Insets(10, 0, 0, 0); // top padding
c.gridx = 1; // aligned with button 2
c.gridwidth = 2; // 2 columns wide
c.gridy = 2; // third row
pane.add(button, c);
}
//method which creates the GUI (places 5 buttons) and rednders it
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("GridBagLayoutDemo");//here we create the window adn also specify the title for it
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // this is teh default behaviour that we want when close button is clicked
// Set up the content pane.
addComponentsToPane(frame.getContentPane());
// Display the window.
frame.pack();
frame.setVisible(true); //if not provided then the window won't be visible
}
//this is the main driver code the program
//it is the first method that gets invoked when the jaav application is run
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
//runnable has a predefined method run() which is invoked at the appropriate time
public void run() {
//call the method to craete teh respective gui and display it
//createAndShowGUI is a static method and hence can be called directly , without having to specify class name
createAndShowGUI();
}
});
}
}
Screenshot